Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dynamically info to lookup property in material-table component

Im trying to add data dynamically to lookup property in Material-Table component, and I'm seeing issues.

The lookup is an object, and its definition you can find here in the first example https://mbrn.github.io/material-table/#/docz-examples-06-example-filtering

But if you tried to create that object outside and after that assign it to lookup you will get an error.

So, is there a way to assign an array of object to this lookup property ?

Thanks in advance for your time, any guidance will be appreciated.

Best Regards

like image 740
Orestes Avatar asked Apr 11 '19 15:04

Orestes


People also ask

How to add a lookup property to a table?

Display Lookup Table > Add Properties > Add Lookup Properties (the second radio button at the bottom) Some things I've noticed. 1. You can only add a lookup property if the lookup parameter being added does not have an action assigned. 2. Once you add the lookup property an action is assigned to the parameter.

Is there a material-UI table with search and filter?

However, the lines between search and filter are blurry. They are a UI presentation question as much as a data question. If you want a Material-UI Table with tons of built in functionality, take a look at material-table for React. Material-Table has tons of features that you wish came out-of-the-box with Material-UI’s Table component.

How to render a table with material-table in react?

To render a table with material-table, you have to supply the data (an array of objects) and the name of the columns to map with the data. The columns will specify which piece of data will go in which column. Let’s create a new file named BasicTable.jsx and add the following code: Nice!

How to search through the data in material-table?

We have already seen that, by default, material-table enables us to search through data. You can override the look and feel of the search bar by adding styles in the options.searchFieldStyle. If you don’t want to show the search bar for some reason, you will have to pass search : false, like so:


2 Answers

I found the way, it using Reduce, and it work perfectly: supposing that you have this array of object:

  const dinamicObject = [
  { id: 4, name: "name" },
  { id: 2, name: "Mehmet" },
  { id: 3, name: "middle" }
  ];

  var obj = dinamicObject.reduce(function(acc, cur, i) {
  acc[cur.id] = cur.name;
  return acc;
  }, {});

And then you assign that to your lookup property in Material-Table component

Check this for more clarification https://codesandbox.io/s/vq2lj0krkl

I hope this help to others

Best regards

like image 71
Orestes Avatar answered Oct 20 '22 19:10

Orestes


Create an object outside of the table.

import React from "react";
import ReactDOM from "react-dom";
import MaterialTable from "material-table";
import FilterList from "@material-ui/icons/FilterList";
import Clear from "@material-ui/icons/Clear";
import "./styles.css";

function App() {

  const dynamicLookupObject = { 34: "test1", 63: "test2" }

  // TODO: const createDynamicObject = () => {
       // change object
       // return dynamicLookupObject
     })

  return (
    <div className="App">
      <MaterialTable
        icons={{
          Filter: () => <FilterList />,
          ResetSearch: () => <Clear />
        }}
        columns={[
          { title: "Name", field: "name", defaultFilter: "Meh" },
          { title: "Surname", field: "surname" },
          { title: "Birth Year", field: "birthYear", type: "numeric" },
          {
            title: "Birth Place",
            field: "birthCity",
            lookup: dynamicLookupObject,
            defaultFilter: ["63"], // For numeric,
            emptyValue: () => <div>"dfsdf"</div>
          }
        ]}
        data={[
          { name: "Mehmet", surname: "Baran", birthYear: null, birthCity: 63 },
          {
            name: "Zerya Betül",
            surname: "Baran",
            birthYear: 2017,
            birthCity: 34
          }
        ]}
        title="Filtering Example"
        options={{
          filtering: true,
          maxBodyHeight: 300,
          rowStyle: data =>
            data.surname === "Baran"
              ? { background: "red" }
              : { background: "green" }
        }}
      />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
like image 41
Isaac Pak Avatar answered Oct 20 '22 19:10

Isaac Pak