如何更新CheckBoxMultipleChoice的模型?

时间:2017-07-25 21:58:02

标签: java wicket

ClassChoice控件继承CheckBoxMultipleChoice。它是在多个页面上使用的常用控件,会话中保留了选择。可用的选项从数据库中获得。一个"所有"当有多个数据项时添加复选框。在某些页面上,选择更改会导致使用新数据刷新页面。在其他页面上,选择应该更改而不刷新。

我的问题是我需要控制"全部"当其他复选框更改时复选框,并在"全部"时更改所有复选框。复选框更改。

我试图致电updateModel()强制进行更改,但这不起作用。如何在不刷新页面的情况下更改选择(model参数)?

此编辑的代码不会显示页面刷新。

public class ClassChoice<T> extends CheckBoxMultipleChoice
{
    private static final long serialVersionUID = 1L;

    @SpringBean
    private ClassService classService;

    List<EntityClassModel> selection;
    EntityClassModel ecmAll;

    static List<EntityClassModel> availableClasses;

    public ClassChoice(..)
    {
        super("classcheckboxes");

        setSuffix(" "); // sets checkbox separator and ensures inline display

        ecmAll = (EntityClassModel) modelFactory.getNewClassModel();
        ecmAll.setClassname("All");

        // List of all classes associated with user
        availableClasses = classService.getListOfClasses(..);
        setClassChoices();

        add( new AjaxFormChoiceComponentUpdatingBehavior()
        {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onUpdate(AjaxRequestTarget target)
            {
                List<Integer> previousIDs = UserSession.get().getSelectedClassIDs();
                if ((previousIDs.size() > 0) && ((previousIDs.size() + 1)  >= availableClasses.size()))
                {
                    // Was previously Select All
                    if (selection.get(selection.size() - 1) == ecmAll)
                    {
                        // Select All still selected, remove it
                        selection.remove(selection.size() - 1);
                    }
                    else
                    {
                        // Remove all selections
                        selection.clear();
                    }
                }
                else if (selection.size() > 0)
                {
                    // Was none or some selected
                    if (selection.get(selection.size() - 1) == ecmAll)
                    {
                        // Select All, select all available
                        selection.clear();
                        selection.addAll(availableClasses);
                    }
                    else if ((selection.size() + 1) >= availableClasses.size())
                    {
                        // Is now full, add Select All
                        selection.add(ecmAll);
                    }
                    // else change but no special handling required
                }
                // else none are currently selected

                UserSession.get().setSelectedClasses(selection);

                // Generate a list of selected class IDs, excluding All
                List<Integer> selectedIDs = new ArrayList<Integer>();
                int copysize = selection.size();
                if ((copysize > 0) && (selection.get(copysize - 1) == ecmAll))
                {
                    copysize--;
                }
                for (int index = 0; index < copysize; index++)
                {
                    selectedIDs.add(selection.get(index).getId());
                }
                UserSession.get().setSelectedClassIDs(selectedIDs);

                // Update the selections on the page
                updateModel();
            }                   
        });
        Initialize();
    }

    @SuppressWarnings("unchecked")
    protected void Initialize()
    {   
        // Grabs already selected classes from UserSession
        List<Integer> selectedIDs = UserSession.get().getSelectedClassIDs();
        selection = classService.getClassesByClassIDs(selectedIDs);
        if (selectedIDs.size() > 1)
        {
            if ((selectedIDs.size() + 1) >= availableClasses.size())
            {
                selection.add(ecmAll);
            }
        }
        setModel(Model.ofList(selection));

        // Configure the data and display
        setChoiceRenderer(new ChoiceRenderer<EntityClassModel>("classname", "id"));
        setOutputMarkupId(true);
    }

    @SuppressWarnings("unchecked")
    public void setClassChoices()
    {
        // Adds 'All' option when there is more than one class
        if (availableClasses.size() > 1)
        {
            availableClasses.add(ecmAll);
        }
        setChoices(availableClasses);

    }

    public List<EntityClassModel> getSelection()
    {
        return selection;
    }
}

1 个答案:

答案 0 :(得分:2)

您必须使用AjaxRequestTarget更新浏览器端的HTML元素。通过向selection添加/删除元素,您可以在服务器端更改ClassChoice的模型。在AjaxFormChoiceComponentUpdatingBehavior#onUpdate()的底部,你应该target.add(this)告诉Wicket用它的新选择/模型重新绘制这个ClassChoice实例。

请确保在其构造函数中调用setOutputMarkupId(true),否则您将无法使用Ajax更新它。