使用json对象的Dynatree树视图jquery插件问题

时间:2015-01-23 15:30:13

标签: jquery treeview dynatree

js文件中,dynatree在节点中显示 null ,因为它在php个文件中正常工作。

        var jsoncat =  jsoncat.replace(/\%/g,'"').trim();
        var jsoncat =  '{"data":['+JSON.stringify(jsoncat)+']}';
        var jsonObj =  JSON.parse(jsoncat);

        var delay=1000;//1 seconds
        alert(typeof(jsonObj));
        alert(jsonObj.data);    

        setTimeout(function(){
            var treeData = [jsonObj.data];
                // --- Initialize sample trees
                jQuery('#tree1').dynatree({
                    checkbox: true,`enter code here`

                    // Override class name for checkbox icon:
                    classNames: {checkbox: "dynatree-radio"},
                    selectMode: 3,
                    children: treeData,
                    onActivate: function(node) {
                        jQuery('#echoActive1').text(node.data.key);
                    },

                    onSelect: function(select, node) {
                        // Display list of selected nodes
                        jQuery("#categoryid").val(node.data.key);
                    },

                    onDblClick: function(node, event) {
                        node.toggleSelect();
                    },

                    onKeydown: function(node, event) {
                        if( event.which == 32 ) {
                            node.toggleSelect();
                            return false;
                        }
                    },

                }); 
            },delay);

1 个答案:

答案 0 :(得分:0)

我的JSON没有正确格式化,一旦我获得正确的JSON,我的问题就解决了。这是最新的代码。

 str_trim = jsoncat.replace(/'/g,'"').trim();
            str = '{"data":['+str_trim+']}'
            var jsonObj =  JSON.parse(str);
            cWindowShow('jax.call("community","admin,groups,ajaxEditGroup", ' + groupId + ');', 'Editing Group' , 550 , 450);
            var delay=1000;//1 seconds
            setTimeout(function(){
                var treeData = jsonObj.data;
                    // --- Initialize sample trees
                    jQuery('#tree1').dynatree({
                        checkbox: true,
                        // Override class name for checkbox icon:
                        classNames: {checkbox: "dynatree-radio"},
                        selectMode: 1,
                        children: treeData,
                        onActivate: function(node) {
                            jQuery('#echoActive1').text(node.data.key);
                        },
                        onSelect: function(select, node) {
                            // Display list of selected nodes
                            jQuery("#categoryid").val(node.data.key);
                        },
                        onDblClick: function(node, event) {
                            node.toggleSelect();
                        },
                        onKeydown: function(node, event) {
                            if( event.which == 32 ) {
                                node.toggleSelect();
                                return false;
                            }
                        },

                    }); 
                },delay);