material-table icon indicating copy to clipboard operation
material-table copied to clipboard

how to add new row data manually?

Open haqisaurus opened this issue 7 years ago • 5 comments

I want to add new row data from outside, not from feature that provide by material-table, I did something like this

actions={[          
                        {
                            icon: 'add',
                            tooltip: 'Add Supplier',
                            isFreeAction: true,
                            onClick: (event) => {
                                // this.props.toggleFormSupplier()
                                this.tableRef.current.setState({data: [...this.tableRef.current.state.data, {_id: "5cd7a3405f2fb02d56479687", name: "test l", phone: "0878653766", address: 'this is address'}]})
                                console.log(this.tableRef.current.renderData)
                                this.tableRef.current.onQueryChange();
                            }
                        }
                    ]}

this code purpose to add new row directly but when echo-ing at console data added but not rendered? someone can help me out this case?

haqisaurus avatar May 12 '19 06:05 haqisaurus

Hi @haqisaurus

  1. Store your data on your state.
  2. Add a new row to array that stored on state.
  3. Just setState

mbrn avatar May 12 '19 16:05 mbrn

Hi @haqisaurus

  1. Store your data on your state.
  2. Add a new row to array that stored on state.
  3. Just setState

I use data from remote API not from array state, is there a function to trigger reload data from API server?

haqisaurus avatar May 12 '19 23:05 haqisaurus

Whatever triggers you’re remote call for new data should set the state and you’re all set. Could easily be a custom action inside material-table. I’m using custom actions and redux with redux thunk for custom search, add,modify, delete all works great with reducers updating the state.

priemd avatar May 13 '19 01:05 priemd

@haqisaurus

if you use data and you want to add new row please check editable feature. It lets you to create a new row with a form.

But if you want to refresh data you can use tableRef and call onQueryChange manually.

mbrn avatar May 13 '19 04:05 mbrn

@mbrn

Okey I got it now, thank you for helping me 👍

haqisaurus avatar May 13 '19 08:05 haqisaurus