Topic: Combining setChecked with json loading

I have been trying to understand/fix this now for three days mad

I have a page rather like the demo 'Tree simple checkboxes' so that as each node is opened up an AJAX call is made to retrieve the next section of the tree.

My understanding is that if I call setChecked, which recursively navigates the tree looking for the node to check, it will not be able to check a node that has not yet been loaded. (Seems reasonable).

So if when I display a page where there is a checkbox tree with some boxes I want to check (even if they are not yet opened up) I have to load the whole tree? (Or at least those portions of the tree that need to be checked).

So, what I want is code that allows me to preload the whole tree, then I can make a call to setChecked.

However, it seems that I need to make a call to tree.loadOptions and this over-writes any checked values, and I can't get my page to open nodes if I leave it out!

        function setupTree(data,tContainer,tFormField,tTreeData,tAjaxUri) {
            var tree = new Mif.Tree({
                container:      $(tContainer),
                forest:         true,                               // tree does not have a root node
                initialize:     function() {
                    $(tFormField).dispose();
                    this.initCheckbox('partial', 'id', tFormField);
                },
                types: {
                    folder: {
                        openIcon:       'mif-tree-open-icon',
                        closeIcon:      'mif-tree-close-icon'
                    },
                    loader: {
                        openIcon:       'mif-tree-loader-open-icon',
                        closeIcon:      'mif-tree-loader-close-icon',
                        DDnotAllowed:   ['inside','after']
                    }
                },
                dfltType:       'folder',
                height:         18
            });

            tree.load({json: data[tTreeData]});

            tree.loadOptions=function(node){
                var options = {};
                if (node.id) {
                    options.url = tAjaxUri+node.id;
                }
                return options;
            }

            if (data && data.form_values && data.form_values[tFormField])
            {
                tree.setChecked(data.form_values[tFormField]);
            }
            tree.openLoaded();
            return tree;
        }

Re: Combining setChecked with json loading

Try check nodes using load and loadChildren events and remove already checked nodes from array of nodes which should be checked:

tree.checked_nodes=some_nodes
tree.addEvent('load', function(){
     this.setChecked();//this function set checked nodes from tree.checked_nodes array and remove already checked nodes from this array.
});
tree.addEvent('loadChildren', function(){
    this.setChecked();
});

Re: Combining setChecked with json loading

moro
Thanks for your prompt reply. I *think* I might have understood it but it seemed too complicated to implement (if I understood it correctly)

Anyway, after a major re-work of my code I have decided to change the back-end code so that the whole of the tree is pre-loaded and there are now no longer any AJAX calls to get the children nodes. The pre-loaded JSON has the 'checked' values for those checkboxes I want to be checked.

I now almost have a working system but something is different in terms of the way checking boxes works, but I will cover that in a separate thread so as not to change the subject of this thread. smile

Last edited by icydee (2009-04-11 20:04:25)

Re: Combining setChecked with json loading

I *think* I might have understood it but it seemed too complicated to implement

similar functionality implemented in new CookieStorage