Skip to content

How to pass an event from a button in columns file into data-table file? #5399

Closed Answered by alexdev888
alexdev888 asked this question in Q&A
Discussion options

You must be logged in to vote

The solution was to use meta of the table to pass an event:

{
  accessorKey: "Menu", 
  header: "Actions", 
  cell: ({ row, table, cell }) => { 
    return (<Trash2 onClick={() => {
      table.options.meta.onCustomEvent("delete", row)
    }} />) 
  }
}

also we need to extend our meta model

declare module "@tanstack/react-table" {
  interface TableMeta<TData extends RowData> {
    onCustomEvent: (eventName: string, rowData: Row<TData>) => void
  }
}

And in our useReactTable initialisation:

const table = useReactTable({
    data,
    columns,
    meta: {
      onCustomEvent: (eventName, rowData) => {
        if (eventName === "delete") {
          console.log("Edit row:", rowData.original)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by alexdev888
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant