This is what I've till now. Clearing the text field does not work:
private void addTagsPart(final Recipe recipe) {
final WebMarkupContainer tagsContainer = new
WebMarkupContainer("tagsContainer");
final TagsAutoCompleteTextField tagsTextfield = new
TagsAutoCompleteTextField("tags", new Model<Tag>());
add(tagsTextfield);
tagsTextfield.add(new AjaxFormComponentUpdatingBehavior("onchange")
{
@Override
protected void onUpdate(AjaxRequestTarget target) {
String inputValue = tagsTextfield.getValue();
if (StringUtils.isNotBlank(inputValue)) {
String[] splittedInputValues =
StringUtils.split(inputValue, ",");
for (String value : splittedInputValues) {
recipe.addTag(new Tag(value));
}
}
}
});
add(new IndicatingAjaxLink<String>("addTagLink") {
@Override
public void onClick(AjaxRequestTarget target) {
target.addComponent(tagsContainer);
target.appendJavascript(String.format("document.getElementById(%s).value =
''", tagsTextfield.getMarkupId()));
target.appendJavascript("new Effect.Shake($('" +
tagsContainer.getMarkupId() + "'));");
}
});
tagsContainer.add(new ListView<Tag>("tags", new
PropertyModel<List<Tag>>(recipe, "tags")) {
@Override
protected void populateItem(ListItem<Tag> item) {
Tag tag = item.getModelObject();
item.add(new Label("tag", tag.getName()));
}
});
tagsContainer.setOutputMarkupId(true);
add(tagsContainer);
}
...
private class TagsAutoCompleteTextField extends AutoCompleteTextField<Tag> {
public TagsAutoCompleteTextField(String id, final IModel<Tag> model)
{
super(id, model, new AbstractAutoCompleteTextRenderer<Tag>() {
@Override
protected String getTextValue(Tag tag) {
return tag.getName();
}
});
}
@SuppressWarnings("unchecked")
@Override
protected Iterator<Tag> getChoices(String input) {
if (StringUtils.isBlank(input)) return
Collections.EMPTY_LIST.iterator();
List<Tag> choices = new ArrayList<Tag>(10);
List<Tag> tags =
RecipeService.getInstance().getAllPersistentTags();
for (Tag tag : tags) {
if
(tag.getName().toUpperCase().startsWith(input.toUpperCase())) {
choices.add(tag);
if (choices.size() == 10) break;
}
}
return choices.iterator();
}
}
Regards,
Hbiloo
On Thu, Feb 12, 2009 at 5:00 PM, Martin Makundi <
[email protected]> wrote:
> If you copy-paste your code here we can probably dig into it better ...
>
> **
> Martin
>
> 2009/2/12 Azzeddine Daddah <[email protected]>:
> > Thanks guys for the quick replay.
> > Martin: What I want to do is to select a value from the auto complete
> text
> > field, and when clicking the "Add" button, the selected value should be
> > added to the tagsContainer (which is in fact a div that wraps the added
> > values) and cleared from the text field.
> >
> > Luca: I've tried your suggestion but it didn't help. I've also tried to
> > clear the text field using Javascript
> >
> target.appendJavascript(String.format("document.getElementById('%s').value =
> > ''", auto.getOutputMarkupId()));
> > but it also didn't work.
> >
> > Are there any other suggestions ways to implement this?
> >
> >
> >
> > Kind Regards
> >
> > Hbiloo
> >
> >
> >
> >
> >
> > On Thu, Feb 12, 2009 at 11:24 AM, Martin Makundi <
> > [email protected]> wrote:
> >
> >> Also depending on your situation, clearInput might be necessary?
> >>
> >> **
> >> Martin
> >>
> >> 2009/2/12 Luca Provenzani <[email protected]>:
> >> > i think you can put the field of the model/bean of the form to empty
> and
> >> > then call target.addComponet(tagsContainer);
> >> > what kind of effect do you want? If you need to call javascript you
> can
> >> use
> >> > target.appendJavaScript()...
> >> >
> >> > Luca
> >> >
> >> > 2009/2/12 Azzeddine Daddah <[email protected]>
> >> >
> >> >> Hello everyone,
> >> >>
> >> >> Could someone please tell me how I can clear the selected value of an
> >> >> AutoCompleteTextField. I've an AutoCompleteTextField and a link which
> >> >> should
> >> >> clear the value of the text field after clicking it. I would also do
> >> some
> >> >> effects after clearing this value. Below my code:
> >> >>
> >> >> final AutoCompleteTextField<String> auto = new
> >> >> AutoCompleteTextField<String>("auto", new Model<String>())
> >> >> ...
> >> >> form.add(auto);
> >> >>
> >> >> form.add(new IndicatingAjaxLink<String>("addLink", new
> >> >> Model<String>("Add"))
> >> >> {
> >> >> @Override
> >> >> public void onClick(AjaxRequestTarget target) {
> >> >> String inputValue = auto.getValue();
> >> >> auto.clearInput();
> >> >> // How to clear the auto's value and add some effects to
> the
> >> >> tagsContainer?
> >> >> target.addComponent(tagsContainer);
> >> >> }
> >> >> });
> >> >>
> >> >>
> >> >> Kind Regards,
> >> >>
> >> >> Hbiloo
> >> >>
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [email protected]
> >> For additional commands, e-mail: [email protected]
> >>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>