jquery动态树按节点id搜索

时间:2013-07-01 07:24:52

标签: javascript jquery

任何人都可以帮我编写搜索jQuery dynatree中的节点的代码。如果我以逗号(,)分隔输入多个节点ID,我想将树扩展到该节点。目前我正在显示默认树。

以下显示树代码。

    function showhpTreeView(levelnumber,debugDelay)//this function will e used to show the tree view
    {
        createhpTreeDialog();
        var treeData=new Array();

        $(".showtreeview").dynatree({
            //persist: true,
            activeVisible: true,
            checkbox: true,
            selectMode: 1,
            classNames: {checkbox: "dynatree-radio"},
            fx: { height: "toggle", duration: 200 },
            /* onPostInit: function(isReloading, isError) {
                this.reactivate();
            }, */
          // initAjax is hard to fake, so we pass the children as object array:
            initAjax: {url: "../common/treeInfo",
                dataType: "json", // Enable JSONP, so this sample can be run from the local file system against a localhost server
                timeout: 10000, // timeout, otherwise 'connection refused' is not recognized if server is not running
                data: "levelnumber="+levelnumber+"&nutzer_id="+$("#kamnutzer_id").val()+"&search=true",
                //addExpandedKeyList: true 
                // Send list of expanded keys, so the webservice can deliver these children also
                success:function(node){
                 var nodeKey=(node.data.key).split("_");
                 nodeKey=nodeKey[(nodeKey.length)-1];//this is the SAPID of the current node.
                 if(($("#kamsapIdsForUser").val()).indexOf(nodeKey)!=-1)
                 {
                     node.select(true);
                 }
                var childrens=node.getChildren();
                node.visit(function(subnode){
                    if(node.data.hideCheckbox)
                    {
                        subnode.data.hideCheckbox=true;
                        subnode.render();
                    }
                    if(subnode.data.key)
                    {
                        var nodeKey=(subnode.data.key).split("_");
                        nodeKey=nodeKey[(nodeKey.length)-1];//this is the SAPID of the current node.
                        if(($("#kamsapIdsForUser").val()).indexOf(nodeKey)!=-1)
                        {
                            subnode.select(true);
                        }
                    }
                });
                alert(node.data.key);
             }

                },
            onLazyRead: function(node){
            node.appendAjax(
              {url: "../common/treeInfo",
               dataType: "json", // Enable JSONP, so this sample can be run from the local file system against a localhost server
               data: "levelnumber="+(node.data.key)+"&nutzer_id="+$("#kamnutzer_id").val(),
               success:function(node){
               //node.data.hideCheckbox=true;
               node.render();
                var nodeKey=(node.data.key).split("_");
                nodeKey=nodeKey[(nodeKey.length)-1];//this is the SAPID of the current node.

                if(node.data.hideCheckbox)
                {
                    subnode.data.hideCheckbox=true;
                    subnode.render();
                }
                if(($("#kamsapIdsForUser").val()).indexOf(nodeKey)!=-1)
                {
                    node.select(true);
                }
                var childrens=node.getChildren();
                node.visit(function(subnode){
                if(node.data.hideCheckbox)
                {
                    subnode.data.hideCheckbox=true;
                    subnode.render();
                }
                //subnode.data.hideCheckbox=true;
                    subnode.render();
                    if(subnode.data.key)
                    {
                        var nodeKey=(subnode.data.key).split("_");
                        nodeKey=nodeKey[(nodeKey.length)-1];//this is the SAPID of the current node.
                        if(($("#kamsapIdsForUser").val()).indexOf(nodeKey)!=-1)
                        {
                            node.select(true);
                        }
                    }   
                    //alert(subnode.data.title);
                });
            }
            });
            //alert('childs'+node.childList);
          }

        });
    }

以下代码我在输入节点ID时尝试显示树,但它只显示了我在搜索框中输入的一个节点

   $("#searchnode_btn").live('click',function(){

     var sapid = $("#node").val();
     $.ajax({
        url:"searchlevelbynodeid",
        type:'post',
        data : 'sapid='+sapid,
        dataType:'json',
        success:function(data){
        alert("came back");
            if(data.hasOwnProperty('message'))
            {
                if(data.message!=''){
                    //alert("success");
                    var levelnumber=data.message;
                    alert(JSON.stringify(levelnumber,0,4));
                    levelnumber=(parseInt(levelnumber[0]['sap_level'],10)-1)+"_"+sapid;
                    var debugDelay=5;
                    showhpTreeView(levelnumber,debugDelay);
                }else{
                    //alert(JSON.stringify(data,0,4));
                    showError("node not found");
                }

            }
        },  
        error:function(){
            showError(__("Some_Error_Occured"));
        }
        });//ajax call ends here



    }); 

0 个答案:

没有答案