To show row number column

Hi Syncfusion team,

Is it possible to add a column for showing the row number. Number need to be refreshed after sorting, filtering, grouping data and changing page.


Regards,

Kuan Long


5 Replies

RS Rajapandiyan Settu Syncfusion Team September 1, 2022 02:07 PM UTC

Hi Kuan,


Thanks for contacting Syncfusion support.


Query: Is it possible to add a column for showing the row number.


You can show the serial number of the row by using following code in the rowDataBound event of Grid.


rowDataBound: https://ej2.syncfusion.com/javascript/documentation/api/grid/#rowdatabound


 

[index.cshtml]

 

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowFiltering="true" allowPaging="true" allowSorting="true" rowDataBound="rowDataBound" height="300">

    <e-grid-columns>

        <e-grid-column field="SerialNo" headerText="Serial NO" allowFiltering="false" allowSorting="false" width="100"></e-grid-column>

        <e-grid-column field="OrderID" headerText="Order ID"  textAlign="Right" width="100"></e-grid-column>

        <e-grid-column field="CustomerID" headerText="Customer ID" width="120"></e-grid-column>      

        <e-grid-column field="ShipCity" headerText="Shio City"   width="100"></e-grid-column>

        <e-grid-column field="ShipName" headerText="Ship Name" width="100"></e-grid-column>

    </e-grid-columns>

</ejs-grid>

 

<script>

    function rowDataBound(args){

        // bind the index of the record with respect to current page

        args.row.children[0].innerText = parseInt(args.row.getAttribute('aria-rowindex')) + (this.pageSettings.currentPage - 1) * this.pageSettings.pageSize;

    }

</script>

 


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/core_grid_rowindex_currentpage719764207.zip

Note: The provided solution shows the index based on the current page. If you want to refresh the serial number with respect to sorting, filtering, and grouping, we suggest you to maintain a separate field in the dataSource to store the index of records.


Please get back to us if you need further assistance with this.


Regards,

Rajapandiyan S



KL Kuan Long Khiu September 2, 2022 01:59 AM UTC

Hi  Rajapandiyan,


Thanks for your reply. The solution works fine in pagination and searching. However, serial number column shows blank in grouping and exporting. Please help. Thanks.



Regards,

Kuan Long




RS Rajapandiyan Settu Syncfusion Team September 2, 2022 01:52 PM UTC

Hi Kuan,


Thanks for your update.


Query: The solution works fine in pagination and searching. However, serial number column shows blank in grouping and exporting.


By using queryCellInfo, pdfQueryCellInfo and excelQueryCellInfo events, you can achieve all your requirements.


queryCellInfo: https://ej2.syncfusion.com/javascript/documentation/api/grid/#querycellinfo

pdfQueryCellInfo: https://ej2.syncfusion.com/javascript/documentation/api/grid/#pdfquerycellinfo
excelQueryCellInfo: https://ej2.syncfusion.com/javascript/documentation/api/grid/#excelquerycellinfo


 

[index.cshtml]


<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowFiltering="true" allowPaging="true" allowGrouping="true" allowPdfExport="true" allowExcelExport="true" allowSorting="true" toolbarClick="toolbarClick" toolbar="@(new List<string>() { "PdfExport", "ExcelExport"})" queryCellInfo="queryCellInfo" pdfQueryCellInfo="pdfQueryCellInfo" excelQueryCellInfo="excelQueryCellInfo" height="300">

    <e-grid-columns>

        <e-grid-column type="checkbox" width="100"></e-grid-column>

        <e-grid-column field="SerialNo" headerText="S/NO" allowFiltering="false" allowSorting="false" width="100"></e-grid-column>

    </e-grid-columns>

</ejs-grid>

 

<script>

    var serialNo;

    function queryCellInfo(args){

        if ( args.column.field=="SerialNo" ) {

            // bind the index of the record based on current page

            args.cell.innerText = parseInt(args.cell.getAttribute('index')) + 1 + (this.pageSettings.currentPage - 1) * this.pageSettings.pageSize;

        }       

    }

    function toolbarClick(args) {

        var gridObj = document.getElementById("Grid").ej2_instances[0];

        if (args.item.id === 'Grid_excelexport') {

            serialNo = 0; // initialize the serial No

            gridObj.excelExport();

        }

        if (args.item.id === 'Grid_pdfexport') {

            serialNo = 0; // initialize the serial No

            gridObj.pdfExport();

        }

    }

    function excelQueryCellInfo(args){

        if (args.column.field == "SerialNo") {

            args.value = serialNo + 1; // bind the serial No in the excel cell

            serialNo++; // increase the serial No

        }

    }

    function pdfQueryCellInfo(args){

        if (args.column.field == "SerialNo") {

            args.value = (serialNo + 1).toString(); // bind the serial No in the pdf cell

            serialNo++; // increase the serial No

        }

    }

</script>


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/core_grid_serialNo_column1736166721.zip

Please get back to us if you need further assistance.


Regards,

Rajapandiyan S



KL Kuan Long Khiu September 5, 2022 02:15 AM UTC

Hi  Rajapandiyan,


It works perfectly fine. Thanks.


Regards,

Kuan Long



RS Rajapandiyan Settu Syncfusion Team September 5, 2022 05:19 AM UTC

Hi Kuan,


We are happy to hear that you have achieved your requirement with the solution provided.


Please get back to us if you need further assistance.


Regards,

Rajapandiyan S


Loader.
Up arrow icon