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

Dependent columns

Open tudro opened this issue 7 years ago • 23 comments

Hello,

First thank you for your great work :)

I'm interested if it's possible to change the value of a column based on another one. An example: let's say i have 2 columns, A and B, both dropdowns. I would like that the options available in column B change depending on what i select in column A.

I tried using the editComponent and props.rowData, but this rowData doesn't seem to update according to the changes. The values in it are the same from the moment you enter edit mode. So if i change the value in column A, it doesn't update the rowData.

Thank you

tudro avatar May 23 '19 09:05 tudro

I've figured it out. In case anybody is interested: in the onChange of my custom select defined for the editComponent of column A, i check the selected value and then update the state accordingly for the lookup of column B. Maybe no the best solution, but it works for my case :)

tudro avatar May 23 '19 10:05 tudro

Hi, sorry, i'm still having problems with this for some cases. For example, if i have column A as dropdown and column B as a text field. Depending on what i select in column A, i would like to automatically fill in column B. Is this possible?

Thank you

tudro avatar May 28 '19 13:05 tudro

if you use editComponent for both A and B you can. You will call onChange of B when value of A changed.

mbrn avatar May 28 '19 17:05 mbrn

Thank you for the help, but i can't get it right. How can i access the props of B in the onChange of A. I mean, when A changes, i have to call the onChange of B, but with B's props, otherwise i can't update the B field, right?

Thank you

tudro avatar May 29 '19 08:05 tudro

It tried set props of B to component (this.propsB). But when i call double onChange in for A one of them is not working properly.

