简体   繁体   English

将选中的复选框列表从Javascript返回到Wicket

[英]Return list of checked checkboxes from Javascript to Wicket

I am trying to make a filter for a searchfield where a number of checkboxes can be checked to choose what people want to search. 我正在尝试为搜索字段创建一个过滤器,在其中可以选中许多复选框以选择人们想要搜索的内容。 I am currently trying to do this with the CheckGroup component but as I do not have a submit button I do not know how I can retrieve the latest checked objects. 我目前正在尝试使用CheckGroup组件执行此操作,但是由于我没有提交按钮,因此我不知道如何检索最新的检查对象。 One thought of doing it was using Javascript, to call a function in Javascript and retrieve all the checkboxes like that. 这样做的一种想法是使用Javascript,在Javascript中调用一个函数并检索所有类似的复选框。 I currently have the following code in Wicket. 我目前在Wicket中有以下代码。 So my question would be how to do this and if it is possible to not do this with Javascript. 所以我的问题是如何做到这一点,以及是否有可能不使用Javascript做到这一点。 I have tried using AjaxFormChoiceComponentBehaviour and that works but since it does a post whenever a checkbox is checked, I think JS would be a better option. 我已经尝试过使用AjaxFormChoiceComponentBehaviour,并且可以工作,但是由于每当复选框被选中时它都会发布,因此我认为JS是一个更好的选择。

public Filter(String id) {
    super(id);
    form = new Form("filterform");
    types = resultItemHandlerPool.getTypes();

    checkGroup = new CheckGroup<Class<?>>("checkGroup", new PropertyModel<Collection<Class<?>>>(this,"types"));

    ListView typesListview = new ListView<Class<?>>("typesList", new PropertyModel<List<? extends Class<?>>>(this,"types")) {
        @Override
        protected void populateItem(final ListItem<Class<?>> item) {
            item.add(new Check<Class<?>>("check", item.getModel()));  
            item.add(new Label("className", item.getModelObject().getSimpleName()));
        }
    };
    typesListview.setReuseItems(true);

    checkGroup.add(typesListview);

    form.add(checkGroup);
    add(form);
}

public List<Class<?>> getSearchableTypes() {
  return types;
}

Thanks and kind regards, 谢谢和亲切的问候,

Merlijn 梅林

You say you want to do the search server side. 您说要在搜索服务器端进行操作。 So, the server needs to know which items are checked in order to do the search. 因此,服务器需要知道检查了哪些项目才能进行搜索。

Just use a plain old form for the searchfield (including checkboxes) and make it so that after entering the search-value the user posts the form. 只需对搜索字段使用简单的旧表单(包括复选框)并进行设置,以使用户在输入搜索值后即可发布该表单。 That way, the serverside code will receive the search value and the list of checked checkboxes and will know exactly what to search for. 这样,服务器端代码将接收搜索值和选中的复选框列表,并将确切知道要搜索的内容。

AjaxFormChoiceComponentBehaviour does indeed update the server side Checkgroup after every click with an ajax post. 每次单击带有ajax帖子的AjaxFormChoiceComponentBehaviour确实会更新服务器端Checkgroup。 If you only need to know the value of the Checkgroup after posting the search value, just don't use the AjaxFormChoiceComponentBehaviour and submit the form. 如果您只需要在发布搜索值后才知道Checkgroup的值,则不必使用AjaxFormChoiceComponentBehaviour并提交表单。 Both a normal form submit and an ajax submit will work here. 普通表单提交和ajax提交都可以在这里工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM