jstree中如何回滚无法移动的节点
我试图弄清楚如何仅回滚未成功移动的文件夹节点。下面的代码是我正在尝试做的事情的示例。当您选择了几个文件夹并将它们移动到另一个文件夹中时,就会出现问题。如果其中一个目录无法移动,我希望能够将其回滚到其原始父目录。 不幸的是 $.jstree.rollback(data.rlbk);
会将所有选择的文件夹回滚到之前的位置。
$("#tree").jstree({...}).bind("move_node.jstree", function (e, data) {
// process all selected nodes directory
data.rslt.o.each(function (i) {
// Send request.
var move = $.parseJSON($.ajax({
url: "./jstree.php",
type: 'post',
async: false,
data: {
operation: "move_dir",
....
}
}).responseText);
// When everything's ok, the reponseText will be {success: true}
// In all other cases it won't exist at all.
if(move.success == undefined){
// Here I want to rollback the CURRENT failed node.
// $.jstree.rollback(data.rlbk); will rollback all
// of the directories that have been moved.
}
}
});
有办法做到这一点吗?
I'm trying to figure out how to rollback only a folder node that wasn't successfully moved. The code below is an example of what I'm trying to do. The problem comes when you have selected a couple of folders and moved them into another folder. If one of the directories fails to be moved I want to be able to roll it back to it's original parent.
Unfortunately $.jstree.rollback(data.rlbk);
rollsback all of the folders that were selected to their previous locations.
$("#tree").jstree({...}).bind("move_node.jstree", function (e, data) {
// process all selected nodes directory
data.rslt.o.each(function (i) {
// Send request.
var move = $.parseJSON($.ajax({
url: "./jstree.php",
type: 'post',
async: false,
data: {
operation: "move_dir",
....
}
}).responseText);
// When everything's ok, the reponseText will be {success: true}
// In all other cases it won't exist at all.
if(move.success == undefined){
// Here I want to rollback the CURRENT failed node.
// $.jstree.rollback(data.rlbk); will rollback all
// of the directories that have been moved.
}
}
});
Is there a way for this to be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以前看过使用 jstree,但没有在我的代码中使用它。因此,代码可能不正确,但概念应该正确。
根据您的代码,您似乎正在服务器端执行移动操作,并且希望更新树以反映结果。
根据 jsTree 文档,您似乎无法提交节点更新并回滚到上次提交。
您可以回滚树(所有更改)并随后执行移动,而不是仅回滚您不需要的更改。
为了更好地理解下面的代码,您可能需要阅读它(或创建副本),而不需要在“if”语句的条件中设置或引用“wasTriggeredByCode”的行。
I've looked at using jstree before, but haven't used it in my code. As a result, the code may not be correct, but the concepts should be.
Based on your code, it appears that you're performing the move operation on the server side and you want the tree to be updated to reflect the results.
Based on the jsTree documentation, it looks as though you cannot commit node updates and roll back to the last commit.
Instead of rolling back only the changes that you don't want, you can roll back the tree (all changes) and perform the moves afterward.
In order to better understand the code below, you may want to read it (or create a copy) without the lines where "wasTriggeredByCode" is set or referenced in the condition for an "if" statement.
我想过在 jstree 中有类似“onbeforenodemove”事件的东西,如下所示:
所以我查看了 jstree.js 文件(版本 jsTree 3.1.1)并搜索原始“move_node.jstree”处理程序的声明。它发现它声明了起始行 3689:
该函数在其主体末尾包含以下行:
上面的行实际上调用使用 .bind("move_node.jstree") 声明的回调。
所以在这个函数体的开头,我
在before_data赋值的末尾添加了这样的内容:Mind "cancelled": false。
还请注意在分配 new_par 等值之后插入上述内容。
我页面上的代码(jsTree 实例化)现在如下所示:
传递给“before_move_node.jstree”的数据对象包含与您在标准“move_node.jstree”数据参数中收到的相同值,因此您可以根据一切来决定是否要取消移动或放开它。如果您决定取消,只需将附加的“已取消”属性设置为 true 即可。整个行动将不会发生。
I thought about having something like "onbeforenodemove" event in jstree, something like this:
So I looked inside jstree.js file (version jsTree 3.1.1) and searched for declaration of original "move_node.jstree" handler. It found it declared starting line 3689:
This function contains the following line at the end of its body:
The above line actually calls your callback declared using .bind("move_node.jstree").
So at the beginning of this function body, I added this:
Mind "cancelled": false at the end of before_data assigned value.
Also mind inserting the above after new_par, etc. values are assigned.
Code (jsTree instantiation) on my page looks now like this:
data object passed to 'before_move_node.jstree' contains the same values that you receive in standard 'move_node.jstree' data argument so you have everything to decide whether you want to cancel the move or let it go. If you decide to cancel, just set the additional 'cancelled' property to true. The entire move will then not happen.
正如文档所述 https://github.com/vakata/jstree/wiki# more-on-configuration,您可以查看
more.core
属性示例
示例2
As the documentation says https://github.com/vakata/jstree/wiki#more-on-configuration, you can check
more.core
propertyExample
Example 2