javascript - 我们可以在删除父节点之前复制该节点吗?

标签 javascript jquery

我想限制某些节点添加到父节点中。我想在删除节点之前检查一个条件,如果节点有(删除的)子节点,则它不会删除,否则如果它没有子节点,它将删除。我想我需要使用复制概念。

详细信息:请仅检查两个节点“c-a”、“c-b”。如果它们落在“a”上,它们就会成为“a”的子节点。但如果它们落在“b”上,它们就会成为“a”的子节点。无法放下并返回到原来的位置。但是如果它们落在“b-a”上,它们可以成为“b-a”的子级。

这可能吗?我确实检查了可用的 API here但没有人像这样工作。

fiddle link :

$('#tree').jstree({
    core: {
       check_callback: function (op, node, node_parent) {
          console.log(op);
           console.log(node)
           console.log(node_parent.id)
          return op == 'move_node' ? node_parent.id.indexOf('not') === -1 : true;
       }
    },
    dnd: {
       is_draggable: function (x) {
          return x[0].id.indexOf('not') === -1;
       }
    },
    "plugins": ["dnd"]
 });

最佳答案

给你(再次)

//we'll call the node having `not` in its id, a "n_node"
$('#tree').jstree({
    core: {
       check_callback: function (op, node, node_parent) {
          var ret = true;
          if (op == 'move_node' && node.id.indexOf('not') !== -1) {
             //n_node can only be dropped in an empty non-n_node
             ret = node_parent.id.indexOf('not') === -1 && !node_parent.children.length;
          }
          return ret;
       }
    },
    dnd: {
       check_while_dragging: false
    },
    "plugins": ["dnd"]
 });

更新

对于jsfiddle.net/fuu94/127,在此 fiddle 用户可以在“c-a”内添加节点“a”,“b”,“b-a”,“b-b”,“b-b-a”,“b-b-b”,“c-b”。我们可以限制它们

//function to check n_node in one place
function isNNode(node) {return node.id.indexOf('not') !== -1;}  

并更换您的条件

if (op == 'move_node' && node.id.indexOf('not') !== -1) {
    //n_node can only be dropped in an empty non-n_node
    ret = node_parent.id.indexOf('not') === -1 && !node_parent.children.length;
}

具有以下内容

if (op == 'move_node') {
    ret = isNNode(node) ? !isNNode(node_parent) && !node_parent.children.length : !isNNode(node_parent);
}

我希望您的所有问题现在都得到解决。

关于javascript - 我们可以在删除父节点之前复制该节点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23676091/

相关文章:

javascript - jQuery:如何从列表中删除列表项?

javascript - 图片库使第一张图片变大

javascript - jquery中的(this)和(_this)有什么区别吗?

javascript - 如何在弹出窗口中获取所有表单元素及其值

javascript - jquery - 子菜单在主菜单的鼠标移开时隐藏

javascript - ListBox 项目在 jQuery 中被空数组替换后仍然存在

javascript - 使用ajax和formData上传文件

javascript - 动画在特定时刻开始

javascript - 停止从子元素到父元素的事件转发

javascript - 将 Div 滑出屏幕而不是淡入淡出