So i added onRowDataChange to props of editComponent. This is usage. You can use it for next release. I will publish it tomorrow.

 columns: [
      {
        title: 'Adı', field: 'name', editComponent: props => {
          return (
            <input
              value={props.value}
              onChange={e => {
                var data = { ...props.rowData };
                data.name = e.target.value;
                data.surname = e.target.value.toLocaleUpperCase();
                props.onRowDataChange(data);
              }}
            />
          )
        }
      },
      {
        title: 'Soyadı', field: 'surname', editComponent: props => {
          this.inputBProps = props;
          return (
            <input
              value={props.value}
              onChange={e => props.onChange(e.target.value)}
            />
          )
        }
      },

mbrn avatar May 29 '19 08:05 mbrn

This is done with version 1.39.0

mbrn avatar May 30 '19 04:05 mbrn

After upgrading to version 1.39.0 when start row edit, in browser console it generate warning:

index.js:1437 Warning: Unknown event handler property `onRowDataChange`. It will be ignored.
    in div (created by ForwardRef(FormControl))
    in ForwardRef(FormControl) (created by WithStyles(ForwardRef(FormControl)))
    in WithStyles(ForwardRef(FormControl)) (created by ForwardRef(TextField))
    in ForwardRef(TextField) (created by WithStyles(ForwardRef(TextField)))
    in WithStyles(ForwardRef(TextField)) (created by MTableEditField)
    in MTableEditField (created by MTableEditRow)
    in td (created by ForwardRef(TableCell))
    in ForwardRef(TableCell) (created by WithStyles(ForwardRef(TableCell)))
    in WithStyles(ForwardRef(TableCell)) (created by MTableEditRow)
    in tr (created by ForwardRef(TableRow))
    in ForwardRef(TableRow) (created by WithStyles(ForwardRef(TableRow)))
    in WithStyles(ForwardRef(TableRow)) (created by MTableEditRow)
    in MTableEditRow (created by MTableBody)
    in tbody (created by ForwardRef(TableBody))
    in ForwardRef(TableBody) (created by WithStyles(ForwardRef(TableBody)))
    in WithStyles(ForwardRef(TableBody)) (created by MTableBody)
    in MTableBody (created by Droppable)
    in table (created by ForwardRef(Table))
    in ForwardRef(Table) (created by WithStyles(ForwardRef(Table)))
    in WithStyles(ForwardRef(Table)) (created by Droppable)
    in div (created by Droppable)
    in div (created by Droppable)
    in Droppable (created by ConnectFunction)
    in ConnectFunction
    in ConnectFunction (created by MaterialTable)
    in div (created by ScrollBar)
    in ScrollBar (created by MaterialTable)
    in div (created by ForwardRef(Paper))
    in ForwardRef(Paper) (created by WithStyles(ForwardRef(Paper)))
    in WithStyles(ForwardRef(Paper)) (created by Container)
    in Container (created by MaterialTable)
    in Provider (created by App)
    in App (created by ErrorBoundary)
    in ErrorBoundary (created by DragDropContext)
    in DragDropContext (created by MaterialTable)
    in MaterialTable
    in Unknown (created by WithStyles(Component))
    in WithStyles(Component) (at Applications/index.tsx:116)
    in div (at Applications/index.tsx:114)
    in ApplicationsComponent (created by WithStyles(ApplicationsComponent))
    in WithStyles(ApplicationsComponent) (created by Context.Consumer)
    in Route (created by PrivateRoute)
    in PrivateRoute (at App.tsx:65)
    in Switch (at App.tsx:57)
    in Router (created by BrowserRouter)
    in BrowserRouter (at App.tsx:48)
    in App (at src/index.tsx:12)

BVH77 avatar Jun 01 '19 02:06 BVH77

I am also seeing "Warning: Unknown event handler property onRowDataChange. It will be ignored." after upgrading to 1.39.0

Kove11 avatar Jun 04 '19 23:06 Kove11

This problem remains though upgrading to version 1.39.0

AugustoCalaca avatar Jun 06 '19 12:06 AugustoCalaca

What progress on this issue? I think, somewhere should be defined prop type on this, but I’m not sure where. Very annoying warning...

BVH77 avatar Jun 10 '19 13:06 BVH77

I will start to working on MT at tomorrow (1 hour daily). So this would be resolved in 1 week.

mbrn avatar Jun 13 '19 07:06 mbrn

Before I read that there will be a fix in a week I wrote a small hack to get rid of the error when there are no editComponent defined for a column. So far, only for string type but more can be added. Hopefully, this code can help others to get rid of the error until a real fix is in place.

The code is written in TypeScript.

  import React, { ChangeEvent } from "react";
  import { EditComponentProps } from "material-table";

  interface RealEditComponentProps extends EditComponentProps {
    onRowDataChange?: (data: []) => void;
  }
  const cleanColumns = columns.map(column => {
    if (!column.editComponent) {
      /**
       * Without defining a custom component will lead to:
       * 1. the material-table default editComponents will send the onRowDataChange as an
       * attribute to the DOM element and that results in annoying errors in the developer
       * console.
       */
      column.editComponent = ({
        value,
        columnDef,
        rowData,
        onRowDataChange
      }: RealEditComponentProps) => {
        const props = {
          value: value || "",
          onChange: (event: ChangeEvent<HTMLInputElement>) => {
            const newRowData = Object.assign(rowData, {
              [columnDef.field]: event.target.value || "",
            });
            onRowDataChange && onRowDataChange(newRowData);
          }
        };

        return <input {...props} />;
      };
    }

    return column;
  });

robinlarsson avatar Jun 15 '19 10:06 robinlarsson

Any update on this issue @mbrn ?

Kove11 avatar Jun 28 '19 21:06 Kove11

Hi, this problem still persist on ^1.40.0

mrvncaragay avatar Jun 30 '19 04:06 mrvncaragay

Сonfirm this error on version 1.40 How to fix it?

PiAlexay avatar Jul 05 '19 12:07 PiAlexay

Any news about that issue? 🧐

rommelmamedov avatar Jul 21 '19 19:07 rommelmamedov

It's been a month since the last merge on this repo -- I think the mainter(s) are on vacation

0x-2a avatar Jul 26 '19 20:07 0x-2a

Hi everyone,we will fix asap Thanks

ekaradev avatar Aug 01 '19 12:08 ekaradev

is there any update of this issue?

hungbang avatar Oct 03 '19 15:10 hungbang

is there any update of this issue?

The onRowDataChange function is working well in version 1.51.0

andykwanfai avatar Dec 10 '19 08:12 andykwanfai