2017-07-25 40 views
0

控制ClassChoice继承CheckBoxMultipleChoice。这是在多个页面上使用的通用控件,其中会话中保留了选择。可用选项从数据库中获得。当有多个数据项时,添加“全部”复选框。在某些页面上,选择更改会导致页面被新数据刷新。在其他页面上,选择应该更改而不刷新。如何更新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; 
    } 
} 

回答

2

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

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