将节点从 jstree 拖到外部 div 元素上,然后在放下时执行操作

发布于 2024-11-09 03:27:45 字数 3306 浏览 0 评论 0原文

我已经有一个 jstree 正在工作,现在希望能够将某些类型的节点拖动到外部 div 元素(包含由 highcharts 生成的图表)。我实际上并不希望从树中删除该节点,也不想要该节点的副本。我只是想要删除节点的操作来使用节点的 ID 更新图表。

我想我可以解决图表更新的问题,但这是使用 jstree 拖/放的过程,我发现有点令人困惑。另外,我只希望某些类型的节点可拖动(任何具有 rel="ds" 属性的节点)。

我对此很挣扎,但还没有走得太远。这就是我必须要做的:

$("#statstree").jstree({ 
       "json_data" : {
                "ajax" : {
                    "url" : "test.json",
                    "data" : function (n) { 
                        return { id : n.attr ? n.attr("id") : 0 }; 
                    },
                    "progressive_render" : true
                }
            },
            "types" : {
                "valid_children" : [ "root" ],
                "types" : {
                    "folder" : {
                        "icon" : { 
                            "image" : "images/folder.png" 
                        },
                        "valid_children" : [ "default" ],
                        //"max_depth" : 2,
                        "hover_node" : true,
                        "show_checkboxes" : false
                    },
                    "default" : {
                        "icon" : { 
                            "image" : "images/folder.png"
                        },
                        "valid_children" : [ "default" ]
                    },
                    "hover_node" : false
                }
            },
            "themes" : {
                "theme" : "classic",
                "dots" : false,
                "icons" : true
            },
            "core" : { "html_titles" : true },

            "dnd" : {
                "drop_target" : "#test_area",
                "drop_finish" : function (data) {
                    if(data.o.attr("rel") === "ds") {
                      //update chart with new data here?
                      //using data.o.attr("id")
                    }
                }
            },
            "crrm" : { move : { check_move : function (m) { return false; } } },

            "plugins" : ["themes","json_data","dnd","ui","types","crrm"]
        });

我在某处读到绑定“before.jstree”事件可以帮助阻止某些节点被拖动(还有 crrm 位)。但我认为我做错了。看来“start_drag”无权访问 data.args[0]:

$("#statstree").bind("before.jstree", function (e, data) {
            if(data.func === "start_drag" && data.args[0].parent("li").attr("rel") != "ds") {
                e.stopImmediatePropagation();
                return false;
            }
        });

有人知道我如何完成此任务吗?

干杯:)

编辑:我已经想出了如何阻止非“ds”节点被丢弃在该区域:

"drag_check" : function (data) {
                    if(data.r.attr("rel") != "ds") {
                        return false;
                    }
                    return { 
                        after : false, 
                        before : false, 
                        inside : true 
                    };
                }

我现在将完善我的问题:

我怎样才能如果我有多个 drop_targets,如何获取目标的 ID? "drop_target" : "#testarea1, #testarea2"

编辑 2

哦,边走边回答我的问题!我已经坐在那里盯着这个看了很多年了。一瞬间我就想到了:

data.r.attr("id")

Edit 3

现在唯一剩下的问题是,尽管所有没有 rel="ds 属性的节点" 无法“拖放到”外部 div/图表中,当将鼠标悬停在允许的区域上时,它们仍然显示 jstree 绿色勾号图标。有什么想法如何阻止这种情况发生吗?

I have got a jstree working, and now want to be able to drag certain types of nodes to an external div element (containing a chart generated by highcharts). I don't actually want the node to be removed from the tree, nor do I want a copy of the node. I simply want the action of dropping the node to update the chart using the node's ID.

I think I can work out the chart updating bit, but it is the process of dragging/dropping with jstree I am finding a little confusing. Plus I only want certain types of nodes to be draggable (any with an attribute of rel="ds").

I'm struggling quite a bit with it, and haven't got very far. This is where I have got to:

$("#statstree").jstree({ 
       "json_data" : {
                "ajax" : {
                    "url" : "test.json",
                    "data" : function (n) { 
                        return { id : n.attr ? n.attr("id") : 0 }; 
                    },
                    "progressive_render" : true
                }
            },
            "types" : {
                "valid_children" : [ "root" ],
                "types" : {
                    "folder" : {
                        "icon" : { 
                            "image" : "images/folder.png" 
                        },
                        "valid_children" : [ "default" ],
                        //"max_depth" : 2,
                        "hover_node" : true,
                        "show_checkboxes" : false
                    },
                    "default" : {
                        "icon" : { 
                            "image" : "images/folder.png"
                        },
                        "valid_children" : [ "default" ]
                    },
                    "hover_node" : false
                }
            },
            "themes" : {
                "theme" : "classic",
                "dots" : false,
                "icons" : true
            },
            "core" : { "html_titles" : true },

            "dnd" : {
                "drop_target" : "#test_area",
                "drop_finish" : function (data) {
                    if(data.o.attr("rel") === "ds") {
                      //update chart with new data here?
                      //using data.o.attr("id")
                    }
                }
            },
            "crrm" : { move : { check_move : function (m) { return false; } } },

            "plugins" : ["themes","json_data","dnd","ui","types","crrm"]
        });

I read somewhere that binding a 'before.jstree' event could help out with blocking certain nodes from being dragged (and the crrm bit too). But I think I am doing it wrong. It appears that "start_drag" doesn't have access to data.args[0]:

$("#statstree").bind("before.jstree", function (e, data) {
            if(data.func === "start_drag" && data.args[0].parent("li").attr("rel") != "ds") {
                e.stopImmediatePropagation();
                return false;
            }
        });

Anyone got any ideas how I can achieve this task?

Cheers :)

EDIT: I've since worked out how to stop non 'ds' nodes being dropped on the area:

"drag_check" : function (data) {
                    if(data.r.attr("rel") != "ds") {
                        return false;
                    }
                    return { 
                        after : false, 
                        before : false, 
                        inside : true 
                    };
                }

I'll refine my question now:

How can I get the target's ID if I have multiple drop_targets? "drop_target" : "#testarea1, #testarea2"

EDIT 2

Doh, answering my quesitons as I go along! And I've been sitting staring at this for ages now. It's all coming to me in a flash:

data.r.attr("id")

Edit 3

Now the only remaining issue is that although all nodes which don't have an attribute of rel="ds" cannot be 'dropped' into the external div/chart, they still show a jstree green tick icon when hovering over the permitted area/s. Any ideas how to stop this from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-11-16 03:27:45

检查 valid_children 选项。

Check the valid_children option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文