1

I have a requirement to display row index for every row in my table. I see two options:

  • Server side data loading
  • Using customRowRender

Both options have some downsides. And I am surprised this feature doesn't come out-of-the box.

Are there some other options? I would prefer to be able to load all data into the browser, but with customRowRender I won't be able to keep original row styling...

2
  • Did you check this :- github.com/gregnb/mui-datatables Hope this will help in your case. Commented Jan 22, 2020 at 5:46
  • Yes, I checked. Even looked at source code. Commented Jan 22, 2020 at 5:48

2 Answers 2

2

The solution is to add an extra column to a source dataset with undefined or empty string value and then to use a custom cell body render function:

export function customRowIndexColumn() {
    return ({
        name: '#',
        options: {
            filter: false,
            customBodyRender: (value: any, meta: MUIDataTableMeta) => {
                return (
                    meta.rowIndex + 1
                );
            }
        }
    })
}
....

    const columns: MUIDataTableColumnDef[] = [
        customRowIndexColumn(),
        {
            name: 'Plane ID',
            options: {
                filter: false
            }
        },
        {
            name: 'Name',
            options: {
                filter: false
            }
        },
...
Sign up to request clarification or add additional context in comments.

Comments

0

use customBodyRender pass rowIndex and dataIndex, then access dataIndex.rowIndex it has worked for me

        name: "id",
        label: "#",
        options: {
            filter: true,
            sort: true,
            customBodyRender: (rowIndex, dataIndex) => dataIndex.rowIndex + 1 
        }
    },

increment with 1 to start at position 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.