jsTree Node Expand/Collapse

前端 未结 4 578
故里飘歌
故里飘歌 2021-02-09 08:55

I ran into the excellent jstree jQuery UI plug in this morning. In a word - great! It is easy to use, easy to style & does what it says on the box. The one thing I have n

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-09 09:18

    Yet another example for jstree 3.3.2. It uses underscore lib, feel free to adapt solution to jquery or vanillla js.

    $(function () {
        var tree = $('#tree');
        tree.on('before_open.jstree', function (e, data) {
            var remained_ids = _.union(data.node.id, data.node.parents);
            var $tree = $(this);
            _.each(
                    $tree
                        .jstree()
                        .get_json($tree, {flat: true}),
                    function (n) {
                        if (
                            n.state.opened &&
                            _.indexOf(remained_ids, n.id) == -1
                        ) {
                            grid.jstree('close_node', n.id);
                        }
                    }
            );
        });
        tree.jstree();
    });
    

提交回复
热议问题