1

I want to execute a function before the add row editable opens. enter image description here Before this UI opens up I want to execute a function. (like console.log('Hello')) This is my current material table code.

            <MaterialTable
                title="Timesheet"
                columns={columns}
                data={DataArray}
                icons={tableIcons}
                editable={{
                onRowAddCancelled: rowData => console.log('Row adding cancelled'),
                onRowUpdateCancelled: rowData => console.log('Row editing cancelled'),
                onRowAdd: newData =>
                    new Promise((resolve, reject) => {
                        
                        setTimeout(() => {
                            // add row
                        }, 1000);
                    }),    

                }}
                options={{
                    actionsColumnIndex: -1,
                    addRowPosition: "first",
                    search:false,
                    exportButton: true,
                    
                }}
                
            />
2
  • 1
    What is not working? Could you please elaborate? Commented Oct 19, 2021 at 20:05
  • OK. There is a + button on top which enables me to add rows. But when i click on the button i want it to execute a function (maybe console.log('Hello') ) then execute the add row function. Is it clear enough? Commented Oct 20, 2021 at 7:47

1 Answer 1

1

You can put the code you want to execute in the onRowAdd callback function, for example

    <MaterialTable
      title="Timesheet"
      columns={columns}
      data={DataArray}
      icons={tableIcons}
      editable={{
        onRowAddCancelled: rowData => console.log('Row adding cancelled'),
        onRowUpdateCancelled: rowData => console.log('Row editing cancelled'),
        onRowAdd: newData => {
            console.log('Hello');
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    // add row
                }, 1000);
            });  
        }
      }}
      options={{
        actionsColumnIndex: -1,
        addRowPosition: "first",
        search:false,
        exportButton: true,
      }}
    />
  );

If you want to call your function on the + button click, you need to add the actions. For example

    <MaterialTable      
      actions={[
        {
          icon: 'add',
          tooltip: 'Add User',
          isFreeAction: true,
          onClick: (event) => alert("You want to add a new row")
        }
      ]}
    />
Sign up to request clarification or add additional context in comments.

2 Comments

This console.log('Hello') is executed when I add data to the row and click save. I want to execute the console.log('Hello') when I click on the + button but before the add row opens.
@SohanReddy where is the code for the + button? I don't see it in the code that you provided. In MaterialTable you can add buttons to the table toolbar by using the actions property. Read more here material-table.com/#/docs/features/actions

Your Answer

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