How to select a row for ejs-grid with enableVirtualization='true'

Hi,
We are using ejs-grid control to render over 200,000 records with more than 100 attributes in one page, so we have to use enableVirtualization='true' feature in ejs-grid.
 
In the project this ejs-grid need to communicate with their related components( for example they have same Id),  

(1)  when click a row in ejs-grid, we need to get the events and pop up a window on the its related component or location.
(2) when click a component, its related row in ejs-grid will be selected and be visible in the showing area.

We don't know if this can be done and how to implement this  in ejs-grid when enableVirtualization='true' ? 

Thanks,



13 Replies

SK Sujith Kumar Rajkumar Syncfusion Team March 30, 2020 11:11 AM UTC

Hi Chunfeng, 

Greetings from Syncfusion support. 

Query – 1: When click a row in ejs-grid, we need to get the events and pop up a window on the its related component or location. 

On selecting a grid row, the rowSelecting and rowSelected events will be triggered with the details of the selected row returned in its arguments. You can use these events to perform your required operations. 


Query – 2: When click a component, its related row in ejs-grid will be selected and be visible in the showing area. 

Based on this query we are able to understand that you wish to select grid rows on clicking an external component in your application. You can use the selectRow or selectRows method to select single or multiple rows in the Grid respectively using its index value as demonstrated in the below code snippet, 

// Select single row 
this.$refs.grid.ej2Instances.selectRow(22); 
// Select multiple rows 
this.$refs.grid.ej2Instances.selectRows([1,2,3]); 


We have prepared a sample based on the above queries for your reference which you can find below, 


Let us know if you have any concerns. 

Regards, 
Sujith R 



CZ CZ March 30, 2020 01:47 PM UTC

Thank you for your quick answer, we tested the project, below is our question

(1) if we scroll the grid or scroll a little(such as Task ID = 11 is visible in ViewPort), click "Select Row" button , the Task ID = 23 is selected and grid is scrolling and the selected row is visible in grid. it works very well.

but after I scroll the grid more(such as scroll to TaskID = 7532 is the first row in ViewPort), click "Selected Row" button,  the row can be selected, but grid will not scroll to make this selected row visible in the grid, how to fix this?


We have additional question
(2) The data in grid will be huge, can we dynamically get only the visible part data from remote server side(such as only 50 row are visible in grid although there are 20000 records in server side, we have get the visible 50 rows data after grid scroll) ?

(3) In the project, the columns of grid are defined in Database, we will dynamically set up grid columns.
    some columns  data display value  are different from its store value( such as we have UserId,  but we need display UserName), if the data display in columns can hooked up with a function, which can return display value based on column name? if yes, can you give us a sample, or give us a solution?

Thanks,

 


SK Sujith Kumar Rajkumar Syncfusion Team March 31, 2020 12:22 PM UTC

Hi Chunfeng, 

Query – 1: “How to scroll grid to make selected row visible when virtualization is enabled” 

You can achieve this requirement by setting the selected row element’s position to the content’s scrollTop value which automatically scrolls to the selected row index’s position. The selected row index’s position can be calculated by multiplying the selected index value with the grid’s row height as demonstrated in the below code snippet, 

OnCreated: function(args) { 
      var parent = this; 
      // Click event for selectrow button 
      document.getElementById("selectrow").addEventListener("click", event => { 
        parent.$refs.grid.ej2Instances.selectRow(parent.selectIndex); 
        var gridRowHeight = this.$refs.grid.ej2Instances.getRowHeight(); 
        this.$refs.grid.ej2Instances.getContent().firstElementChild.scrollTop = this.selectIndex * gridRowHeight; 
      }); 
}, 

We have modified the previously provided sample based on this which you can find below, 


Query – 2: The data in grid will be huge, can we dynamically get only the visible part data from remote server side(such as only 50 row are visible in grid although there are 20000 records in server side, we have get the visible 50 rows data after grid scroll)  

Based on this query we are able to understand that your requirement is to get 50 rows at a time from the server and be able to scroll between these 50 rows without any request being sent. You can achieve this by setting the required row count value in the pageSize property of the grid’s pageSettings as demonstrated in the below code snippet, 

<ejs-grid :dataSource='data' height=300 :enableVirtualization=true :pageSettings='options'> 
</ejs-grid> 
 
export default { 
        name: 'app', 
        data() { 
            return { 
                options: { pageSize: 50 }, 
            }; 
        }, 
           . 
           . 
} 

Query – 3: “In the project, the columns of grid are defined in Database, we will dynamically set up grid columns. Some columns  data display value  are different from its store value, if the data display in columns can hooked up with a function, which can return display value based on column name?” 
 
Based on this query we suspect your requirement is to set the column’s header text value which will be different from the field name. For this if you have defined the grid columns then you can directly set the required header text for the column by using its headerText property as demonstrated in the below code snippet, 
 
<e-column field='TaskID' headerText='Task ID' width=70></e-column> 
 
If the grid columns are auto-generated in your application then you need to dynamically set the headerText for all the columns in the grid’s dataBound event as demonstrated in the below code snippet, 
 
// Grid’s dataBound event function 
OnDataBound: function(args) { 
        // All the grid columns are returned 
        var gridColumns = this.$refs.gridColumns.ej2Instances.columns; 
        var count = 0; 
        while (count < gridColumns.length) { 
            gridColumns[count].headerText = 'Header text ' + count; 
            count++; 
        } 
}, 
 
If we misunderstood your query please get back to us with the details. 
 
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 



CZ CZ March 31, 2020 03:37 PM UTC

Query – 2: The data in grid will be huge, can we dynamically get only the visible part data from remote server side(such as only 50 row are visible in grid although there are 20000 records in server side, we have get the visible 50 rows data after grid scroll)  

How can I know the data range in grid will be refresh after I scroll the grid(such as start index and refresh data count and sort parameter), and how to display the content after I get this part data from server? can you provide a example?

Query – 3: “In the project, the columns of grid are defined in Database, we will dynamically set up grid columns. Some columns  data display value  are different from its store value, if the data display in columns can hooked up with a function, which can return display value based on column name?” 

we have a user list in our project , such as 
data(){
return {
   userList  =
     [ { UserID: 100, UserName: 'John'},
       { UserID: 101, UserName: 'Peter'},
       { UserID: 102, UserName: 'Jeff'},
       { UserID: 103, UserName: 'David'},
        ....
     ]
}

the record data in datagrid return from database is like
[ { CreateUserID: 100,  ModifiedUserID: 101,.....},
  { CreateUserID: 102,  ModifiedUserID: 102,.....}, 
  { CreateUserID: 102,  ModifiedUserID: 103,.....}, 
]

We need to show UserName in CreateUserID and ModifiedUserID field, what is best way to show User Name in the Id Fields?

Query – 4: How to display single property in a object from the return data?

We will get some Lat/Lng object in record Data like below, each LatLng object include two fields, Lat and Lng, but LatLng itself maybe null
[  { LatLng: { Lat:100.01, Lng: 200.12},.....},
   { LatLng: { Lat:100.21, Lng: 200.22},.....},
     LatLng: null
  ......
]

but In the Grid the columns will show Lat and Lng in two columns, what is the best way to show the Lat and Lng value?

Thanks,




SK Sujith Kumar Rajkumar Syncfusion Team April 1, 2020 10:36 AM UTC

Hi Chunfeng, 

Query – 1: How can I know the data range in grid will be refresh after I scroll the grid(such as start index and refresh data count and sort parameter), and how to display the content after I get this part data from server? 

From your query we suspect your requirement is - how to know the data range set in the grid and how to return the data based on that from the server. The grid supports binding remote data using data manager from different adaptors like, OData, ODataV4, URL adaptor, Web API adaptor. On using these adaptors the Grid sends skip and take values along with the data request to the server. Based on these values you can return the required records back to the Grid. More details on this can be found in the below help documentation site, 

                                                       https://ej2.syncfusion.com/vue/documentation/grid/data-binding/#web-api 

Query – 2: We need to show UserName in CreateUserID and ModifiedUserID field, what is best way to show User Name in the Id Fields? 

You can achieve this requirement using foreign key column. This is explained in detail in the below help documentation site, 


We have explained this below on how to achieve it based on your mentioned field - CreateUserID

In your case, your requirement is to display the user name from a data source in the CreateUserID column field. So in the grid column definition for CreateUserID you need to define the dataSource property(in your case - userList data), the mapping field between the foreign data and the column data in the foreignKeyField property(in your case - UserID), the field to be displayed in the column in foreignKeyValue property(in your case - UserName). And so the column needs to be defined like below for your scenario, 

<e-column field='CreateUserID' headerText='Create User ID' :dataSource='userList' foreignKeyField='UserID'  foreignKeyValue='UserName'></e-column> 

Similarly you can achieve this for ModifiedUserID field. 

You can check the below online demo sample for your reference, 



Query – 3: How to display single property in a object from the return data? 

You can access data from object and bind it to the Grid filed using complex data binding with the dot(.) operator as demonstrated in the below code snippet based on your field values, 

<e-column field='LatLng.Lat' headerText='Lat'></e-column> 
<e-column field='LatLng.Lng' headerText='Lng'></e-column> 

This is documented in the below help site which you can check for more details, 


Note: If the data is an array of objects then you need to access them like, “LatLng.0.Lat” and “LatLng.0.Lng”. Please check below help documentation for more details on this, 

Complex data binding with array of objects documentation: https://ej2.syncfusion.com/vue/documentation/grid/how-to/list-of-array-of-objects/ 

Please get back to us if you require any further assistance. 

Regards, 
Sujith R 



CZ CZ April 1, 2020 06:12 PM UTC

Hi Sujith,

Thanks for the precise answer as always.

Query – 1: How can I know the data range in grid will be refresh after I scroll the grid(such as start index and refresh data count and sort parameter), and how to display the content after I get this part data from server? 

I read the document and learn rules.

In our project, we use axios.post to send parameters to our web server and get  data, I like to know if we can get  "skip, take and sort parameters " of the grid after scrolling and integrate into our post  parameters to the server and get return data?

additional question:
(1) I don't know if we can show row sequence number in grid, such as first row show 0, second row show 1, 
(2) for grid header text if it can be wrap to next line with header height increased if its column width is not enough to show whole text, but the data row are not, it keep same height.


Thanks,






SK Sujith Kumar Rajkumar Syncfusion Team April 2, 2020 11:16 AM UTC

Hi Chunfeng, 

You’re welcome. We are happy to assist you. 
 
Query – 1: In our project, we use axios.post to send parameters to our web server and get  data, I like to know if we can get  "skip, take and sort parameters " of the grid after scrolling and integrate into our post  parameters to the server and get return data? 
 
On scrolling past the records in a page, actionBegin event will be triggered with requestType as “virtualscroll”(This will be returned in the event arguments). In this event the page number value will be returned based on which you can calculate the records to skip and take and then send request to your server with these values as demonstrated in the below code snippet, 
 
// Grid’s actionBegin event function 
actionBegin: function(args) { 
        if (args.requestType == "virtualscroll") { 
            // Get the current page value 
            var curPage = args.virtualInfo.page; 
            // Calculate the records to skip 
            var recordsToSkip = this.$refs.grid.ej2Instances.pageSettings.pageSize * (curPage - 1); 
            // Get the records to take 
            var recordsToTake = this.$refs.grid.ej2Instances.pageSettings.pageSize; 
            // With these value you can send request to your server 
        } 
} 
 
The sorted columns can be accessed from the grid instance’s sort module as demonstrated in the below code snippet, 
 
this.$refs.grid.ej2Instances.sortModule 
 
The sorted column properties will be returned as displayed in below image, 
 
 
 
You can pass these skip, take and sort parameters in your server request and assign the returned data to the Grid’s data source. 
 
Query – 2: I don't know if we can show row sequence number in grid, such as first row show 0, second row show 1 

You can achieve this requirement by assigning the row index value to the required column’s innerText using the rowDataBound event of the Grid. This is explained below, 

Initially bind rowDataBound event to the Grid which will be triggered before each row is appended to the Grid. Here you can get the row element’s index value from the arguments and assign it to the required column’s innerText. This is demonstrated in the below code snippet, 

// Grid’s rowDataBound event 
rowDataBound: function(args) { 
        // Row index is returned and assigned to the required cell’s innerText 
        var rowIndex = parseInt(args.row.getAttribute('aria-rowindex')); 
        // Here we have assigned it to the first column’s cell 
        args.row.querySelector('.e-rowcell').innerText = rowIndex; 
} 

Sample for your reference, 



Query – 3: For grid header text if it can be wrap to next line with header height increased if its column width is not enough to show whole text, but the data row are not, it keep same height. 

From your query we suspect your requirement is to wrap the header text and content. You can enable text wrap by setting the allowTextWrap property of the Grid to true. By default it wraps both header and content. You can change it to wrap either header or content alone by using the textWrapSettings property. 


If we misunderstood your query or if you require further assistance please get back to us. 

Regards, 
Sujith R 



CZ CZ April 2, 2020 04:46 PM UTC


Hi Sujith,

Thank you very much for your help!

(1) In previous solution about UserId and UserName issue, the solution is using foreignKeyField like blow

<e-column field='CreateUserID' headerText='Create User ID' :dataSource='userList' foreignKeyField='UserID'  foreignKeyValue='UserName'></e-column> 

In our project, all the columns are dynamically, and column information are from database, we set up like below
<ejs-grid :columns = datasheetColumns >                
</ejs-grid>

we will  bind columns to datasheetColumns dynamically, the question is how can to set up bounding field :dataSource 
assign property like below
{    dataSource: 'userList' 
}  will not work

(2) In our project, first column content is a customized checkbox which user can clicked(it is not select or un-select the whole row),  the rest columns in the grid are dynamically from database,  later we will get checked rows, it is complex, we do't know how to do it.

additional question:

(3) how to set up the whole grid header and content style? 
  We don't know if we can set up header font and margin of whole header, not set up by each column header special style.
  We like to set up content font.

(4) In our project, we need to set up gird row background color like below.
  Row 0, row 2, row 4,... are one color
  Row 1, row 3, row 4 are different color.

Thanks


SK Sujith Kumar Rajkumar Syncfusion Team April 3, 2020 11:37 AM UTC

Hi Chunfeng, 

No problem. We are here to assist you. 
 
Query – 1: “How to bind foreign column data source for dynamically generated columns?” 

For dynamically generated columns the foreign column data source can be assigned like demonstrated in the below code snippet, 

import { GridPlugin, ForeignKey, Page } from "@syncfusion/ej2-vue-grids"; 
import { data, hierarchyOrderdata, employeeData } from './datasource.js'; 
 
Vue.use(GridPlugin); 
 
export default { 
  name: 'app', 
  data() { 
    return { 
    datasheetColumns: [ 
        { field: 'OrderID', headerText: 'Order ID', width: 100 }, 
        { field: 'EmployeeID', headerText: 'Employee Name', foreignKeyValue: 'FirstName', foreignKeyField: 'EmployeeID', dataSource: employeeData, width: 120 }, 
        { field: 'Freight', headerText: 'Freight', format: 'N2', width: 100 } 
    ], 
    data: hierarchyOrderdata 
    } 
  } 
} 

Alternately, you could also assign the foreign column data source directly to the grid column using grid instance as demonstrated in the below code snippet, 

// Directly assign the foreign column data to the grid columns based on its index 
this.$refs.grid.ej2Instances.columns[1].dataSource = employeeData; 
 
Query – 2: In our project, first column content is a customized checkbox which user can clicked(it is not select or un-select the whole row),  the rest columns in the grid are dynamically from database,  later we will get checked rows, it is complex, we don't know how to do it. 
 
We are not able to properly understand your requirement based on the query. So can you please elaborate on it(If possible with a pictorial representation to better understand it). 
 
Query – 3: How to set up the whole grid header and content style?  

You can modify the Grid’s appearance(Including styling the header and content) by overriding its default CSS. The list of CSS classes that needs to be overridden for each area is explained in detail in the below help documentation site which you can refer for the style customization, 


For your requirement, the Grid header and content section can be customized by overriding the CSS classes and setting the required styles to them which are demonstrated in the below code snippet, 

<style> 
// CSS class for header cells 
.e-headercelldiv { 
  color: #0066ff; 
  font-family: cursive; 
} 
 
// CSS class for content cells 
.e-rowcell { 
  color: #590c0c !important; 
  font-family: sans-serif; 
} 
</style> 
 
Query – 4: In our project, we need to set up grid row background color like - Row 0, row 2, row 4,... are one color and Row 1, row 3, row 4 are different color. 

For each alternate rows rendered in the grid cell an additional class – “e-altrow” will be added to the row element. You can set the background for alternate rows by overriding CSS class and setting styles for “e-row” and “e-altrow” classes as demonstrated in the below code snippet, 

<style> 
// CSS class for row 
.e-row { 
  background-color: #00ff43; 
} 
 
// CSS class for alternate rows 
.e-row.e-altrow { 
  background-color: #00ffff; 
} 
 
// CSS class for header 
.e-headercell { 
  background-color: #df239c !important; 
} 
</style> 

We have prepared a sample based on the queries – 1, 3 & 4 for your reference which you can find below, 


Let us know if you have any concerns. 

Regards, 
Sujith R 



CZ CZ April 3, 2020 03:05 PM UTC

Hi Sujith,

Thank you for your professional help.

About Query – 2: 

The grid columns are set up dynamically, the first columns will bind to a check box which user can click it, after it is clicked, a event will occur and we need get this check box status and this row binding value to do related work.

See below pictures, it is what we will do


The first column is a check box, when a check box is checked or un-checked, we will show or hide  its location on a map, multiple locations can be show on the map at same time. only first column is clickable, the reset columns are just show some information.

Thanks,
chunfeng








SK Sujith Kumar Rajkumar Syncfusion Team April 6, 2020 12:50 PM UTC

Hi Chunfeng, 
 
From your query we are able to understand that your requirement is to render a checkbox column in the Grid and in its change event you need its check state and the row data to perform your operations. You can achieve this by rendering EJ2 CheckBox control inside the Grid column using template, bind change event to the CheckBox and here get the check state which is returned in the event arguments and the row data using the getRowInfo method. This is explained below, 
 
Initially render a checkbox in the Grid using column template and set its checked state using the field value bound in the Grid. 
 
<ejs-grid id="grid" ref="gridObj" :dataBound='onDataBound' :dataSource="data" height="315"> 
    <e-columns> 
        <e-column field='status' headerText="State" :template="checkTemplate" width="80"></e-column> 
    </e-columns> 
</ejs-grid> 
 
export default { 
        name: 'app', 
        data() { 
            return { 
                checkTemplate: () => { 
                    return { 
                        template: Vue.component("columnTemplate", { 
                            template: `<div v-if=cData class="template_checkbox"> 
                    <input type="checkbox" checked /> 
                </div> 
                <div v-else class="template_checkbox"> 
                    <input type="checkbox" /> 
                </div>`, 
                            data: function () { 
                                return { 
                                    data: {} 
                                }; 
                            }, 
                            computed: { 
                                cData: function () { 
                                    // Field value is returned based on which checkbox is rendered using v-if and v-else condition 
                                    return this.data.status; 
                                } 
                            } 
                        }) 
                    }; 
                } 
 
            }; 
        }, 
} 
 
Now in the Grid’s dataBound event get all the checkbox elements rendered inside the template column, initialize Checkbox component and append it to the checkbox element. Then bind change event to the checkbox component where the checked state and row info can be retrieved. 
 
// Grid’s dataBound event function 
onDataBound: function(args) { 
        // Checkbox elements rendered inside the column template are retrieved 
        var checkboxElements = this.$refs.gridObj.ej2Instances.element.querySelectorAll('.template_checkbox'); 
        var count = 0; 
        while (count < checkboxElements.length) { 
            var inputEle = checkboxElements[count].querySelector('input'); 
            // Initialize CheckBox component. 
            var checkbox = new CheckBox({ 
                // Checked state is set based on input elements state 
                checked: (inputEle.checked) ? true : false, 
                change: this.checkboxChange.bind(this) 
            }); 
            // Render initialized CheckBox 
            checkbox.appendTo(inputEle); 
            count++; 
        } 
}, 
// Checkbox’s change event 
checkboxChange: function(args) { 
        // Checkbox state is retrieved 
        console.log('Checked state: ' + args.checked); 
        // Row data is retrieved by passing the checkbox element to Grid’s getRowInfo method 
        var rowData = this.$refs.gridObj.ej2Instances.getRowInfo(args.event.target); 
} 
 
Sample for your reference, 
 
 
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 



CZ CZ April 6, 2020 02:40 PM UTC

Hi Sujith,

Thanks

chunfeng


SK Sujith Kumar Rajkumar Syncfusion Team April 7, 2020 09:40 AM UTC

Hi Chunfeng, 

You’re welcome. Please get back to us if you require any further assistance. 

Regards, 
Sujith R 


Loader.
Up arrow icon