【问题标题】:Is it possible to deselect a particular row in Aggrid?是否可以取消选择 Aggrid 中的特定行?
【发布时间】:2020-07-04 01:01:25
【问题描述】:

我正在尝试取消选择 agggrid 中的特定行。 我浏览了文档,但找不到。

  gridOptions.api.deselectAll();

这是我用来取消选择所有行的方法。但是有什么方法可以取消选择 Aggrid 中的特定行

我尝试了很多方法。请帮助我解决这个问题。

【问题讨论】:

    标签: javascript ag-grid


    【解决方案1】:

    您可以使用行节点api取消选择一个节点。

    基于docs -

    setSelected(newValue: boolean, clearSelection: boolean)
    选择(或取消选择)节点。 newValue=true 用于选择,newValue=false 取消选择。如果选择,则为 clearSelection 传递 true 将专门选择节点(即不进行多选)。如果做 取消选择,clearSelection 没有影响。

    您需要首先获取要取消选择的节点的 id 并执行类似的操作 -

    const node = gridOptions.api.getRowNode(id);
    node.setSelected(false);
    

    【讨论】:

      【解决方案2】:

      ag-Grid 没有开箱即用的单行选择切换。这只是我根据官方文档的理解,我肯定对此有误。

      因此,对于多行选择来说,它很容易并且开箱即用:

      您只需要在 gridOptions(或任何您调用的配置对象)中将 rowMultiSelectWithClick 设置为 truerowSelection 设置为 'multiple'

      对于单行选择/取消选择,我提出了以下建议 (working plunker):

      const gridOptions = {
      
      
      columnDefs: [
          { field: 'name' },
          { field: 'continent' },
          { field: 'language' },
        ],
        defaultColDef: {
          flex: 1,
          resizable: true,
        },
        rowData: rowData,
      
        // here i'm just checking for the right selection mode
        // and firing my custom little function when the row is clicked
        onRowClicked: this.rowSelection === 'single' && toggleRowSelection,
      
        rowMultiSelectWithClick: true,
        rowSelection: 'single'
      };
      
      let selectedRow = null;
      
      // this function is just for keeping track of selected row id
      // and programmatically deselecting the row by doing node.setSelected(false)
      
      function toggleRowSelection({ node }) {
        if (selectedRow !== node.id) {
          selectedRow = node.id;
        } else {
          selectedRow = null;
          node.setSelected(false);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-01
        • 2011-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多