You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Azzeddine Daddah <wa...@gmail.com> on 2009/02/11 15:24:01 UTC

AutoCompleteTextField value is always empty

Hi,

I'm trying to get the value of an AutoCompleteTextField but it returns
always an empty String. Also clearing this filed does not work.
When I click the "Add" link a new container is added but because the tag
value is empty, nothing is shown. Could someone please tell me what am I
doing wrong here:

public class HomePanel extends MainContentPanel {

    private WebMarkupContainer tagsContainer = new
WebMarkupContainer("tagsContainer");

    private List<String> tags = new ArrayList<String>();

    /**
     * Creates a new {@link HomePanel}.
     *
     * @param id the non <code>null</code> id
     */
    public HomePanel(String id) {
        super(id, "Dashboard");
        mainContent.add(new Label("test", new Model<String>("Test me")));

        Form<Void> form = new Form<Void>("form");
        mainContent.add(form);
        final AutoCompleteTextField<String> auto = new
AutoCompleteTextField<String>("auto") {
            @Override
            protected Iterator<String> getChoices(String input) {
                if (StringUtils.isEmpty(input)) {
                    List<String> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }

                List<String> choices = new ArrayList<String>(10);
                List<String> dummy = generateDummyChoices();
                for (String choice : dummy) {
                    if
(choice.toUpperCase().startsWith(input.toUpperCase())) {
                        choices.add(choice);
                        if (choices.size() == 10) break;
                    }
                }
                return choices.iterator();
            }

            private List<String> generateDummyChoices() {
                return Arrays.asList("agile", "ajax", "apache", "api",
"apml", "applescript", "architecture", "auth",
                        "autocomplete", "beautify", "bug", "bugs", "C",
"canvas", "cheatsheet", "closure", "Cocoa", "code",
                        "codedump", "comet", "compiler", "compression",
"compressor", "Computer", "crossdomain", "csrf", "css3",
                        "D", "dashcode", "debug", "debugger", "debugging",
"development", "dom", "ext", "firebug", "firefox",
                        "flash", "flex", "framework", "functions",
"greasemonkey", "hack", "hacks", "howto", "html", "html5",
                        "ie", "iframe", "iframes", "innerhtml", "input",
"Java", "javascript", "jquery", "js", "js2", "keycodes",
                        "keypress", "LAMP", "language", "languages", "leak",
"leaks", "livesearch", "memory", "memoryleak",
                        "minify", "moo", "mootools", "namespace", "nu",
"oauth", "obfuscate", "obfuscator", "objective-c",
                        "onload", "oop", "opml", "optimisation",
"optimised", "optimization", "pack", "packer", "performance",
                        "perl", "php", "plugin", "plugins", "programming",
"prototype", "prototyping", "rail", "rails", "regexp",
                        "replacehtml", "reserved", "rest", "ruby",
"scripting", "scroll", "scrolling", "sdk", "snippet");
            }
        };
        form.add(auto);
        form.add(new AjaxLink<String>("addLink", new Model<String>("Add")) {
            @Override
            public void onClick(AjaxRequestTarget target) {
                String inputValue = auto.getModelObject();
                /*if (StringUtils.isNotBlank(inputValue))*/
tags.add(inputValue);
                auto.clearInput();
                target.addComponent(tagsContainer);
            }
        });

        tagsContainer.setOutputMarkupId(true);
        tagsContainer.add(new ListView<String>("tags", new
PropertyModel<List<String>>(this, "tags")) {
            @Override
            protected void populateItem(ListItem<String> item) {
                item.add(new Label("tag", item.getModelObject()));
            }
        });
        form.add(tagsContainer);
    }

    public List<String> getTags() {
        return tags;
    }
}


<html xmlns:wicket>
    <wicket:panel>
        <wicket:extend>
            <span wicket:id="test"/>
            <form wicket:id="form">
                <input wicket:id="auto" type="text"/> <a
wicket:id="addLink">Add</a>
                <div wicket:id="tagsContainer">
                    <div wicket:id="tags">
                        <span wicket:id="tag" style="margin-right: 10px;"/>
                    </div>
                </div>
            </form>
        </wicket:extend>
    </wicket:panel>
</html>


Regards,

Hbiloo