如何在提交后清除Wicket中的autoComplete文本字段?

时间:2013-09-09 16:06:41

标签: wicket

我在清除autoComplete Textfield的输入时遇到问题。出于某种原因我无论何时调用target.add(引用AutoCompleteTextField);

我的输入值为空...

基本上,我可以清除输入存储的引用字符串,但每次调用ajaxupdate时它都为空。

我尝试了ajaxButton和AjaxSubmitLink。两者都得到了相同的反应。我有一个按钮来提交textField的输入。

代码:

        <form wicket:id="autoCompleteForm">
          <td><input wicket:id="autoCompleteTextField" size="20"/></td>
          <td><button width:100px wicket:id="selectRoleBtn">Select</button></td>    
        </form>


private void autoCompleteForm()
{
    findRoleForm = new Form<Void>("autoCompleteForm");
    findRoleForm.setOutputMarkupId(true);
    addOrReplace(findRoleForm);
    field = new AutoCompleteTextField<String>("autoCompleteTextField",
            new PropertyModel<String>(this,"autoString"))
        {
            @Override
            protected Iterator<String> getChoices(String input)
            {
                if (Strings.isEmpty(input))
                {
                    List<String> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }

                List<String> choices = new ArrayList<String>(10);

                for (final Role role : rolesList)
                {

                    final String roles = role.getRoleName();

                    if (roles.toUpperCase().startsWith(input.toUpperCase()))
                    {
                        choices.add(roles);
                        if (choices.size() == 10)
                        {
                            break;
                        }
                    }
                }

                return choices.iterator();
            }
        };
        findRoleForm.addOrReplace(field);
        findRoleForm.addOrReplace(new AjaxSubmitLink("selectRoleBtn", findRoleForm)
        {
            protected void onSubmit(AjaxRequestTarget target, Form<?> form)
            {
                System.out.println("here1" + autoString);
                if(rolesList  != null && autoString!= null)
                {
                    if(rolesList .size() != 0)
                    {   
                        for(int i=0; i < rolesList .size(); i++)
                        {
                            System.out.println("here2" + autoString);
                            if(rolesList .get(i).getRoleName().equals(autoString))
                            {
                                role = rolesList.get(i);

                                roleInformation.addOrReplace(new Label("roleNameTxt", role.getRoleName()));
                                roleInformation.addOrReplace(new Label("roleAliasTxt", role.getRoleAlias()));
                                roleInformation.addOrReplace(new Label("roleOwnerTxt", role.getRoleOwnerId()));
                                roleInformation.addOrReplace(new Label("roleStatusTxt", role.getRoleAccessStatus()));
                                roleInformation.addOrReplace(new Label("roleCategoryTxt", role.getRoleCategoryName()));
                                roleInformation.addOrReplace(new Label("roleDescriptionTxt", role.getRoleDescription()));
                                roleInformation.addOrReplace(new Label("roleValidityTxt", role.getRoleValidityStatus()));
                                roleInformation.addOrReplace(new Label("roleNumUsers", ""));


                                //add adOrReplace(findRoleForm);
                                autoString = "";
                                target.add(field);
                                target.add(roleInformation);
                                currentRoleSelection = null;
                                target.add(rolesDropDownChoice);
                                break;
                            }
                        }
                    }
               }
            }
        }).add(getIndicatorAppender());
}

编辑: autoCompleteTextField输入字段第一次清除,但是当我再次尝试时。字符串:autoString将为null。因此,如果您在第一次尝试时选择了您的选择(从提供的搜索列表中),点击选择按钮,它会为您提供正确的字符串并清除。但是当你第二次这样做,选择一个提供的值时,“autoString”将为null ..并且不会获得分配给它的输入值。

2 个答案:

答案 0 :(得分:0)

你能更恰当地描述你的问题吗?

我必须修改你的代码才能编译

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.string.Strings;

public class AutoTestPage2 extends WebPage {

    Form<Void> findRoleForm;
    AutoCompleteTextField field;
    List<Role> rolesList = new LinkedList<AutoTestPage2.Role>();
    String autoString;

    public AutoTestPage2() {
        super();
        autoCompleteForm();
    }

    private void autoCompleteForm() {
        rolesList.add(new Role("Adam"));
        rolesList.add(new Role("Boris"));
        rolesList.add(new Role("Clair"));

        findRoleForm = new Form<Void>("autoCompleteForm");
        findRoleForm.setOutputMarkupId(true);
        add(findRoleForm);
        field = new AutoCompleteTextField<String>("autoCompleteTextField",
                new PropertyModel<String>(this, "autoString")) {
            @Override
            protected Iterator<String> getChoices(final String input) {
                if (Strings.isEmpty(input)) {
                    final List<String> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }

                final List<String> choices = new ArrayList<String>(10);

                for (final Role role : rolesList) {

                    final String roles = role.getName();

                    if (roles.toUpperCase().startsWith(input.toUpperCase())) {
                        choices.add(roles);
                        if (choices.size() == 10) {
                            break;
                        }
                    }
                }

                return choices.iterator();
            }
        };
        findRoleForm.addOrReplace(field);
        findRoleForm.addOrReplace(new AjaxSubmitLink("selectRoleBtn", findRoleForm) {
            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                System.out.println("here1" + autoString);
                if (rolesList != null && autoString != null) {
                    if (rolesList.size() != 0) {
                        for (int i = 0; i < rolesList.size(); i++) {
                            System.out.println("here2" + autoString);
                            if (rolesList.get(i).getName().equals(autoString)) {
                                final Role role = rolesList.get(i);

                                //                                roleInformation.addOrReplace(new Label("roleNameTxt", role.getName()));

                                //add adOrReplace(findRoleForm);
                                autoString = "";
                                target.add(field);
                                //                                target.add(roleInformation);
                                //                                currentRoleSelection = null;
                                //                                target.add(rolesDropDownChoice);
                                break;
                            }
                        }
                    }
                }
            }
        })/* .add(getIndicatorAppender()) */;

        //        add(findRoleForm);
    }

    class Role implements Serializable {

        String name;

        public Role(final String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

    }
}

当我按下select时,文本字段被清除,控制台中有两条消息

here1Adam
here2Adam

答案 1 :(得分:0)

我能够通过将其他组件放在与autoCompleteTextField相同的表单中来解决这个问题,无论出于何种原因,当重新呈现它们并且它们在表单内时...它会纠正问题。