zTree的使用:

<ul id="selectTree" class="ztree" style="max-height:600px;overflow:auto;padding-right: 30px;border-right: 1px solid #ccc;"></ul>

var setting = {
        view: {
            // addHoverDom: addHoverDom,
            // removeHoverDom: removeHoverDom,
            selectedMulti: true
        },
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }
        },
        data: {
            key:{
                name:"deptname",
                isParent:"parentis"
            },
            simpleData: {
                idKey:"deptid",
                pIdKey:"parentdeptid",
                enable: true
            }
        },
        edit: {
            enable: false
        },
        callback: {
            onCheck: zTreeOnCheck,
            // beforeCheck: zTreeBeforeCheck
        }
    };




 var zNodes =[];

    $.ajax({
        url:_CONTEXT + "/protect.target/getAllDept",
        data:{},
        dataType:"json",
        type:"get",
        success:function (resultData) {
            zNodes = resultData
            $min.fn.zTree.init($("#selectTree"), setting, zNodes);
        }
    })




  function fuzzySearch(zTreeId, searchField, isHighLight, isExpand){
        var zTreeObj = $min.fn.zTree.getZTreeObj(zTreeId);
        if(!zTreeObj) return;
        var nameKey = zTreeObj.setting.data.key.name; //get the key of the node name
        isHighLight = isHighLight?true:false;//default true, only use false to disable highlight
        isExpand = isExpand?true:false; // not to expand in default
        zTreeObj.setting.view.nameIsHTML = isHighLight; //allow use html in node name for highlight use

        var metaChar = '[\\[\\]\\\\\^\\$\\.\\|\\?\\*\\+\\(\\)]'; //js meta characters
        var rexMeta = new RegExp(metaChar, 'gi');//regular expression to match meta characters

        // keywords filter function
        function ztreeFilter(zTreeObj,_keywords,callBackFunc) {
            if(!_keywords){
                _keywords =''; //default blank for _keywords
            }
            // function to find the matching node
            function filterFunc(node) {
                if(node && node.oldname && node.oldname.length>0){
                    node[nameKey] = node.oldname; //recover oldname of the node if exist
                }
                zTreeObj.updateNode(node); //update node to for modifications take effect
                if (_keywords.length == 0) {
                    //return true to show all nodes if the keyword is blank
                    zTreeObj.showNode(node);
                    zTreeObj.expandNode(node,isExpand);
                    return true;
                }
                //transform node name and keywords to lowercase
                if (node[nameKey] && node[nameKey].toLowerCase().indexOf(_keywords.toLowerCase())!=-1) {
                    if(isHighLight) {
                        var newKeywords = _keywords.replace(rexMeta, function (matchStr) {
                            return '\\' + matchStr;
                        });
                        node.oldname = node[nameKey]; //store the old name
                        var rexGlobal = new RegExp(newKeywords, 'gi');//'g' for global,'i' for ignore case
                        //use replace(RegExp,replacement) since replace(/substr/g,replacement) cannot be used here
                        node[nameKey] = node.oldname.replace(rexGlobal, function (originalText) {
                            //highlight the matching words in node name
                            var highLightText =
                                '<span style="color: whitesmoke;background-color: #18BC9C;">'
                                + originalText
                                + '</span>';
                            return highLightText;
                        });
                        zTreeObj.updateNode(node);
                    }
                    zTreeObj.showNode(node);//show node with matching keywords
                    zTreeObj.expandNode(node,isExpand);
                    return true; //return true and show this node
                }
                // zTreeObj.hideNode(node); // hide node that not matched
                return false; //return false for node not matched
            }

            var nodesShow = zTreeObj.getNodesByFilter(filterFunc); //get all nodes that would be shown
            processShowNodes(nodesShow, _keywords);//nodes should be reprocessed to show correctly
        }

        /**
         * reprocess of nodes before showing
         */
        function processShowNodes(nodesShow,_keywords){
            if(nodesShow && nodesShow.length>0) {
                //process the ancient nodes if _keywords is not blank
                if(_keywords.length>0){
                    $min.each(nodesShow, function(n,obj){
                        var pathOfOne = obj.getPath();//get all the ancient nodes including current node
                        if(pathOfOne && pathOfOne.length>0){
                            //i < pathOfOne.length-1 process every node in path except self
                            for(var i=0;i<pathOfOne.length-1;i++){
                                zTreeObj.showNode(pathOfOne[i]); //show node
                                zTreeObj.expandNode(pathOfOne[i],true); //expand node
                            }
                        }
                    });
                } else { //show all nodes when _keywords is blank and expand the root nodes
                    var rootNodes = zTreeObj.getNodesByParam('level','0');//get all root nodes
                    $.each(rootNodes,function(n,obj){
                        zTreeObj.expandNode(obj,false); //expand all root nodes
                    });
                }
                // zTreeObj.checkAllNodes(false)
            }
        }

        //listen to change in input element
        $(searchField).bind('input propertychange', function() {
            var _keywords = $(this).val();
            searchNodeLazy(_keywords); //call lazy load
        });

        var timeoutId = null;
        var lastKeyword = '';
        // excute lazy load once after input change, the last pending task will be cancled
        function searchNodeLazy(_keywords) {
            if (timeoutId) {
                //clear pending task
                clearTimeout(timeoutId);
            }
            timeoutId = setTimeout(function() {
                if (lastKeyword === _keywords) {
                    return;
                }
                zTreeObj.expandAll(false);
                ztreeFilter(zTreeObj,_keywords); //lazy load ztreeFilter function
                // $(searchField).focus();//focus input field again after filtering
                lastKeyword = _keywords;
            }, 700);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值