Is it possible to update the data in a table row and then transfer it to the backend API?

Can someone assist me with updating a record in the database through the front-end table display? Here is a screenshot for reference: https://i.stack.imgur.com/CPLN6.png

I need the entire record to be updated in the backend once I click the save button on the displayed record. Below is the code snippet:

export default function AdminXnaLog() {
  const [data, setData] = useState([]); //table data
var columns = [
    {title: "id", field: "_id", hidden: true},
    {title: "Member No", field: "memberno"},
    {title: "Name", field: "name"},
    {title: "Surname", field: "surname"},
    {title: "XNA Description", field: "description"},
    {title: "XNA Withdraw", field: "xnawithdraw"},
    {title: "XNA Deposit", field: "xnadeposit"},
    {title: "XNA Balance", field: "balance"},
    {title: "Date", field: "date"}
    ]
useEffect(() => {
        async function fetchBooks() {
          const response = await fetch('/xnalogget');
          const json = await response.json();
          setData(json.xl);
          console.log(json.xl)
      }
      fetchBooks();
  },[]);
return (
    <MaterialTable
  title="XNA Logs"
  columns={columns}
  data={data}
   editable={{
    onRowUpdate: ()=>handleRowUpdate()
  }}
/>  
  )
}

I am struggling to implement the handleRowUpdate function. Is there a way I can pass the current row id and updated data to the PUT API in the backend for updating? The backend should locate the row by id and update the corresponding database record.

Answer №1

Revision on the fifth-to-last line: Earlier: onRowUpdate: ()=>handleRowUpdate() New Version: onRowUpdate: (oldData, newData)=>handleRowUpdate(oldData, newData)

When updating the function, you will receive both the new and old data, allowing you to make an api call.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm sorry, but we were unable to locate the /bin/sh

After running a command using execSync that runs with sh, I observed the following: spawnSync /bin/sh ENOENT bin is now included in the PATH environment variable. Any ideas on this issue? ...

What is the best method for updating the state of a dynamically created Switch component using React's setState()?

I have a dynamic creation of Switches based on a map like shown in the image: https://i.stack.imgur.com/jDLbS.png For example, using this code: const [enabled, setEnabled] = useState(false) return( ... {people.map((person) => ( ... ...

Convert your Express.js API routes to Next.js API routes for better performance and flexibility

I'm currently working on an Express API route that looks like this: const router = require("express").Router(); router.get("/:jobId/:format", async (req, res) => { try { const { jobId, format } = req.params; if (form ...

I'm having trouble locating the airtable module, even after I successfully ran npm install airtable

Currently attempting to integrate the airtable api into my website's backend using node.js. However, upon writing var Airtable = require('airtable'); and running the file with node [filepath], I encounter an error in the command prompt: ...

Need help resolving this issue? (node:4) UnhandledPromiseRejectionWarning: Error: ETELEGRAM: 400 Bad Request: unable to locate chat

Setting up a telegram bot and encountering an error after entering all the data. Attempting to reload. Confusion about where to insert a specific code fragment - perhaps here. An issue occurred while trying to input the code. Error in getChatMember { E ...

NextJs Route Groups are causing issues as they do not properly exclude themselves from the app's layout.tsx

As far as I know, the layout.tsx in the app directory serves as the root layout. To customize the layout structure for specific segments, you can use Route Groups. More information can be found here. In this setup, any page.tsx file inside a directory nam ...

``The presence of symlink leading to the existence of two different versions of React

Currently, I am working on a project that involves various sub custom npm modules loaded in. We usually work within these submodules, then publish them to a private npm repository and finally pull them into the main platform of the project for use. In orde ...

Issues with routing in NodeJS Express causing routes to not be called

I've been working on setting up a basic REST API using nodeJS. However, I am facing an issue where none of the endpoints are being called when I try to access them. Can someone guide me on what changes I need to make in order to get it working properl ...

Verify whether an item exists within a nested array in ReactJS

Here is the dataset that I have: Data: data: { id:1 groups:[ {id:1 , name: john, permissions : [{id:1 , codename="can_edit"},{id:2,codename="can_write"},{id:3,codename="can_delete"}]} , ...

Why is it that I am limited to running globally installed packages only?

Recently, I made the switch to Mac iOS and encountered an issue while setting up a new TypeScript backend project. All npm packages seem to be not functioning properly in my scripts. Cannot find module 'typescript/bin/tsc' Require stack: - /Users ...

Tips for accessing the PR number in a Node.js GitHub Probot listening for the `pull_request` event

I've recently developed a GitHub probot application using nodejs and typescript. Currently, I have set up an event listener for the pull_request event. How can I extract the pr_number from the context object within the probot? The snippet below is fr ...

The initial state in NextJS fails to update when routing to the same page with different parameters

This particular project utilizes NextJS The structure of my page's URL is as follows: localhost:3000/first/second When I invoke getInitialProps on this page, the data retrieved from it is passed to the component and used to set the initial state li ...

It appears that the Node npm installation is deploying a distinct release compared to the one found on GitHub

On GitHub, I have noticed the desired version of generator-webapp; however, after performing an npm install, it appears to be providing me with an outdated version. I have observed that the version number in the packages.json file has not been modified du ...

Implementing SVG in React Component

I recently downloaded an SVG image with the intention of using it as a React component. However, when I pasted the SVG code into the functional component, I encountered error markers within the tags. What can I do to fix this issue so that I can successf ...

The spy function in the React test refuses to trigger

Having a managed component with a form containing two radio buttons and a text input. There's a function triggered by the onChange event of the text input, and a test is set up to simulate a change event. Despite expecting the spy function to be calle ...

How can localization be achieved in an express app using i18n + handlebars without using it to generate output HTML?

I have an Express application where I need to utilize Handlebars for creating localized email templates. The challenge here is that Handlebars necessitates a globally registered helper to translate elements within the .hbs files, while I require using a co ...

What are the steps to showcase JSON data from a Node.js server on an HTML page through AJAX with the use of the GET method

I'm currently utilizing a Node.js server accessed from this URL: http://localhost:5000/listproducts. The data I have is as follows: [{"Id":1,"Name":"New Product","Description":"P1 desc","Quantity":1},{"Id":2,"Name":"Product2","Description":"p2 desc", ...

React - The ._id received by the Modal inside the map function is incorrect

My map is generating edit and delete buttons. The delete button requires confirmation, so I implemented a modal. When I use console.log(additive._id) on the first delete button, I get the correct ._id, but when I click the confirm button inside the modal, ...

What is the best way to reduce the spacing between bullets in a JoyUI List?

I have some lists on this page: However, I am looking to reduce the spacing between the bullets. Despite my attempts at adjusting the theme, I haven't been successful. Here is what I have tried so far: import { extendTheme } from "@mui/joy/style ...

Managing pagination in Node.js applications

I'm having trouble retrieving a specific number of rows from my database. I've been examining the query code, but can't seem to identify what might be causing the issue. Here's an excerpt of the code in question: var num_rows = offset ...