简体   繁体   English

如何在角度为 4 的 ag-grid 中选择无限行模型上的行?

[英]How to select rows on infinite row model in ag-grid in angular 4?

I have used a custom headerComponent to show a checkbox in the header cell as a select all option in ag-grid with infinite scroll row model.我使用自定义 headerComponent 在标题单元格中显示一个复选框,作为具有无限滚动行模型的 ag-grid 中的全选选项。 On click of the checkbox i need to be able to select/deselect all rows in the table in the current block of rows.单击复选框时,我需要能够选择/取消选择当前行块中表中的所有行。 Also on scroll to the end of the table a server call is made to get the next set of rows.同样在滚动到表末尾时,会调用服务器来获取下一组行。 The new data loaded should also be selected by default.默认情况下还应选择加载的新数据。

I know that the ag-grid does not support the header selection for select all in the infinite row model.我知道 ag-grid 不支持无限行模型中全选的标题选择。 How can i do this programmatically?我怎样才能以编程方式做到这一点? How can i get all the selected rows too.我怎样才能获得所有选定的行。

This is my current code:这是我当前的代码:

Header component :标题组件:

import { Component, OnInit,ViewChild, ElementRef } from '@angular/core';
import {IHeaderParams} from "ag-grid/main";
import {IHeaderAngularComp} from "ag-grid-angular";

@Component({
  selector: 'app-customheader',
  templateUrl: './customheader.component.html',
  styleUrls: ['./customheader.component.css']
})
export class CustomheaderComponent implements IHeaderAngularComp{
  private params : any;

  agInit(params:any): void {
    console.log(params);
    this.params = params;
  }

  toggleSelectAll(){

  }

  constructor() { }
}

Header component template :标题组件模板:

<div>
  <div *ngIf="params.displayName == ''" ><input type="checkbox" class="selectAllCheckBox" (click)="toggleSelectAll()"> </div>
  <div>{{params.displayName}}</div>
</div>

Table component :表组件:

constructor(public appServices:AppServices) {
    this.rowData = this.assayDataList;
    this.columnDefs = [
      {
        //headerCheckboxSelection: true,
        //headerCheckboxSelectionFilteredOnly: true,
        checkboxSelection: true,
        width: 40,
        headerComponentParams: CustomheaderComponent
      },
      {
        headerName: "Date/Time",
        field: "createdDate",
        width: 230
      },
      {headerName: 'Assay Name', field: 'assayName', width: 240},
      {headerName: 'Samples', field: 'sampleCount', width: 100}

    ];

    this.gridOptions = {
      rowSelection: 'multiple',
      cacheBlockSize: 30,
      //maxBlocksInCache: 2,
      enableServerSideFilter: false,
      enableServerSideSorting: false,
      rowModelType: 'infinite',
      paginationAutoPageSize: true,
      suppressRowClickSelection: true
      //suppressCellSelection: true
    };

    this.frameworkComponents = { agColumnHeader: CustomheaderComponent };

  }

Had this issue as well, there's a callback function for each row node forEachNode((node, index)) , you can loop through an set each selection on your select-all event.也有这个问题,每个行节点forEachNode((node, index))一个回调函数forEachNode((node, index)) ,您可以循环遍历全选事件上的每个选择集。

 gridApi.forEachNode((row, index) => {
            gridApi.getRowNode(row.id).selectThisNode(true);
        });

I have a solution to make the new chunk of the data selected.我有一个解决方案来选择新的数据块。 Not sure how would you be able to propagate the flag for selectAll from CustomheaderComponent to your grid component, so just answering assuming you already have access to selectAll flag inside the component.不确定您如何能够将来自CustomheaderComponent selectAll标志传播到您的网格组件,因此只需回答假设您已经有权访问组件内的selectAll标志。

When you get the new chunk of data, after you call successCallback , do the selection logic.当您获得新的数据块时,在调用successCallback ,执行选择逻辑。

params.successCallback(rowsThisPage, this.count);
this.manageRecordsWithSelectAllFlag(rowsThisPage);

Get the id of the new records, grab the RowNode and then use RowNode.selectThisNode function to select it.获取新记录的 id,抓取RowNode然后使用RowNode.selectThisNode函数选择它。

private manageRecordsWithSelectAllFlag(rowsThisPage: any[]): void {
  let id;
  if(this.selectedAll) {
    for(let x = 0; x < rowsThisPage.length; x++) {
      id = rowsThisPage[x]['yourIdProperty'];
      this.gridApi.getRowNode(`${id}`).selectThisNode(this.selectedAll);
    }
  }
}

Simple solution简单的解决方案

Select all:全选:

gridApi.forEachNode(node => {
  node.setSelected(true);
});

Deselect All取消全选

gridApi.forEachNode(node => {
  node.setSelected(false);
});

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

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