Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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 };

  }
like image 607
UI Dev Avatar asked Jul 26 '18 18:07

UI Dev


People also ask

How do I select a row in angular data grid?

Angular Data Grid: Row Selection Select a row by clicking on it. Selecting a row will remove any previous selection unless you hold down Ctrl while clicking. Selecting a row and holding down Shift while clicking a second row will select the range.

How do I get selected rows in AG-grid?

You can then use the grid API method getSelectedNodes () to return a list of all currently selected rows in ag-Grid. Extracting the data from each node is as simple as mapping over each node and returning its data property. You can also see this illustrated in the Angular/React/Vue.js examples below:

Is there a way to refresh a table with agGrid in angular?

i personally didn't use agGrid with angular, but if you are using infiniteRow model, you can use the setDataSource method that would force your table to refresh it's data. the setDataSource method accepts an object with two minimum parameters :

How to get the last selected node in AG-grid?

Sure, you have to use onRowSelected event for that, in which you'll get the required data as event argument. Here is the live example: Plunk - ag-grid rowMultiSelectWithClick You can use gridApi to then get the last selected node. This will even work when you are deselecting a row.


2 Answers

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.

 gridApi.forEachNode((row, index) => {
            gridApi.getRowNode(row.id).selectThisNode(true);
        });
like image 166
Brendan B Avatar answered Oct 02 '22 09:10

Brendan B


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.

When you get the new chunk of data, after you call successCallback, do the selection logic.

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.

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);
    }
  }
}
like image 20
Paritosh Avatar answered Oct 02 '22 09:10

Paritosh