1. #1
    Sencha User
    Join Date
    May 2011
    Posts
    14
    Vote Rating
    0
    gxtforumloginac is on a distinguished road

      0  

    Question TreePanel check changed events??

    TreePanel check changed events??


    Hi,

    I would like to know why i click on a leafNode, Node A with 3 children, the Events.CheckChange listener will be fired for 3 times. Is there any listener that listen to the tree panel, even i checked the root node, it is only fired once. This is because i need to get the checked items and pass the checked items information to database and get the data to build other tree. i dont want the Events.CheckChange is fired 3 times and go to database 3 times.

    whatever which of the following listener is used, if you check the leafNode, they will be fired many times which depend on how many children the node has.




    Code:
    1. // overall checked state changes
    2. tree.addCheckListener(new CheckChangedListener<ModelData>() { </SPAN>
    3. @Override </SPAN>
    4. public void checkChanged(CheckChangedEvent<ModelData> event) { </SPAN>
    5. }
    6. });
    7. // change in node check state </SPAN>
    8. tree.addListener(Events.CheckChange, new Listener<TreePanelEvent<ModelData>>() { </SPAN>
    9. public void handleEvent(TreePanelEvent<ModelData> be) { </SPAN>
    10. }
    11. });
    Hope anyone can help please. Thanks a lot.

  2. #2
    Sencha User
    Join Date
    Feb 2012
    Posts
    7
    Vote Rating
    -1
    Snahrck is an unknown quantity at this point

      -1  

    Default First post

    First post


    The TreePanel documentation stats that the event CheckChange "Fires after a item's check state changes.".

    However, there is also the event "CheckChanged" which documentation says "Fires after the tree's overall checked state changes." Nevertheless, this event is also called several times when the parent of other check nodes is checked. I don't know if it is a bug or if it is the expected behavior, but I've made a change to the ThreePanel class so that CheckChanged behave like we thought it should do.

    I added a cascade version of the method "setChecked" that I call "setCascadeChecked" which is a copy of "setChecked" whithout firing CheckChanged. Then I modified the method "onCheckCascade" to call setCascadeChecked instead of setChecked. Here is the code for both methods:

    Code:
    protected void onCheckCascade(M model, boolean checked) {
        switch (getCheckStyle()) {
          case PARENTS:
            if (checked) {
              M p = store.getParent(model);
              while (p != null) {
                setCascadeChecked(p, true);
                p = store.getParent(p);
              }
            } else {
              for (M child : store.getChildren(model)) {
                setCascadeChecked(child, false);
              }
            }
            break;
          case CHILDREN:
            for (M child : store.getChildren(model)) {
                setCascadeChecked(child, checked);
            }
        }
      }
    
    protected void setCascadeChecked(M item, boolean checked) {
        if (!checkable) return;
        TreeNode node = findNode(item);
        if (node != null) {
          if (node.checked == checked) {
            onCheckCascade(item, checked);
            return;
          }
    
    
          boolean leaf = node.isLeaf();
          if ((!leaf && checkNodes == CheckNodes.LEAF) || (leaf && checkNodes == CheckNodes.PARENT)) {
            return;
          }
    
    
          TreePanelEvent<M> evt = new TreePanelEvent<M>(this, item);
          evt.setChecked(checked);
    
    
          if (fireEvent(Events.BeforeCheckChange, evt)) {
            node.checked = checked;
    
    
            view.onCheckChange(node, checkable, checked);
    
    
            fireEvent(Events.CheckChange, evt);
    
            onCheckCascade(item, checked);
            
            //Coment out fire of CheckChanged
            //CheckChangedEvent<M> cce = new CheckChangedEvent<M>(this, (M) null);
            //fireEvent(Events.CheckChanged, cce);
    
    
            onCheckCascade(item, checked);
          }
        }
      }
    Additionally, in setChecked I moved the firing of CheckChanged to after onCheckCascade(item, checked).

    Code:
    public void setChecked(M item, boolean checked) {
    
            /* Code omitted */
            // .....
    
            onCheckCascade(item, checked);
            
            CheckChangedEvent<M> cce = new CheckChangedEvent<M>(this, (M) null);
            fireEvent(Events.CheckChanged, cce);
           }
        }
    }
    It is not the best design but works with minimum coding.

Thread Participants: 1