【问题标题】:Set datapath for tree data ag grid为树数据 ag 网格设置数据路径
【发布时间】:2021-02-12 02:04:20
【问题描述】:

我有如下数据

let data = [{id: 1,name: 'x', children: [{name:'y', id:2}, {name:'c', id: 3}]}]

我想用客户端行模型显示树数据

如何设置getdata路径并显示名称为x的父行和名称为y和c的子行?

【问题讨论】:

    标签: ag-grid ag-grid-react


    【解决方案1】:

    嵌套子级很难在 TreeData 模式下使用。您需要以datapath 格式重新构建数据,以便网格更容易呈现。我编写了一个简单的 flatten 函数来组织层次结构的数据,以便网格可以轻松地使用它。同样在 official documentation 上,它的文档记录非常差,因此我们根据网格重新构造数据是有道理的。

    var rawData = [{id:9,name:'xx'},{id:10,name:'yy',children: [{
        name: 'yyy',
        id: 21,}]},{
      id: 1,
      name: 'x',
      children: [{
        name: 'y',
        id: 2,
        children: [{
          name: 'a',
          id: 4
        }, {
          name: 'b',
          id: 5
        }]
      }, {
        name: 'c',
        id: 3,
        children: [{
          name: 'd',
          id: 6
        }, {
          name: 'e',
          id: 7
        }]
      }]
    }];
    //function to flatten and make heirarchy
    
    function flatten(arr, parentref) {
      return arr ? arr.reduce((result, item) => [
    
        ...result,
        {
          name: item.name,
          id: item.id,
          hierarchy: parentref ? [...parentref, item.name] : [item.name]
        },
        ...flatten(item.children, parentref ? [...parentref, item.name] : [item.name])
      ], []) : [];
    }
    var rowData = flatten(rawData);
    var gridOptions = {
      columnDefs: [{
        field: 'id'
      }],
      defaultColDef: {
        flex: 1,
      },
      autoGroupColumnDef: {
        headerName: 'name',
        minWidth: 300,
        cellRendererParams: {
          suppressCount: true,
        },
      },
      rowData: rowData,
      treeData: true, // enable Tree Data mode
      animateRows: true,
      groupDefaultExpanded: -1, // expand all groups by default
      getDataPath: function(data) {
        return data.hierarchy;
      },
    };
    
    function onFilterTextBoxChanged() {
      gridOptions.api.setQuickFilter(
        document.getElementById('filter-text-box').value
      );
    }
    
    // wait for the document to be loaded, otherwise
    // ag-Grid will not find the div in the document.
    document.addEventListener('DOMContentLoaded', function() {
      // lookup the container we want the Grid to use
      var eGridDiv = document.querySelector('#myGrid');
    
      // create the grid passing in the div to use together with the columns & data we want to use
      new agGrid.Grid(eGridDiv, gridOptions);
    });
    .example-wrapper {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    
    #myGrid {
      flex: 1 1 0px;
      width: 100%;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <script>
        var __basePath = './';
      </script>
      <style media="only screen">
        html,
        body {
          height: 100%;
          width: 100%;
          margin: 0;
          box-sizing: border-box;
          -webkit-overflow-scrolling: touch;
        }
        
        html {
          position: absolute;
          top: 0;
          left: 0;
          padding: 0;
          overflow: auto;
        }
        
        body {
          padding: 1rem;
          overflow: auto;
        }
      </style>
      <script src="https://unpkg.com/@ag-grid-enterprise/all-modules@24.1.0/dist/ag-grid-enterprise.min.js"></script>
      <link rel="stylesheet" href="styles.css">
    </head>
    
    <body>
      <div class="example-wrapper">
        <div style="margin-bottom: 5px;">
          <input type="text" id="filter-text-box" placeholder="Filter..." oninput="onFilterTextBoxChanged()" />
        </div>
        <div id="myGrid" class="ag-theme-alpine"></div>
      </div>
    
    </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多