我如何在Wicket AutoCompleTextField中设置值

时间:2019-03-22 06:50:32

标签: java wicket

我有我的AutoCompleteTextField

    private class AutoCompleteField extends AutoCompleteTextField<City> {

    public AutoCompleteField(String autoField, HibernateModel<City> selectedWorkTemplate,
                             IAutoCompleteRenderer<City> autoCompleteRenderer) {
        super(autoField, selectedWorkTemplate, autoCompleteRenderer);
    }

    @Override
    protected Iterator<City> getChoices(String input) {
        return input == null ? Collections.emptyIterator() :
                cityRepository.findByNameLike(input).iterator();
    }
}

我这样宣布

IAutoCompleteRenderer<City> autoCompleteRenderer = new AbstractAutoCompleteTextRenderer<City>() {
        @Override
        protected String getTextValue(City object) {
            return object.getName();
        }
    };
    cityField = new AutoCompleteField("cityField", selectedCityModel, autoCompleteRenderer);
    form.add(cityField);

例如,如何通过按钮设置值? 我尝试在selectedCityModel中进行设置,但是它什么也没做,AutoCompleteTextField不会为该模型获取/设置值。

1 个答案:

答案 0 :(得分:0)

将我的班级更改为

private class AutoCompleteField extends AutoCompleteTextField<String> {

public AutoCompleteField(String autoField, IModel<String> selectedCityListNameModel) {
    super(autoField, selectedCityListNameModel);
}

@Override
protected Iterator<String> getChoices(String input) {
    return input == null ? Collections.emptyIterator() :
            cityRepository.findByNameLike(input).iterator();
}

}

selectedCityListNameModel声明

private IModel<String> selectedCityListNameModel = Model.of("");

所以,现在设置了AutoCompleteField并从selectedCityListNameModel中获取值(这很奇怪……我认为AutoCompleteTextField的构造函数与渲染器无法正常工作)

现在,如果我需要在开始时为此组件设置非空值,则需要dl:selectedCityListNameModel.setObject("some String"),就像我们需要为简单的TextField做