1 2 Previous Next 23 Replies Latest reply on Sep 22, 2008 9:40 AM by nbelaevski Go to original post
      • 15. Re: Tree component with multiple select
        ykravchenko

        Had same opinion regarding this issue, but in case of checkboxes (and actually their value binding) it will not work.

        setSelected method is invoked not immediately when you click the checkbox, it will be invoked when page data is submitted (as far as I understand).

        The simplest approach will not work, I was thinking of using javascript but I'm not good in it at all.

        • 16. Re: Tree component with multiple select
          viggo.navarsete

          Hi again,

          I'm not sure what to do since the setSelected method isn't invoked immediately. Perhaps we can get some more input from other users on the forum?

          • 17. Re: Tree component with multiple select
            ykravchenko

            Hey viggo

            I decided to dig into JavaScripts and use jQuery js library to update child and parent checkboxes depending on state of current checkbox.

            Here is the js code of updating child checkboxes using jQuery

            // Put event for the document and perform operations when document is ready
            // Similar to window.onload = function(){ ... }
            jQuery(document).ready(function() {
            
             // Add click event listener to each checkbox in the tree page
             // Note! Using this simple selector assumes that there are no other
             // checkboxes on the page, if there are other checkboxes then
             // selector should be changed
             jQuery(":checkbox").click(function(){
             updateChildren(this);
             });
            });
            
            
            // Update checkboxes that are placed in child nodes of tree regarding to current checkbox
            function updateChildren(currentCheckbox) {
             // Get state of current checkbox (true or false)
             var state = currentCheckbox.checked;
            
             // Get parent TABLE, where current checkbox is places
             var parentTables = jQuery(currentCheckbox).parents("table");
             var parentTable = parentTables[0];
            
             // Get DIV where child nodes with checkboxes are situated
             // See http://docs.jquery.com/Traversing/ to get better uderstanding of
             // parents() and next()
             var childDivs = jQuery(parentTable).next("div");
             var childDiv = childDivs[0];
            
             // Iterate over all child nodes checkboxes and set same state as the
             // current checkbox state
             jQuery(childDiv).contents().find(":checkbox").each(function() {
             this.checked = state;
             });
            }
            


            Right now I'll be working on updating parent checkboxes.

            P.S: there is also issue on JIRA regarding this topic http://jira.jboss.com/jira/browse/RF-1372

            • 18. Re: Tree component with multiple select
              nayanj

               


              I'd like to be able to select all child nodes (set checkboxes to true) if my parent checkbox is checked.


              I have been able to invoke selection method on bean immediately when checkbox is checked or unchecked. This is done by wrapping a:support element with the selectBooleanCheckbox as shown below:

              <h:selectBooleanCheckbox id="categoryCheckbox" value="#{item.selected}" styleClass="checkbox">
               <a:support event="onclick" actionListener="#{item.setSelected}" RequestDelay="1"
               immediate="true" reRender="treeDiv"/>
               </h:selectBooleanCheckbox>
              


              Thanks to your help (http://www.jboss.com/index.html?module=bb&op=viewtopic&t=123717) I have resolved most of the issues mentioned in the JIRA except for alignment issue between checkbox and node label.
              http://www.jboss.com/index.html?module=bb&op=viewtopic&t=123717&postdays=0&postorder=asc&start=0

              My observation was that setSelected also gets called when tree is collapsed or expanded, you probably do not want to use default setSelected() method for setting variable "selected" in your bean. I use tree with only two levels, so propagation of check/uncheck values is simpler for me. Based on your requirements, you may want something more sophisticated. Here is how I did it:

              
               public void setSelected(boolean selected) {
               // Intentionally empty - nothing to do
               // UI invokes this method on tree collapse and expand events
               }
              
               public void setSelection(boolean selected) {
               this.selected = selected;
               // selection propagation logic
               // uncheck parent (Category) if this item was unchecked
               if(selected==false && Category.isSelected()){
               Category.setSelection(false,false);
               }
               }
              
               public void setSelection(boolean selected, boolean propagate) {
               this.selected = selected;
               // propagation logic
               // select or unselect all nodes in subtree when selection of this
               // node is modified
               if (propagate) {
               Collection sources = dataItems.values();
               for (Object object : sources) {
               Item source = (Item) object;
               source.setSelection(selected);
               }
               }
               }
              
               // invoked by ajax call
               public void setSelected(ActionEvent event) {
               Object source = event.getSource();
               if (source instanceof HtmlAjaxSupport) {
               HtmlAjaxSupport support = (HtmlAjaxSupport) source;
               HtmlSelectBooleanCheckbox checkbox = (HtmlSelectBooleanCheckbox) support
               .getParent();
               setSelection(new Boolean((String)checkbox.getSubmittedValue()));
               } else { //should never happen
               System.err.println("Unknown event from "
               + source.getClass().getCanonicalName());
               }
               }
              


              Above is mix of code snippets to give a rough idea. Let me know if you still have problems with selection issues.


              • 19. Re: Tree component with multiple select
                ykravchenko

                Hi nayanj

                Thank You for support and help.

                Actually this my second try to implement tree with multiple selection.

                The first try was based on a4j:support, so after each click of tree node the AJAX request was sent to server. This solution worked great until I had large tree (>80 nodes), and as I had to reRender the whole tree after each AJAX response it took up to 4sec. for selection to finish.

                Try making your tree large also and see if You have performance issues.

                My second try is based on client side processing of tree via JavaScript. I'm not a JavaScript guru, but using jQuery js library made this task very simple. Right now I've finished parent nodes update functionality. See code below:

                function updateParent(currentCheckbox) {
                 var parentDivs = jQuery(currentCheckbox).parents("div");
                
                 parentDivs.each(function() {
                 var parentDiv = this;
                
                 var hasSelected = false;
                 var hasUnselected = false;
                
                 jQuery(parentDiv).contents().find(checkboxSelector).each(function() {
                 if(this.checked) {
                 hasSelected = true;
                 }
                 });
                
                 var parentTables = jQuery(parentDiv).prev("table");
                 var parentTable = parentTables[0];
                
                 var parentCheckboxes = jQuery(parentTable).find(":checkbox");
                 var parentCheckbox = parentCheckboxes[0];
                
                 parentCheckbox.checked = hasSelected;
                 });
                }
                



                I also wanted checkbox to be gray (with slightly gray background) if not all child nodes are selected. See example here http://www.zapatec.com/website/main/products/prod3/demo.jsp#checkboxes.html

                Can't figure out is there other way then using images instead of 'input type='checkbox'' to make gray checkboxes

                • 20. Re: Tree component with multiple select
                  nayanj

                  Hi ykravchenko,

                  I dont have much expertise in JavaScript. I will probably be able to get to testing my app for large tree data in few days and post if I see any issues. (btw, I dont expect my tree to have more than 20 nodes)

                  coming to your question:

                  Can't figure out is there other way then using images instead of 'input type='checkbox'' to make gray checkboxes.


                  I searched around a bit and discovered that you can apply CSS style to get a gray checkbox. See example # 4 on following page:
                  (CSS styles for Checkbox)
                  http://www.456bereastreet.com/lab/form_controls/checkboxes/

                  Since you need to change CSS style dynamically, see if the following is any help:
                  (Manipulate CSS with JavaScript)
                  http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript




                  • 21. Re: Tree component with multiple select
                    ykravchenko

                    Hi nayanj,

                    Thank you for help and support. I'll review article that you have provided.

                    Please try your solution with large tree, it's very interesting to me, maybe your AJAX request/response are smaller then mine and your tree will be faster.

                    Waiting forward any news

                    • 22. Re: Tree component with multiple select
                      philipxgerard

                      Hi,

                      I find this discussion about tree components very interesting. With regards to RF-1372, I am hoping to find out if there is any support for creating multiple tree select components with checkboxes in version 3.2.2?. Are there any examples that I could start looking at?

                      Thanks for your help.

                      • 23. Re: Tree component with multiple select
                        nbelaevski

                        Hello,

                        There is nothing in 3.2.2 for this feature. Please vote for it in Wiki if you like it.

                        1 2 Previous Next