jstree 獲取選中節點的所有子子點


   //加載功能樹
                    function initTree() {
                        $.jstree.destroy();
                        $.ajax({
                            type: "Get",
                            url: "/Department/GetTreeData/0?_t=" + new Date().getTime(),    //獲取數據的ajax請求地址
                            success: function (data) {
                                $('#dptTree').jstree({       //創建JsTtree
                                    'core': {
                                        'data': data,        //綁定JsTree數據
                                        "multiple": false    //是否多選
                                    },
                                    "plugins": ["wholerow"]  //配置信息
                                })
                                $("#dptTree").on("after_open.jstree", function (event, data) {

                                })
                                $("#dptTree").on("ready.jstree", function (e, data) {   //樹創建完成事件
                                    //data.instance.open_all();    //展開所有節點
                                });
                                $("#dptTree").on('changed.jstree', function (e, data) {   //選中節點改變事件
                                    //獲取所有選中項目及子項目
                                    function getChildNodes(treeNode, result) {
                                        var childrenNodes = data.instance.get_children_dom(treeNode);
                                        if (childrenNodes) {
                                            for (var i = 0; i < childrenNodes.length; i++) {
                                                var row = childrenNodes[i];
                                                if ($.inArray(row.id, result) == -1) {
                                                    result.push(row.id);
                                                }
                                                result = getChildNodes(row.id, result);
                                            }
                                        }
                                        return result;
                                    }
                                    var result = [];
                                    //獲取選中的節點
                                    var node = data.instance.get_node(data.selected[0]);
                                    result.push(node.id);
                                    //遍歷選中節點的子節點
                                    var childNodes = data.instance.get_children_dom(node);
                                    for (var i = 0; i < childNodes.length; i++) {
                                        var row = childNodes[i];
                                        if ($.inArray(row.Id, result) == -1) {
                                            result.push(row.id);
                                        }
                                        getChildNodes(row, result);
                                    }
                                    if (node && firstLoaded != 1) {
                                        dptIds = result; //保存選中的節點ID
                                        dptParentId = node.parent; //保存選中的節點父ID
                                        app.loaddata(1);
                                    };
                                });

                                $("#dptTree").on('loaded.jstree', function (e, data) {
                                    //當tree加載完畢時,獲取樹的根節點對象;
                                    //調用select_node方法,選擇根節點。
                                    firstLoaded = 1;
                                    var inst = data.instance;
                                    var obj = inst.get_node(e.target.firstChild.firstChild.lastChild);
                                    inst.select_node(obj);
                                    firstLoaded = 2;
                                });
                            }
                        });
                    }

 更多精彩文章請關注我們的微信公眾號FocusDotCore:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM