We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Accessing Value of totalItemsInfo or Similar Value in Grid

Dear All,

I would like to know how to access the total number of rows/entries in a grid. The number I am referring to is displayed at the bottom of the grid's pager. For the purpose of localization I already changed the following property totalItemsInfo. I was wondering if there was an option for accessing this or a similar property, since I would like to display a message if a certain number of entries has been reached or not.

I was thinking along these lines, but however, undefined was returned.

<script>
    var gridObj = $("#Grid");
    var totalItems = gridObj.totalItemsInfo;
    console.log("TOTAL ITEMS: " + totalItems);   
</script>

The Grid is built as follows (function for export to Excel skipped for brevity):
 
@{
        List<object> cols = new List<object>();
        cols.Add(new { field = "SomeField", direction = "Descending" });
    }
     @Html.EJS().Grid("Grid").Locale("de").DataSource((IEnumerable<object>)ViewBag.DataSource).Height(500).Width("100%").ToolbarClick("toolbarClick").AllowSorting().AllowFiltering(true).AllowReordering().AllowResizing(true).Columns(col =>
    { [...] // coloumns
   
    }).AllowPaging().DataBound("dataBound").Toolbar(new List<string>() { "ExcelExport" }).AllowExcelExport(true).SortSettings(sort => sort.Columns(cols)).Render()

Let me know if you require additional information, thanks a mil in advance for your help. Looking forward to your replies.

Kind regards

Chris



7 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team January 22, 2020 01:53 PM UTC

Hi Chris , 
 
Greetings from Syncfusion support 
 
Based on your query you need to access the total row counts in the grid. Please follow the below methods 
 
To access the current page records use getCurrentViewRecords() method. 
 
 
var grid = document.getElementById("Grid").ej2_instances[0];      // Grid instance 
grid.getCurrentViewRecords(); 
 
                                                       
 
            
 
 
 
To access the full grid row data use the below method 
 
 
var grid = document.getElementById("Grid").ej2_instances[0];      // Grid instance 
grid.pageSettings.totalRecordsCount 
 
 
 
 
 
 
 
In your update you mentioned that you have changed the totalItemsInfo property for localization. In our grid component, we don’t have property like totalItemsInfo. So please share more details about the changes you have made on localization. 
 
Please get back to us with the following details 
 
Regards 
Prasanna Kumar N.S.V 



CR CR January 24, 2020 01:07 PM UTC

Dear Prasanna, 

Thanks a mil for getting back to regarding my query. To answer your question: I came across totalItemsInfo while analyzing the grid. I had previously added translations to ej.base.L10n.load({'de': { [...], 'totalItemsInfo': [...], that is why I tried to use that.

Thank you also for providing a suggestion about how to access the current records. Unfortunately, I am not quite sure where to put this snippet, since I got an error saying: Uncaught TypeError: Cannot read property '0' of undefined. 

I just put your code snippet in a new script section for testing before working with the value retrurned, e.g.

Do I need to define a function and access this function from the grid? Thanks a mil in advance for your suggestions.

Kind regards

Chris



PK Prasanna Kumar Viswanathan Syncfusion Team January 27, 2020 11:27 AM UTC

Hi Chris , 
 
Greetings from Syncfusion support 
 
Query 1 : Use totlItemsInfo in grid pager localization. 
 
To access the “totalItemsInfo” property of the pager you need to set the localization of text inside the pager function. In the below we have attached a sample and in that sample we have changed the totalItemsInfo property of the pager into some content. The whole customization are done under pager function.  
 
Please refer to the below code snippet for more reference. 
 
Index.cshtml 

@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col => 
         … 

}).AllowPaging().Locale("de").Height(500).Width("100%").PageSettings(page=> { page.PageCount(5); }).Render() 

<script> 

    ej.base.L10n.load({ 
            'de': {                                                           // in de function give the customization 
                'pager': {                                                 // pager function use to locale the pager components 
                    'currentPageInfo': '{0} von {1} Seiten', 
                    'totalItemsInfo': '({0} Beiträge)', 
                    'firstPageTooltip': 'Zur ersten Seite', 
                    'lastPageTooltip': 'Zur letzten Seite', 
                    'nextPageTooltip': 'Zur nächsten Seite', 
                    'previousPageTooltip': 'Zurück zur letzten Seit', 
                } 
    }); 

</script> 

 
 
 
Query 2 : How to access the current records of the grid. 
 
Here in the below sample, we have made a button click to access the records of the grid. In the button click function we have put the code for accessing the current records. 
 
Index.cshtml 


@Html.EJS().Button("full").Content("Full Grid Records").Render() 
 
@Html.EJS().Button("current").Content("Current Records").Render() 

@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col => 
… 
}).AllowPaging().PageSettings(page=> { page.PageCount(5); }).Render() 

<script> 

    document.getElementById("full").addEventListener('click', () => { 
        var grid = document.getElementById("Grid").ej2_instances[0];  
        var count = grid.pageSettings.totalRecordsCount; 
        console.log(count); 
    }); 

    document.getElementById("current").addEventListener('click', () => { 

        var grid = document.getElementById("Grid").ej2_instances[0]; 
        var num = grid.getCurrentViewRecords(); 
        console.log(num) 
    }); 

</script> 

 
 
 
Please get back to us if you need further assistance 
 
Regards, 
Prasanna Kumar N.S.V 



CR CR January 27, 2020 12:05 PM UTC

Dear Prasanna, 

Thanks a mil for your reply and providing some samples. I am sorry for not having phrased my requirements correctly. I had already managed to implement totalRecordsCount in the respective section of the grid when using the pagination and it is working well. 

Also thank you for the sample regarding Query2. I was indeed looking for a similar code as indicated in 

document.getElementById("full").addEventListener('click', () => { [...]

However, I do not require a button click event. I would like to automatically display an alert once a the amount of records / entries returned by grid.getCurrentViewRecords();  is less than a specific amount. Is there a way I could trigger that function each time records are loaded into the grid or the grid is being loaded? 

Please do not hesitate to get back in touch should you require additional information. 

Thank you very much in advance for reply.

Kind regards

Chris


PK Prasanna Kumar Viswanathan Syncfusion Team January 28, 2020 09:47 AM UTC

Hi Chris , 
 
We are happy to hear that your first issue has been resolved. 
 
Query : Show the alert message of current view records of the grid. 
 
For this query you need to go up with dataBound event. The dataBound event will be triggered when dataSource is populated in the Grid. In this event using getCurrentViewRecords method you can get the total record in the grid page.  
 
After that using if condition compare the records with the specific value and then enable the alert message. Kindly refer the code snippet and sample for more reference. 
 
Index.cshtml 


@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).DataBound("dataBound").Columns(col => 
                  

}).AllowPaging().Width("100%").PageSettings(page=> { page.PageCount(5); page.PageSize(10); }).Render() 


<script> 

    function dataBound() { 
        var grid = document.getElementById('Grid').ej2_instances[0]; 
        var num = grid.getCurrentViewRecords(); 
        console.log(num) 
        if (num.length < 15) {                                                                             // here consider 15 has the specific amount of records 
            alert("The current view Records is : " + num.length+ " It is less than the specific amount of 15");   // alert message 
        } 
    } 
</script> 

 
 
 
Please get back to us if you need further assistance 
 
Regards 
Prasanna Kumar N.S.V 



CR CR January 28, 2020 02:02 PM UTC

Dear Prasanna 

Thanks a mil for your detailed explanations and the code sample. Using that particular event worked. Please consider this matter / thread resolved. 

Thanks again for your help. 

Kind regards

Chris


PK Prasanna Kumar Viswanathan Syncfusion Team January 29, 2020 05:00 AM UTC

Hi Chris, 
 
We are happy to hear that issue has been resolved. 
 
Please get back to us if you need any further assistance. 
 
Regards, 
Prasanna Kumar N.S.V 


Loader.
Live Chat Icon For mobile
Up arrow icon