如何正确加载dynatree以选择更多或取消选择节点

时间:2012-12-03 21:07:16

标签: php jquery json edit dynatree

如何生成具有所选值的树?我所做的是存储选定的键,然后在编辑时,我使用带有所选键的额外json数组生成树。但问题是如果我通过mysql改变图像的顺序,键不代表真正的值。

这是我为树创建json数组的方法,深度= 1。

    /*** get image tree structure ***/
    $i = 0; $j = 0;//$j = 0;  $l = 0;
    $counter = 0;
    $whole_tree = $this->cms_model->get_tree_big_array('0');
    foreach ($whole_tree as $node_key => $node_array) {
        foreach ($node_array as $key_name => $node_element_value) {

            if($key_name == 'title'):
                $new_array[$i][$key_name] = $node_element_value;                
            elseif($key_name == 'filename'):
                $new_array[$i][$key_name] = $node_element_value;
            elseif($key_name == 'id'):
                $new_array[$i][$key_name] = $node_element_value;
            elseif($key_name == 'is_folder'):
                $new_key_name = 'isFolder';

                if($node_element_value == '1'):
                    $new_node_element_value = 'true';
                    $new_array[$i][$new_key_name] = $new_node_element_value;

                    $cur_id = $node_array['id'];
                    $children = $this->cms_model->get_children_array($cur_id);

                    foreach ($children as $child_key_name => $child_element_value) {
                        foreach ($child_element_value as $super_child_key_name => $childnode_element_value) {

                        if($super_child_key_name == 'title'):
                            $new_array[$i]['children'][$child_key_name][$super_child_key_name] = $childnode_element_value;
                        elseif($super_child_key_name == 'filename'):
                            $new_array[$i]['children'][$child_key_name][$super_child_key_name] = $childnode_element_value;
                        elseif($super_child_key_name == 'id'):
                            $new_array[$i]['children'][$child_key_name][$super_child_key_name] = $childnode_element_value;
                        endif;
                        }
                        $counter++;
                    }
                else:
                    $new_node_element_value = 'false';
                endif;



                $cur_id = $node_array['id'];
                $children = $this->cms_model->get_children_array($cur_id);

            endif;
            $j++;
        }
        $i++;
    }
    //print_r($new_array);
    $json_tree = json_encode($new_array);
    $data['json'] = $json_tree; 

以上是dynatree脚本,我在其中为页面选择一些照片,并对所选内容和激活内容进行预览。在这种情况下,我有照片。

jQuery(function(){
jQuery("#tree").dynatree({
    checkbox: true,

    onPostInit: function(isReloading, isError){
        var data = <?php echo $json;?>; // the whole json array tree object
        var array = <?php echo $key_array;?>; // these the stored keys where selected from last post.
        var imgarray = <?php echo $img_array;?>; // these are the selected images id's

        for(i=0; i<array.length; i++){
            var tree = jQuery("#tree").dynatree("getTree");
            tree.getNodeByKey(array[i]).select(); // i'm selecting on load the selected nodes using this function.
            // what i want is to select the nodes using the $img_array array in order to have
            // an independence in what order the nodes are generated
        }

    },                              

    onActivate: function(node) {
        var site_url = '<?php echo site_url(); ?>';
        var controller_path = '/admin/actions/get_image_path/';
        var image_id = node.data.id;


        jQuery.ajax({
            url: site_url+controller_path+image_id,
            type: 'GET',
            datatype: 'html',
            processData: false,
            success: function(msg){
                jQuery('.image-previewer').css({display: 'block'});
                jQuery('.image-previewer').html(msg);
            }
        });

    },
    onSelect: function(flag, node) {
        //alert(node.data.id);
        if(flag){
            jQuery('<div id="'+node.data.id+'">').css({
                    display: 'block',
                    borderRadius: '3px',
                    margin: '0 0 10px 0',
            })
            .appendTo('.list');
            jQuery('<span id="'+node.data.id+'" style="padding:0px 5px; line-height:40px; margin-right:5px; float:left; background:#fffccc;">Εχεις επιλέξει: '+node.data.filename+' </span>').appendTo('div#'+node.data.id+'');
            jQuery('<img id="img_'+node.data.id+'" src="http://pinecone.gr/files/thumbs/'+node.data.filename+'" style="float:left; margin-right:5px">').appendTo('div#'+node.data.id+'');   
            jQuery('<span>').css({
                lineHeight: '40px',
                display: 'block',
                float: 'left',
                id: 'span_'+node.data.id
            })

            jQuery('<input>',
            { 
                type: 'hidden',
                name: 'img_array[]',
                id : 'input_'+node.data.id,
                value: 'img_'+node.data.id
            }).appendTo('div#'+node.data.id+'');

            jQuery('<input>',
            { 
                type: 'hidden',
                name: 'keys_array[]',
                id : 'inputkeys_'+node.data.id,
                value: node.data.key
            }).appendTo('div#'+node.data.id+'');                                        

            jQuery('<br clear="all" />').appendTo('div#'+node.data.id+'');
            jQuery('input#input_'+node.data.id+'').attr({ value: node.data.id})

        }else if(!flag){
            jQuery('.list div#'+node.data.id+'').remove();
        }

    },
    persist: false,
    children:
    <?php echo $json; ?>

});

});

如果我更改数据库中图像记录的顺序,或者通过添加图像,当前记录的选定键不是真实的,因为结构已更改,并且我的数据完整性失败。您对解决方案有何建议? 在此先感谢

0 个答案:

没有答案