How do I pre-select records on a remote paged grid without using the index?

How do I specify a set of pre-selected rows on load by record or by primaryKey (NOT by index) on a grid? I need a set records to be pre-selected when the grid is opened, but since the records are remote / paged selecting by index is useless. I am using persistent / checkboxOnly selection settings.

1 Reply

AG Ajith Govarthan Syncfusion Team April 22, 2020 10:21 AM UTC


Hi Adam, 

Greetings from Syncfusion. 
 
Query: How do I specify a set of pre-selected rows on load by record or by primaryKey (NOT by index) on a grid? 
 
To achieve your requirement we have used the dataBound event of EJ2 Grid.   

In dataBound event we have taken the current rows using the getRows method and using the rows we have taken the rowData from the rows using the getRowInfo method of EJ2 Grid.  

Based on the primarykey data we have taken the rowindex of the corresponding row and selected the rows using the selectRows method. We have attached the sample so please refer the sample for your reference. 

Code Snippet:  

app.component.ts 
 
    dataBound(args){ 
      let rows =  this.grid.getRows(); 
      let selectRows = []; 
      for (let  i = 0; i < rows.length; i++){ 
      let rowInfo = this.grid.getRowInfo(rows[i]); 
      let primaryData = (rowInfo as any).rowData[this.grid.getPrimaryKeyFieldNames()[0]]; 
      if( (primaryData > 10001 &&  primaryData < 10008) || (primaryData > 10013 && primaryData < 10016) ){ 
        let rowIndex =  parseInt(rowInfo.row.getAttribute('aria-rowindex')); 
       selectRows.push(rowIndex); 
      } 
      } 
        if(selectRows.length){ 
          this.grid.selectRows(selectRows); 
        } 
    } 


If the above solution does not meet your requirement, then please share the below details 

1. Share the Complete grid code sample. 

2. Share the Syncfusion package version. 

Regards, 
Ajith G. 


Loader.
Up arrow icon