I am using "material-table": "1.53.0", with the editable and onRowUpdate options, I want to update the data in my table. I am using axios to call the put method, I can see the tab data well but when modifying any field and click on save
I see the following error:
xhr.js:166 PUT http://localhost:xxxx/api/User/edit/?IdUser=1 404 (Not Found)
I realized that no matter what line I want to update, it always tells me that I am calling a 1 for the id and the IdUser is not being sent, but as I am doing it correctly
What is the way to get the idUser of the row?
import React, { useState, useEffect } from 'react';
import MaterialTable from 'material-table';
import axios from 'axios';
export default function UserTable() {
const url='/api/Users';
const [user, setUser]= useState({DataUser:[]});
useEffect(()=>{
const getUser=async()=>{
const response =await axios.get(url);
setUser(response.data);
}
getUser();
},[]);
return (
<MaterialTable
title="Users"
columns={[
{title: 'IdUser',field: 'IdUser', type: 'numeric', hidden:'false'},
{title: 'name',field: 'name'},
{ title: 'lastname', field: 'lastname' },
{ title: 'age', field: 'age', type: 'numeric' },
]}
data={user.DataUser}
options={{
filtering: true
}}
editable={{
onRowUpdate: (newData, oldData) =>
new Promise(resolve => {
setTimeout(() => {
resolve();
const data=[...user.DataUser];
data[data.indexOf(oldData)] = newData;
axios.put('api/User/edit/', newData,{
params: {
id:user.DataUser[0].IdUser
}
})
.then(res => console.log(res.DataUser));
setUser({...user,data});
}, 600);
}),
}}
/>
);
}
Instead of passing the first user id with user.DataUser[0].IdUser
, you should access the id from the user that is passed in the callback as newData
.
So change the axios call should do the trick:
editable={{
onRowUpdate: (newData, oldData) =>
new Promise(resolve => {
setTimeout(() => {
resolve();
const data=[...user.DataUser];
data[data.indexOf(oldData)] = newData;
axios.put('api/User/edit/', newData,{
params: {
id: newData.IdUser
}
})
.then(res => console.log(res.DataUser));
setUser({...user,data});
}, 600);