easyui tree treegrid 전단 검색 실현

20624 단어 easyuijs
function search() {
        var searchCount = {key: '', step: 0};
        var setKey = function (key) {
            searchCount.key = key;
        };
        var addStep = function (step) {
            searchCount.step = step;
        };

        clearSearchWords = function () {
            setKey('');
            addStep(0);
        };
        return function (keywords) {
            if (searchCount.key != keywords) {
                setKey(!keywords ? '' : keywords);
                addStep(0);
            }
            function getSearchData(){
                var roots = $('#tt').tree('getRoots');
                var a = [];
                function find(d){
                    for (var i = 0; i < d.length; i++) {
                        if (d[i].text.indexOf(keywords)!=-1) {
                            a.push(d[i]);
                        }
                        find($('#tt').tree('getChildren',d[i].target) || []);
                    }
                }
                find(roots);
                return a;
            }
            var nodes = getSearchData();
            if (nodes.length > 0) {
                if (searchCount.step + 1 > nodes.length) {
                    addStep(0);
                }
                $('#tt').tree('select',nodes[searchCount.step].target);
                addStep(searchCount.step + 1);
            }
        };
    }

    var searchNodes = search();

treegrid 코드
function search() {
        var searchCount = {key: '', step: 0};
        var setKey = function (key) {
            searchCount.key = key;
        };
        var addStep = function (step) {
            searchCount.step = step;
        };

        clearSearchWords = function () {
            setKey('');
            addStep(0);
        };
        return function (keywords) {
            if (searchCount.key != keywords) {
                setKey(!keywords ? '' : keywords);
                addStep(0);
            }
            function getSearchData(){
                var roots = $('#tt').treegrid('getRoots');
                var a = [];
                function find(d){
                    for (var i = 0; i < d.length; i++) {
                        if (d[i].text.indexOf(keywords)!=-1) {
                            a.push(d[i]);
                        }
                        find($('#tt').treegrid('getChildren',d[i].id) || {});
                    }
                }
                find(roots);
                return a;
            }
            var nodes = getSearchData();
            if (nodes.length > 0) {
                if (searchCount.step + 1 > nodes.length) {
                    addStep(0);
                }
                $('#tt').treegrid('select',nodes[searchCount.step].id);
                addStep(searchCount.step + 1);
            }
        };
    }
	var searchNodes = search();

좋은 웹페이지 즐겨찾기