afridev
afridev

Reputation: 9

How to change accessorKey value based on row value

{
    accessorKey: "userId",
    header: ({ column }) => (
      <DataTableColumnHeader column={column} title="User Id" />
    ),
    cell: ({ row }) => (
      <label>{row?.original?.subRetailerId ?? row?.original?.retailerId}</label>
    ),
},

How to change accessorKey from userId to userId2 when retailerId exists

I am working on sorting it giving me accessorKey or id if exists so i want change accessorKey value from userId to userId2 when retailerId exists else it should be userId

Upvotes: 0

Views: 83

Answers (1)

sds-smith
sds-smith

Reputation: 151

You will need to handle this in your data. Assuming each Obj in the data array has a userIduserId2, and retailerId, where retailerId may have no value, you could do something like this: data.map(row =>({ ...row, userIdColumn: row.retailerId ? row.userId2 : row.userId}) and pass the mapped result into useReactTable as data. Then in columns, change accessorKey: "userId" to accessorKey: "userIdColumn"

Upvotes: 0

Related Questions