美文网首页
jsTree AJAX异步加载,重载,刷新

jsTree AJAX异步加载,重载,刷新

作者: 朱冬杰 | 来源:发表于2020-03-12 15:34 被阅读0次

折腾了一段时间,网上查了不少资料,总算实现了我要的效果

jstree 版本:3.0.4

html代码如下:

<div id="tree" style="margin-top:10px;font-size:13px;margin-left: 130px;"></div>

Script 脚本代码:

jQuery(function ($) {

    function InitTree() {

            var url = "xxxx?"+ "&_=" + Math.random();

            $('#tree').jstree({

                'core': {

                    "multiple": true,

                    "animation": 0,

                    'data': {

                        url: url,

                        dataFilter: function (data) {

                            return JSON.parse(data);

                        }

                    },

                    "themes": {

                        "icons": true/*图标显示开关*/

                    }

                    ,"dblclick_toggle": false,

                    "worker": false

                }

                ,"types": {

                    "default": {

                        "icon": "fa fa-user"/*默认图标,也可以后台传icon属性放入map中,亲测有效*/

                    }

                }

                ,'plugins': ['types', 'checkbox', 'state']

                ,"checkbox": {

                    "tie_selection": true,

                    "keep_selected_style": false,

                }

            }).on('select_node.jstree', function (e, data) {

                    debugger /*选中之后触发*/

            }).on('deselect_node.jstree', function (e, data) {

                debugger /*取消选中触发*/

            }).on("loaded.jstree", function (event, data) {

                /*这两句化是在loaded所有的树节点后,然后做的选中操作,这点是需要注意的,

                loaded.jstree 这个函数取消选中,然后选中某一个节点*/

                $('#tree').jstree().open_all();/*打开树,不打开下面的选中语句执行有问题*/

                //var TempIds = $("#ids").val();

                //var TempIdsArr = TempIds.split(",");

                data.instance.clear_state();/*此句用来清除之前选中的数据不可以去掉*/

                //$.each(TempIdsArr, function (index, value) {

                //    var id = value + '_anchor';

                //    $('#tree').jstree('select_node', id, true, true);/*选中id对应的节点*/

                //});

            }).on("load_node.jstree", function (event, data) {

                debugger /*加载node时候触发*/

            });

        }

    InitTree();

    $(document).on("change", 'select#txtCustomer', function () {

            $('#tree').jstree(true).destroy();// 清除树节点

            InitTree();

            //$('#tree').jstree(true).refresh();//true:表示获得一个已经存在的jstree实例

      });

  });

后台返回的Tree的数据JSON格式,示例数据:

[{

    "id": "4dce003b-1e47-4dd9-bb23-990359b01f00",

    "text": "基础节点1",

    "state": {"opened": true },

    "children": [{

        "id": "2f7938be-23a4-487f-ab27-a04afd40da4f",

        "text": "二级节点1_1",

        "children": [{

            "id": "9f7e1751-9e05-4f53-a5dd-2c241889a2bd",

            "text": "三级节点1_1_1",

            "state": {

                "selected": false,

                "opened": true

                }

            },

            {

                "id": "f99881a0-d092-4a05-85c9-f89b0d7ff562",

                "text": "三级节点1_2_2",

                "state": {

                    "selected": false

            }

    }

]

}]

},

{

"id": "f3782cb4-a23f-481a-9466-a3aab7e77afb",

"text": "基础节点2",

"state": {

"opened": true

},

"children": [{

"id": "0484cf6f-8eca-4aa6-bb2a-0ef030e62f9b",

"text": "二级节点2_1",

"state": {

"opened": true

},

"children": [{

"id": "33d7603a-a881-44b6-9516-61c7d70f66a4",

"text": "三级节点2_1_1",

"state": {

"selected": false

}

},

{

"id": "1e87075b-1102-4456-8492-f4a061fec01d",

"text": "三级节点2_1_2",

"state": {

"selected": false

}

},

{

"id": "440c54b4-bd49-4337-90c9-7e52818a2b62",

"text": "三级节点2_1_3",

"state": {

"selected": false

}

}

]

},

{

"id": "b7f683f7-0940-4a3f-a051-f3e1e0da16d5",

"text": "二级节点2_2",

"state": {

"opened": true

},

"children": [{

"id": "19931955-4450-491b-9814-45a3f4159482",

"text": "三级节点2_2_1",

"state": {

"selected": false

}

},

{

"id": "40cb83c4-81d4-403f-bd75-61c92ebd3a8e",

"text": "三级节点2_2_2",

"state": {

"selected": false

}

}

]

}

]

},

{

"id": "83aa2b5b-5126-4658-878d-1faec1bf5b30",

"text": "基础节点3",

"state": {

"opened": true

},

"children": [{

"id": "69f5a26d-053f-427b-b75c-1e6e0e033c9e",

"text": "二级节点3_1",

"state": {

"opened": true

},

"children": [{

"id": "b15ee80c-8632-4505-9a02-dc369e6bae76",

"text": "三级节点3_1_1",

"state": {

"selected": false

}

},

{

"id": "2cbd4ddf-2ad4-4cdb-878f-cf21abeab7f3",

"text": "三级节点3_1_2",

"state": {

"selected": false

}

},

{

"id": "81344b72-3c0d-46a2-b5ce-1978ef05334c",

"text": "三级节点3_1_3",

"state": {

"selected": false

}

}

]

}]

}

]

参考文章

1、https://blog.csdn.net/yipanbo/article/details/51495830

2、https://www.cnblogs.com/huiy/p/12017207.html

相关文章

  • jsTree AJAX异步加载,重载,刷新

    折腾了一段时间,网上查了不少资料,总算实现了我要的效果 jstree 版本:3.0.4 html代码如下: Scr...

  • AJAX

    同步和异步 异步的javascript和xml就是AJAX,不用全部重新加载,可以实现异步请求,局部刷新的功能。 ...

  • ajax

    什么是ajax? ajax(异步javascript xml) 能够刷新局部网页数据而不是重新加载整个网页。 如何...

  • day12-升级登录案例

    升级登录案例(ajax) ajax:异步的javascript在不重新加载页面的情况下,对页面进行局部刷新。 1、...

  • JavaScript Ajax 和 JSON(八)

    Ajax: 允许加载数据而无需刷新整个页面 Ajax 采用异步( 或称非阻塞 )处理模型, 所以浏览器在等待数据加...

  • JS-Ajax

    全称 Ansync JavaScript and XML,是一门异步的加载技术,局部刷新,Ajax的使用分为原生和...

  • JavaScript DOM 10.9

    Ajax ajax用于概括异步加载页面内容的技术。即使用户看到的只是页面中的一小部分有变化也要刷新和重新加载整个页...

  • Ajax

    Ajax简介 通过AJAX (Asynchronous JAvaScript and XML) 实现异步刷新 用于...

  • Ajax

    1、Ajax最大的作用(特点) 异步传输、局部刷新。 AJAX(Asynchronous Javascript A...

  • ajax回顾

    ajax优势一次http请求对应一个页面ajax,局部刷新页面,客户端(浏览器)就能与服务端进行通信异步加载数据,...

网友评论

      本文标题:jsTree AJAX异步加载,重载,刷新

      本文链接:https://www.haomeiwen.com/subject/gcbujhtx.html