Ag Grid 单击复选框调用函数

11 javascript ag-grid angular ag-grid-angular

checkboxSelection: true在 Angular & Javascript 中,我在其中一列中使用了 Ag-Grid 。

每当单击任何行的复选框时,我都需要调用一个函数...该怎么做?再次,如何在 Ag-Grid 中选中复选框时调用函数?

wen*_*jun 10

我假设只有一列具有复选框选择。

您可以使用selectionChanged事件绑定。每当您选择或取消选择该复选框时都会发出此事件。您可以在这里阅读更多相关内容。

但是,如果您想检查所选行是否已选中或未选中,最好绑定到rowSelected事件。

例如,在 component.html 上,您可以将方法绑定onSelectionChanged()selectionChanged事件。

<ag-grid-angular
    #agGrid
    style="width: 100%; height: 100%;"
    id="myGrid"
    class="ag-theme-balham"
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [suppressRowClickSelection]="true"
    [rowSelection]="rowSelection"
    [rowData]="rowData"
    (gridReady)="onGridReady($event)"
    (rowSelected)="onRowSelected($event)"
    (selectionChanged)="onSelectionChanged($event)"
  ></ag-grid-angular>
Run Code Online (Sandbox Code Playgroud)

在你的 component.ts 上,你将定义onSelectionChanged()方法

onRowSelected(event) {
  console.log(event);
  console.log(event.node.selected);
  console.log(event.rowIndex);
}

onSelectionChanged(event) {
  console.log(event); // verify that the method is fired upon selection
  // do the rest
}
Run Code Online (Sandbox Code Playgroud)

这是一个演示

  • 我将其标记为答案,因为它工作正常。在我的系统上,谢谢@wentjun。干杯! (2认同)
  • 还有一件事我们如何检查,复选框在被选中后是否被取消选中,因为事件是在 SelectionChanged 上触发的,这很好,它总是会被触发 (2认同)