Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-1998

Listbox using GroupsModel sometimes would not update the select all status.

XMLWordPrintable

    • None

      Fiddle sample: http://zkfiddle.org/sample/38emvt5/2-Select-all-status-not-sync-d-for-Listbox-with-GroupsModel

      Select-all checkbox remains unchecked even though the groups model says everything is selected.

      Note:
      1. If manually clicked the "select-all" checkbox for the listbox to enter "select-all" state, then select/deselect the individual items would update the select-all status.
      2. However, if manually clicked the "select-all" checkbox for the listbox to enter "select-none" state, then select/deselect the individual items would not update the select-all status.

      test.zul
      <window apply="pkg$.TestComposer">
          <listbox id="listbox">
              <listhead>
                  <listheader label="Category" />
                  <listheader label="Name" />
                  <listheader label="Top Nutrients" />
              </listhead>
          </listbox>
      </window>
      
      TestComposer.java
      package pkg$;
      
      import java.util.ArrayList;
      import java.util.Comparator;
      import java.util.List;
      import java.util.Set;
      
      import pkg$.TestComposer.FoodGroupModel.FoodGroupInfo;
      
      import org.zkoss.zk.ui.event.SelectEvent;
      import org.zkoss.zk.ui.select.SelectorComposer;
      import org.zkoss.zk.ui.select.annotation.Listen;
      import org.zkoss.zk.ui.select.annotation.Wire;
      import org.zkoss.zul.GroupComparator;
      import org.zkoss.zul.GroupsModelArray;
      import org.zkoss.zul.Label;
      import org.zkoss.zul.ListModelArray;
      import org.zkoss.zul.Listbox;
      import org.zkoss.zul.Listcell;
      import org.zkoss.zul.Listgroup;
      import org.zkoss.zul.Listgroupfoot;
      import org.zkoss.zul.Listheader;
      import org.zkoss.zul.Listitem;
      import org.zkoss.zul.ListitemRenderer;
      import org.zkoss.zul.Window;
      
      @SuppressWarnings("serial")
      public class TestComposer extends SelectorComposer<Window> {
          
          static class Food {
              private String category;
              private String name;
              private String topNutrients;
              
              public Food(String category, String name, String topNutrients) {
                  setCategory(category);
                  setName(name);
                  setTopNutrients(topNutrients);
              }
      
              public String getCategory() {
                  return category;
              }
              public void setCategory(String category) {
                  this.category = category;
              }
              
              public String getName() {
                  return name;
              }
              public void setName(String name) {
                  this.name = name;
              }
              
              public String getTopNutrients() {
                  return topNutrients;
              }
              public void setTopNutrients(String topNutrients) {
                  this.topNutrients = topNutrients;
              }
          }
          
          static class FoodData {
              private static List<Food> foods = new ArrayList<Food>();
              static {
                  foods.add(new Food("Vegetables", "Asparagus", "Vitamin K"));
                  foods.add(new Food("Vegetables", "Beets", "Folate"));
                  foods.add(new Food("Seafood", "Salmon", "Tryptophan"));
                  foods.add(new Food("Seafood", "Shrimp", "Tryptophan"));
              }
              
              public static List<Food> getAllFoods() {
                  return foods;
              }       
          }
          
          static class FoodGroupComparator implements GroupComparator<Food> {
              public int compareGroup(Food food1, Food food2) {
                  return Math.abs(compare(food1, food2));
              }
      
              public int compare(Food food1, Food food2) {
                  return food1.getCategory().compareTo(food2.getCategory());
              }
          }
          
          static class FoodGroupModel extends GroupsModelArray<Food, FoodGroupModel.FoodGroupInfo, Object, Object> {
              public FoodGroupModel(Food[] data, Comparator<Food> compr) {
                  super(data, compr);
              }
              
              protected FoodGroupInfo createGroupHead(Food[] groupData, int index, int column) {
                  return new FoodGroupInfo(groupData[0], index, column);
              }
              
              protected Object createGroupFoot(Food[] groupData, int index, int column) {
                  return groupData.length;
              }
              
              static class FoodGroupInfo {
                  private Food firstChild;
                  private int groupIndex;
                  private int columnIndex;
                  
                  public FoodGroupInfo(Food firstChild, int groupIndex, int columnIndex) {
                      this.firstChild = firstChild;
                      this.groupIndex = groupIndex;
                      this.columnIndex = columnIndex;
                  }
      
                  public Food getFirstChild() {
                      return firstChild;
                  }
                  public int getGroupIndex() {
                      return groupIndex;
                  }
                  public int getColumnIndex() {
                      return columnIndex;
                  }
              }
          }
          
          static class FoodGroupRenderer implements ListitemRenderer<Object> {
              public void render(Listitem item, Object obj, int index) throws Exception {
                  if (item instanceof Listgroup) {
                      FoodGroupInfo groupInfo = (FoodGroupInfo) obj;
                      Food food = groupInfo.getFirstChild();
                      String groupTxt;
                      switch (groupInfo.getColumnIndex()) {
                      case 0:
                          groupTxt = food.getCategory();
                          break;
                      case 1:
                          groupTxt = food.getName();
                          break;
                      case 2:
                          groupTxt = food.getTopNutrients();
                          break;
                      default:
                          groupTxt = food.getCategory();
                      }
                      item.appendChild(new Listcell(groupTxt));
                      item.setValue(obj);
                  } else if (item instanceof Listgroupfoot) {
                      Listcell cell = new Listcell();
                      cell.appendChild(new Label("Total " + obj + " Items"));
                      cell.setSpan(3);
                      item.appendChild(cell);
                  } else {
                      Food data = (Food) obj;
                      item.appendChild(new Listcell(data.getCategory()));
                      item.appendChild(new Listcell(data.getName()));
                      item.appendChild(new Listcell(data.getTopNutrients()));
                      item.setValue(data);
                  }
              }
          }
          
          @Wire
          private Listbox listbox;
          
          private GroupsModelArray<Food, FoodGroupInfo, Object, Object> groupModel;
          
          private ListitemRenderer<Object> groupRenderer = new FoodGroupRenderer();
      
          @Override
          public void doAfterCompose(Window comp) throws Exception {
              super.doAfterCompose(comp);
              org.zkoss.lang.Library.setProperty("org.zkoss.zul.listbox.rod","false");
      
              Food[] foods = FoodData.getAllFoods().toArray(new Food[] {});
              groupModel = new FoodGroupModel(foods, new FoodGroupComparator());
              groupModel.setMultiple(true);
              for (Food food : foods)
                  groupModel.addToSelection(food);
      
              listbox.setCheckmark(true);
              listbox.setModel(groupModel);
              listbox.setItemRenderer(groupRenderer);        
          }    
      }
      

            vincentjian vincentjian
            neillee neillee
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: