Interactive MaterialTable editing with dropdown selection

I'm currently tackling the challenge of creating a MaterialTable that includes an inline editable field with a dropdown feature. The hurdle I am facing appears to lie within the columns object. By utilizing the lookup attribute, it is possible to define key:value pairs as options for the dropdown list. However, my issue arises from the inability to dynamically iterate over a list and include the key-value pairs in the desired manner. It seems to only function properly when formatted like lookup:{ 1: "Test Value", 2: "Test Value 2" }. Could you please clarify if my interpretation is inaccurate.

<MaterialTable
    title="Available Attributes"
    icons={this.tableIcons}
    data={availableAttributesList}
    columns={[
      { title: 'Attribute', field: 'name' },
      {
        title: 'Data Type',
        field: 'dataType',
        lookup: { dataTypePayload.map((attribute, name) => ({
         attribute.id : attribute.dataType
        }))}
      }
    ]}
    options={{
      actionsColumnIndex: -1
    }}
    editable={{

      onRowAdd: newData =>
        new Promise((resolve, reject) => {
          setTimeout(() => {
            {
              const data = editableAvailableAttributesList;
              data.push(newData);
              this.setState({ data }, () => resolve());
            }
            resolve();
          }, 1000);
        }),
      onRowUpdate: (newData, oldData) =>
        new Promise((resolve, reject) => {
          setTimeout(() => {
            {
              const data = editableAvailableAttributesList;
              const index = data.indexOf(oldData);
              data[index] = newData;
              this.setState({ data }, () => resolve());
            }
            resolve();
          }, 1000);
        }),
      onRowDelete: oldData =>
        new Promise((resolve, reject) => {
          setTimeout(() => {
            {
              let data = availableAttributesList;
              const index = data.indexOf(oldData);
              data.splice(index, 1);
              this.setState({ data }, () => resolve());
            }
            resolve();
          }, 1000);
        })
    }}
  />

Answer №1

Using the map function, you are creating an array of objects that are then placed within another object.

If you have noticed that this method is not working as expected, try the following code snippet:

<MaterialTable
  // ...other props
  columns={[
    { title: 'Attribute', field: 'name' },
    {
      title: 'Data Type',
      field: 'dataType',
      lookup: dataTypePayload.reduce((acc: any, attribute: any) => {
        acc[attribute.id] = attribute.dataType
        return acc
      }, {})
    }
  ]}
  // ...other props
/>

I hope this resolves your issue!

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

Step-by-step guide to accessing the detail view upon selecting a table row in react.js

In my project, I have a table with multiple rows. When a row is selected, I want to display detailed information about that particular item. To achieve this functionality, I am utilizing react-router-dom v6 and MUI's material table component. Here is ...

The data that was received on the Express backend appears to have been altered from how it was originally sent from the React frontend

I am facing an issue while attempting to send a post request from my React front end to my Express back end. The object I am trying to receive is showing up as the key of another object with an empty string value. Below is my onSubmit React function: han ...

Is there a way to extract shared properties within a Material UI style declaration?

I am working with a specific set of style rules that define different statuses: status_in_progress: { fontSize: 14, backgroundColor: theme.palette.success.dark, padding: 2, paddingLeft: 5, paddingRight: 5, display: "inline-bl ...

Preparing my JSON data for visualization on a chart

I have successfully retrieved data using this API, but now I need to transform it into a chart within my react project. As a newcomer to JS and React, I am struggling to create a chart with the JSON data. My objective is to display prices by bedrooms over ...

Delivery person receiving biscuit but my internet browser doesn't seem to be getting it when I attempt to retrieve it

Currently, I am in the process of creating a website using Flask and React. The user is required to input an email and password on a form, which is then sent via axios.post to the backend. The backend checks if the email and password match with the databas ...

Utilize Material UI links in React JS to connect to another component seamlessly

I'm completely new to React and have been struggling for hours to navigate to another component that I've created. My goal is to link my login form to the sign-up page once a Material UI link is clicked, but I just can't seem to get it to fu ...

How can I pass props from a page to components in Next.js using getServerSideProps?

Struggling to fetch the coingecko-api for accessing live bitcoin prices. Trying to pass return props of getServerSideProps to my <CalculatorBuy /> component within the <Main /> component. Facing issues when importing async function in calcula ...

Managing errors and error codes in React/Redux applications

I am currently exploring the most effective approach for managing errors, particularly when deciding between error codes and an errors object in response from my API. As I dive into building a multi-part form using react, each time a user progresses to th ...

Using the combination of Nextjs, rsuitejs, and CSS modules ensures that no CSS files will be

In my development work, I have been utilizing Next.js in conjunction with RSuiteJS. During the development phase, everything is working smoothly without any issues. However, after building the project, I have encountered a problem where the library CSS fi ...

The MUI Datagrid's rendercell method is returning an undefined value when calling

After deploying my code to production, I encountered an issue where the getValue function is no longer accessible inside the renderCell method. The environment matches the development environment. Below are the column definitions: const columns = [ ...

react-hook-form replaces the onChange function causing delays in updating the value

Recently, I created a unique Select component utilizing useState and onChange. I attempted to integrate this custom component with the powerful react-hook-form. Allow me to share the code snippet for the bespoke Select component. const Select = forwardRef ...

Testing URL query strings in a React application can be done in a few simple

Can someone help me figure out how to properly test the variable values from the code snippet below? I keep encountering this error message: TypeError: Cannot read property 'search' of undefined export class myView extends React.Component { ...

Steps for triggering a logout using MSAL2Provider

Currently, I am incorporating the React Login Microsoft-Graph-Toolkit component (shown in the code snippet below) along with MSAL2Provider to facilitate user login functionality in my Active Directory application. Although this setup is functioning smoothl ...

Using the .get() method to retrieve Firebase documents results in an error message saying "'is not a function'"

I'm currently attempting to retrieve all the documents from a specific collection in Firebase using the following code snippet: const userCollectionRef = collection(db, currentUser?.uid) const snapshot = await userCollectionRef.get() for (const doc of ...

Exploring the depths of routing with React Router DOM Version 6

Hello everyone, I trust you are all doing well. Recently, I completed a project using React and created a demo here. The issue I'm facing is related to routing. My intention is to display the child route under its parent but unfortunately, I haven&apo ...

Troubles encountered with adapting apexcharts size within a react environment

As a novice front-end coder transitioning from a data analyst background, I am currently facing an issue with integrating an ApexChart visual into a flexbox element. The visual appears fine at a resolution of 2560x1440 pixels. However, upon further resizin ...

Encountered an "next: not found" error when attempting to launch nextjs standalone within Azure Web App

I am having issues deploying my application built with Next.js on an Azure Web App. Despite following the guidelines provided by Microsoft, I encountered a problem while trying to deploy. After compiling the application with the "standalone" flag as instru ...

Implementing a CSS codepen within a React Material-UI component

I'm struggling to implement this effect on my material ui card component using react and makeStyles. I am having difficulty translating this effect to the cards, could someone assist me in simplifying it into a CSS code that can be used in MakeStyles? ...

What is the best approach for generating individual variables for every row in a React table?

Presently, I am developing a dynamic table using react and the structure is as follows: <TableBody> {finalLoadedTokensData.map((loadedToken, index) => { const isItemSelected = isSelected(loadedToken.contract); const label ...

What is the process for downloading a package from unpkg or adding an unpkg package to dependencies?

Challenge I attempted to install IPFS from the unpkg website. https://www.unpkg.com/browse/[email protected] / My Project This is a project built with React. Unfortunately, I am having trouble downloading this package or installing it in my packa ...