Tabulator provides a built‐in row number formatter that makes this easy. You just need to add a column with the formatter set to "rownum". This built-in formatter automatically updates the row numbers when the data is sorted, filtered, or otherwise rearranged.
Below is an example of how you might set up your table to include a serial number (SNo) column:
js
// Sample data for the table
var tabledata = [
{id:1, name:"John", age:25},
{id:2, name:"Jane", age:30},
{id:3, name:"Steve", age:28},
];
// Create Tabulator on the DOM element with id "example-table"
var table = new Tabulator("#example-table", {
data: tabledata,
layout: "fitColumns", // optional layout setting
columns: [
// SNo Column using the built-in rownum formatter:
{
title: "SNo", // column title
formatter: "rownum", // use the rownum formatter
width: 60, // fixed width
align: "center", // center the content
headerSort: false // disable header sorting for this column
},
{title: "Name", field: "name", sorter: "string", width: 150},
{title: "Age", field: "age", sorter: "number", width: 100}
]
});
How It Works
formatter: "rownum": This tells Tabulator to automatically generate a row number for each row.
Automatic Updates: When you sort or filter your data, Tabulator will reassign the row numbers so that they always reflect the current order of rows.
Other Options: You can adjust the width, alignment, or disable sorting on the SNo column as needed.
This setup will generate the row number across various table operations, making sure your SNo column stays in sync with the current table view regardless of sorting or filtering changes.