简体   繁体   English

FancyTree在select上加载所有嵌套的子项

[英]FancyTree load all nested children on select

Here's my issue. 这是我的问题。 I'm using checkboxes and lazy load via ajax. 我通过ajax使用复选框和延迟加载。 However, if you were to check a parent item without expanding it, none of the child nodes have been loaded so they don't get checked. 但是,如果要检查父项而不扩展它,则不会加载任何子节点,因此不会检查它们。 How can I load all child and nested child nodes under a parent, and check them all, when they check the parent? 如何在父项下加载所有子节点和嵌套子节点,并在检查父节点时全部检查它们? Thanks, and this is what I have so far 谢谢,这就是我到目前为止所拥有的

$(function () {
    // Create the tree inside the <div id="tree"> element.
    $("#tree").fancytree({
        source: { url: "/Home/GetData", cache: true }
        , checkbox: true
        , icons: false
        , cache: true
        , lazyLoad: function (event, data) {
            var node = data.node;
            data.result = {
                url: "/Home/GetTreeViewData/" + node.key
                , data: { mode: "children", parent: node.key }
                , cache: true
            };
        }
        , selectMode: 3
        , select: function (event, data) { //here's where I need to load the children and any sub children for that node, if it has them, so they can be set to checked

        }
        , strings: {
            loading: "Grabbing places and events…",
            loadError: "Load error!"
        },
    })
});

Update The reason I need to pull these client side is because this is a treeview that will be loading google map markers. 更新我需要拉这些客户端的原因是因为这是一个将加载谷歌地图标记的树视图。 So if I have a structure like 所以,如果我有一个像这样的结构

Parent1 Parent1
->child1-1 - > child1-1
->->child1-1-1 - > - > child1-1-1
->child1-2 - > child1-2

all of those child nodes load lazy. 所有这些子节点都加载了懒惰。 however, if they were to check the parent node 1, then I'd need to load the markers for all of those child nodes. 但是,如果要检查父节点1,那么我需要为所有这些子节点加载标记。 That's why I'm looking for a way to recursively get all the children. 这就是为什么我正在寻找一种递归方式让所有孩子都能得到的方法。 Because it would be really hard to keep track of what markers have/haven't been added, if I don't just load the treeview items and check the boxes. 因为要跟踪已添加/未添加的标记真的很难,如果我不只是加载树视图项并选中复选框。 Make sense? 说得通?

I think you could use select event: 我想你可以使用select事件:

select: function(event, data) {
    var node = data.node;

    if (node.isSelected()) {
        if (node.isUndefined()) {
            // Load and select all child nodes
            node.load().done(function() {
                node.visit(function(childNode) {
                    childNode.setSelected(true);
                });
            });
        } else {
            // Select all child nodes
            node.visit(function(childNode) {
                childNode.setSelected(true);
            });
        }
    }
}

That way if the child nodes haven't been loaded they will be loaded and selected after that. 这样,如果尚未加载子节点,则在此之后将加载并选择它们。

First: maybe you don't even need to load all child nodes. 第一:也许你甚至不需要加载所有子节点。

In selectMode: 3 a selected parent means 'all children are selected too', so if you post the top-most selected parent nodes to your server, the backend could handle it accordingly. 在selectMode:3中,选定的父级表示“所有子项都被选中”,因此如果将最顶层选定的父节点发布到服务器,则后端可以相应地处理它。 The stopOnParent argument of the tree.getSelectedNodes method supports this as well. tree.getSelectedNodes方法的stopOnParent参数也支持这一点。

Another option would be to fix the the selection state of child nodes after a lazy parent was loaded: 另一种选择是在加载延迟父节点后修复子节点的选择状态:

$("#tree").fancytree({
  checkbox: true,
  selectMode: 3,
  source: { url: "/getTreeData", cache: false },
  lazyLoad: function(event, data) {
      data.result = {
          url: "/getTreeData",
          data: {mode: "children", parent: node.key},
          cache: false
      };
  },
  loadChildren: function(event, data) {
    // Apply parent's state to new child nodes:
    data.node.fixSelection3AfterClick();
  },

Update 更新

If you really need to load the lazy child nodes when the parent is selected, you could try in addition to the above code (untested) 如果您确实需要在选择父级时加载延迟子节点,则可以尝试以上代码(未经测试)

$("#tree").fancytree({
  ...
  select: function(event, data) {
    if( data.node.isUndefined() ) {
      data.node.load(); // triggers lazyLoad to load children
      // alternative:  node.expand()
    }
  },

Adding the unselect option, the Andrew's code would be: 添加取消选择选项,安德鲁的代码将是:

    if (node.isSelected()) {
            if (node.isUndefined()) {
            // Load and select all child nodes
            node.load().done(function() {
                    node.visit(function(childNode) {
                    childNode.setSelected(true);
                    });
            });
            } else {
            // Select all child nodes
            node.visit(function(childNode) {
                    childNode.setSelected(true);
            });
            }
    }
    else{
            if (node.isUndefined()) {
            // Load and unselect all child nodes
            node.load().done(function() {
                    node.visit(function(childNode) {
                    childNode.setSelected(false);
                    });
            });
            } else {
            // Select all child nodes
            node.visit(function(childNode) {
                    childNode.setSelected(false);
            });
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM