2

I have list of texts that are to be displayed inside a data-table using the MUI-Data-Table component but i am facing this problem of displaying not a single item in the column but a list of items. I have an item called system. It contains an array of countries so I want to display that array inside only one column but i don't know how to do it.

this is my json :

systeme:{
          archived: 0
           id: 1
           nomSysteme: "Environnement Tn"
           systeme_country: Array(1)
                   {
                    0: {id: 1}
                   length: 1
                     }
        }

and this is how i customize my column because there is a certain pattern that i need to follow :

 {
    name: "titre",
    label: "Titre",
    options: {
        filter: true,
        sort: false,
    }
},
{

    name: "theme.systeme.systeme_country",
    label: "Countries",
    options: {
        filter: true,
        sort: false,

    }
},

For more explanation the option name must be the same as in the json and it renders only one object but the theme.systeme.system_country is an array of countries's ids that i want to display but it won't let me i didn't know how to customize my columns to do such a thing or if there is a trick that i can do to make it happen

2 Answers 2

6

You could provide a customBodyRender for the specified field. An example could be found here: https://github.com/gregnb/mui-datatables/blob/master/examples/component/index.js

import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import MUIDataTable from "mui-datatables";

function App() {
  const data = [{ user: "john", langs: ["en", "de", "fr"] }];
  const columns = [
    { name: "user" },
    {
      name: "langs",
      options: {
        customBodyRender: (value, tableMeta, updateValue) => (
          <div>{value.join(",")}</div>
        )
      }
    }
  ];
  return <MUIDataTable data={data} columns={columns} />;
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up to request clarification or add additional context in comments.

5 Comments

can i put the filters not inside the data-table but outside , it's like changing it's render place
How can I display only the first element of an object in an array i.e children[0].childName
You mean the first element of the list you want to render?
Is it okay if i put customBodyRender: () => {} with an empty parameter?
If you dont require one of value, tableMeta, updateValue it should work
0

In addition, you can return just the string you want (not needed to be inside a div)

  const colData = (colArrayData: any) => {
        return colArrayData.map((item: any) => item.name).join(", ")
  }


  const columns = [
    { name: "user" },
    {
      name: "langs",
      options: { customBodyRender: (colArrayData: any) => colData(colArrayData) },
    }
  ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.