jsTree树形结构

        $("#using_json").jstree({
                "types": {
                    "#": {
                        "max_depth": 3 //最大深度
                    },
                    "default": {
                        "icon": "fa fa-bank" //默认图标
                    }
                },
                "core": {
                    "data": function(obj, callback) {
                        var jsonstr = "[]";
                        var jsonarray = eval('(' + jsonstr + ')');
                        $.ajax({
                            type: "POST",
                            url: "/department/getAllDepartment",
                            dataType: "json",
                            async: false,
                            success: function(data) {
                                if(data.code==1){
                                    var arrays = data.result;
                                    for (var i = 0; i < arrays.length; i++) {
                                        var arr = {
                                            "id": arrays[i].id,
                                            "parent": arrays[i].pid == "0" ? "#" : arrays[i].pid,
                                            "text": arrays[i].name,
                                            "state": { "opened": true}
                                        }
                                        jsonarray.push(arr);
                                    }
                                }
                            }
                        });
                        callback.call(this, jsonarray);
                    },
                    "check_callback": true
            },
            "state" : { "key" : "department" },
            "plugins": ["contextmenu", "sort", "types", "state","unique"],
            "contextmenu": { //重写菜单
                "items": {
                    "create": {
                        "label": "创建",
                        "action": function (data) {
                            var inst = $.jstree.reference(data.reference),
                                obj = inst.get_node(data.reference);
                            inst.create_node(obj,"请输入名称", "last", function(new_node) {
                                setTimeout(function() {
                                    inst.edit(new_node)
                                }, 0)
                            })}
                    },
                    "rename":{
                        "label": "重命名",
                        "action": function (data) {
                            var inst = $.jstree.reference(data.reference),
                                obj = inst.get_node(data.reference);
                            inst.edit(obj)
                        }
                    },
                    "delete": {
                        "label": "删除",
                        "action": function (data) {
                            swal({
                                title: "您确定要删除吗",
                                text: "删除后将无法恢复,请谨慎操作!",
                                type: "warning",
                                showCancelButton: true,
                                confirmButtonColor: "#DD6B55",
                                confirmButtonText: "删除",
                                closeOnConfirm: false
                            }, function () {
                                var inst = $.jstree.reference(data.reference),
                                    obj = inst.get_node(data.reference);
                                $.ajax({
                                    type: "POST",
                                    url: "/department/deleteDepartment",
                                    dataType: "json",
                                    data:{
                                        id:obj.id
                                    },
                                    success: function(data) {
                                        if(data.code==1){
                                            if (inst.is_selected(obj)) {
                                                inst.delete_node(inst.get_selected())
                                            } else { inst.delete_node(obj) }
                                            swal("删除成功!", "您已经永久删除了这条信息。", "success");
                                        }else{
                                            toastr.warning(data.message)
                                        }
                                    }
                                })
                            });
                        }
                    }
                }
            }
        }).bind(//绑定事件
            "create_node.jstree",
            function(e, data) {
                $.ajax({
                    type: "POST",
                    url: "/department/createDepartment",
                    dataType: "json",
                    data:{
                        name:data.node.text,
                        pid:data.node.parent,
                    },
                    success: function(res) {
                        if(res.code==1){
                            //将数据库中插入的id返回并且重新赋值
                            data.instance.set_id(data.node, res.result);
                        }else{
                            data.instance.refresh();
                            toastr.warning(res.message)
                        }
                    }
                })
        }).bind(
        "rename_node.jstree",
        function(e, data) {
            $.ajax({
                type: "POST",
                url: "/department/renameDepartment",
                dataType: "json",
                data:{
                    name:data.node.text,
                    id:data.node.id
                },
                success: function(res) {
                    if(res.code!=1){
                        data.instance.refresh();
                        toastr.warning(res.message)
                    }
                }
            })
        })

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿S先森丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值