0

我正在尝试在单击的父节点上选择子节点。我找不到选择 api 的任何示例。我应该改用复选框吗?

<button id="btn">Highlight third row</button>
<div id="treeList"></div>
<script>
  $("#treeList").kendoTreeList({
    columns: [
      { field: "id" },
      { field: "name" },
      { field: "age" }
    ],
    dataSource: [
      { id: 1, parentId: null, name: "Jane Doe", age: 22, expanded: true },
      { id: 2, parentId: 1, name: "John Doe", age: 24 },
      { id: 3, parentId: 1, name: "Jenny Doe", age: 3 }
    ],
    selectable: "multiple row"
  });

</script>

如果选择“1”(顶级父节点),我正在尝试这样做,选择 2,3(子节点)。希望有javascript实现的本机解决方案。谢谢指教!

4

1 回答 1

0

选择状态由 TreeList 在数据项行呈现中维护<tr k-state-selected>,该状态不会反映在 TreeList 中dataSource。此外,数据源是唯一可以发现子关系的地方。因此,在处理选择和子项时会有些来回。

TreeList没有开箱即用的select事件TreeView,您不知道选择或取消选择了哪些节点。

change当一个操作导致选择或取消选择一个或多个行(如 shift-click 可以做)时触发该事件。在您的change处理程序中,您只能使用该select()方法了解所有选定行的当前状态。

因此,在 中change,您需要跟踪先前的选择状态和当前选择状态,以查找哪些行更改了选择状态,这将值得根据您的要求选择或取消选择它们的子项。

有关示例,请参阅此 Dojo 。这并不完美,因为change如果您只能确定选定的节点而不知道是否发生了“重新选择”(即再次单击选定的行)。无论如何,TreeList 层次结构的下降是递归执行的,并且依赖于dataSource数据项属性.hasChildren和 dataSource 方法.childNodes()

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>TreeList select (change)</title>

  <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.219/styles/kendo.common.min.css">
  <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.219/styles/kendo.rtl.min.css">
  <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.219/styles/kendo.default.min.css">
  <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.219/styles/kendo.mobile.all.min.css">

  <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
  <script src="https://kendo.cdn.telerik.com/2020.1.219/js/angular.min.js"></script>
  <script src="https://kendo.cdn.telerik.com/2020.1.219/js/jszip.min.js"></script>
  <script src="https://kendo.cdn.telerik.com/2020.1.219/js/kendo.all.min.js"></script></head>
<body>
  <p><a target="_blank" href="https://docs.telerik.com/kendo-ui/api/javascript/ui/treelist">TreeList docs</a></p>
  <div id="treeList"></div>
<script>
  selected_uids = [];              // track selected uids
  autoselecting = false;

  $("#treeList").kendoTreeList({
    columns: [
      { field: "id" },
      { field: "name" },
      { field: "age" }
    ],
    dataSource: [
      { id: 1, parentId: null, name: "Jane Doe", age: 22, expanded: false },
      { id: 2, parentId: 1, name: "John Doe", age: 24 },
      { id: 3, parentId: 1, name: "Jenny Doe", age: 3 },
      { id: 4, parentId: null, name: "Jane Mack", age: 22, expanded: false },
      { id: 5, parentId: 4, name: "John Mack", age: 24 },
      { id: 6, parentId: 4, name: "Jenny Mack", age: 3 }      
    ],
    selectable: "multiple row",
      change: function(e) {
      if (autoselecting) {
        e.preventDefault();
        return;
      }

      debugger;

      autoselecting = true;

      var selected_rows = this.select();
      console.log ('desel',selected_rows, selected_uids);

      // check for new deselections
      for (var index = 0; index < selected_uids.length; index++) {
        var selected_uid = selected_uids[index];
        if (selected_uid) {
            var row = $("tr[data-uid='" + selected_uid + "'].k-state-selected");
          if (row.length == 0) {
            // no such selected row for one that was previously tracked as selected
            deselectChildren(this, selected_uid);
            selected_uids[index] = null; // untrack
          }
        }
      }

      var selected_rows = this.select();
      console.log ('sel',selected_rows,selected_uids);

      // check for new selections
      for (var index = 0; index < selected_rows.length; index++) {        
        var data = this.dataItem(selected_rows[index]);
        console.log('data',data);
        if (jQuery.inArray(data.uid, selected_uids) == -1) { 
          // new selection
          selected_uids.push(data.uid);
          selectChildren(this, data);
        }
      }

      for (var i=0, j=0; i < selected_uids.length; i++) {
        if (selected_uids[i] != null) {
          if (i > j) {
            selected_uids[j] = selected_uids[i];
          }
          j++;
        }
      }

      selected_uids.length = j;

      autoselecting = false;
    },
  });

  function selectChildren(treeList, data) {
    if (!data.hasChildren) return;

    var children = treeList.dataSource.childNodes(data);    

    for (var index = 0; index < children.length; index++) {
      var child = children[index];
      if (jQuery.inArray(child.uid, selected_uids) == -1) {
          selected_uids.push(child.uid);
          treeList.select($("tr[data-uid='" + child.uid + "']"));
        selectChildren(treeList,child);
      }
    }
  }  

  function deselectChildren(treeList, uid) {
    var data = treeList.dataSource.getByUid(uid);
    if (!(data && data.hasChildren)) return;

    var children = treeList.dataSource.childNodes(data);

    for (var index = 0; index < children.length; index++) {
      var child = children[index];
            var row = $("tr[data-uid='" + child.uid + "'].k-state-selected");      
      if (row.length == 1) {
          var at = selected_uids.indexOf(child.uid);
        if (at >= 0) selected_uids[at] = null;
        row.removeClass('k-state-selected');
      }
      deselectChildren(treeList,child.uid);
    }
  }

  $("#treeList").data("kendoTreeList").select($("#treeList tbody>tr:nth(0)"));
</script>

</body>
</html>
于 2020-03-01T03:56:40.573 回答