Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing data as an array of objects #147

Closed
rooby opened this issue Aug 21, 2018 · 36 comments
Closed

Allow passing data as an array of objects #147

rooby opened this issue Aug 21, 2018 · 36 comments

Comments

@rooby
Copy link

rooby commented Aug 21, 2018

It would be very useful to be able to pass in an array of object for data and then in the column definition you would specify the name of the property on the object.

It would be even better if you could specify nested properties with dot notation, similarly to how react-table works.

@gregnb
Copy link
Owner

gregnb commented Aug 22, 2018

I'll leave this open for community feedback

@jkeruzec
Copy link
Contributor

For feedback,

With my spring rest controller, I have developped utils to take my Rest JSON data and transform it to a raw data array mapped to column to be able to display the data.

It will be very usefull to directly be able to read the JSON and avoid this specific mapping technique.

So for short, yes interested in this functionnality ! ;)

@benjaminbours
Copy link
Contributor

Same as @jkeruzec , i'm interested too :)

@x0fma
Copy link

x0fma commented Sep 1, 2018

Does someone started to work on this ?

@stipe09
Copy link

stipe09 commented Sep 4, 2018

I'm interested too, it will be very usefull.

@alouane
Copy link

alouane commented Sep 5, 2018

+1 for this feature

@VIJAYMYSORE
Copy link

+1

@pavlovicIvan
Copy link

Hi, this feature would be very usefull for our project.
Do you know when will be implemented?

@stipe09
Copy link

stipe09 commented Sep 20, 2018

Are you considering to do this feature?

@sidharthdigiworks
Copy link

It's easier if we have array of objects since data from server usually is formatted that way and we can opt to choose which properties need to be shown in the table.

@jpmillan
Copy link

jpmillan commented Oct 2, 2018

+1

@revskill10
Copy link
Contributor

revskill10 commented Oct 8, 2018

You can use this helper function to transform:

const convertDataToArray = (data, keys) =>
  data.map(obj => Array.from(keys, k=>obj[k]))

@Diaver
Copy link

Diaver commented Oct 13, 2018

I've made in my fork + TS support: https://www.npmjs.com/package/material-datatable

@rooby
Copy link
Author

rooby commented Oct 13, 2018

@Diaver are you going to submitting a pull request to address this issue in this project?

@Diaver
Copy link

Diaver commented Oct 14, 2018

@rooby I am not sure it is possible: In my form at least one big change - my datatable accept object array, not array of arrays

@revskill10
Copy link
Contributor

@Diaver Why not adding one prop like itemDataType, which could be array or object ?

@Diaver
Copy link

Diaver commented Oct 14, 2018

@revskill10 because it is not just one required change

@revskill10
Copy link
Contributor

@Diaver I mean let's just reuse the logic, and what we can do is transforming/processing props before passing down the render logic. Because we want to keep maintainance on one project only (?)

@Diaver
Copy link

Diaver commented Oct 14, 2018

@revskill10
There are some reasons for separated project:

  1. It is already applied a lot of changes in my fork.
  2. Keep both logic in one project doesn't seems to be good idea for me, at least, until current code required to be completely refactored - now it is pretty ugly, and any changes applied with a lot of digging down into the code.
  3. I don't want to be dependent from other people.

@ginman86
Copy link

You could even do it dynamically by checking the type on the data elements. Is it an array of strings? Then match it by index to the columns. Is it an object? Then match it by key to a columnKey property on the column object.

@plourenco
Copy link
Contributor

Not sure why it isn't added yet, so I'm here to increase the feedback. Definitely interested on this!

@mgolkardev
Copy link

mgolkardev commented Dec 26, 2018

Hi
I pull request to add this option.
#331

If confirmed, we will be in the next version.

  const columns = [
      {
        name: "ID",
        field: "id", <= Your object key
      }
  ];
  const options= {
        jsonMode : true
  }

Source: https://github.com/mgolkardev/mui-datatables/tree/patch-1
i hope help you.

@victor-arcane
Copy link

Hi,
It would be very useful, do we have any updates on this?

@RafaelRojasCov
Copy link

RafaelRojasCov commented Jan 27, 2019

I also would like to know if this feature will be in the mui-datatables cuz its' very useful, i'm having trouble handling the data I fetch from my APIs and transform them into array (Object.values(obj) doesn't work in this case, when the object has nested objects inside)

@chapmanjacobd
Copy link

#331

If confirmed, we will be in the next version.

  const columns = [
      {
        name: "ID",
        field: "id", <= Your object key
      }
  ];
  const options= {
        jsonMode : true
  }

Source: https://github.com/mgolkardev/mui-datatables/tree/patch-1
i hope help you.

How do I apply this patch to the npm installed mui-datatables?

@gregnb
Copy link
Owner

gregnb commented Feb 21, 2019

The latest release now supports data as objects. Example: https://github.com/gregnb/mui-datatables/blob/master/examples/data-as-objects/index.js

@gregnb gregnb closed this as completed Feb 21, 2019
@raikusy
Copy link

raikusy commented Mar 29, 2019

@gregnb How can I access a nested property? For example:

const data = [
{
  id: 1,
  parent: {
    child: "wah"
  }
}
]

I want to access the child property like:

columns={
  [
    {
      name: 'parent.child',
      label: 'Child'
    }
  ]
}

@saurabh8nitrr
Copy link

You can use this helper function to transform:

const convertDataToArray = (data, keys) =>
  data.map(obj => Array.from(keys, k=>obj[k]))

Can you specify in details...what is the key variable and all

@tjatwood
Copy link

This appears to be broken in version 3.7.1. When I upgraded from v2, these columns stopped showing content. Here's a codesandbox to verify. https://codesandbox.io/s/tender-morning-gknfz

@wdh2100
Copy link
Collaborator

wdh2100 commented Nov 18, 2020

@tjatwood

thank you for reporting.

As it changed to *v3

  • migration guide link
Name Type Default Description
enableNestedDataAccess string "" If provided a non-empty string (ex: "."), it will use that value in the column's names to access nested data. For example, given a enableNestedDataAccess value of "." and a column name of "phone.cell", the column would use the value found in phone:{cell:"555-5555"}. Any amount of nesting will work. Example demonstrates the functionality.

@tjatwood
Copy link

Thank you! This works great.

@TKdevlop
Copy link

Thanks dude.

@musindo
Copy link

musindo commented Aug 24, 2021

This is great thanks a lot. Spent hours on this

@emileWhispa
Copy link

Thanks

@tjatwood

thank you for reporting.

As it changed to *v3

  • migration guide link

Name Type Default Description
enableNestedDataAccess string "" If provided a non-empty string (ex: "."), it will use that value in the column's names to access nested data. For example, given a enableNestedDataAccess value of "." and a column name of "phone.cell", the column would use the value found in phone:{cell:"555-5555"}. Any amount of nesting will work. Example demonstrates the functionality.

THanks this reall worked

@pradeepdev001
Copy link

Just add <enableNestedDataAccess: '.',> in option object.
For Example:

const options = {
filter: true,
filterType: 'dropdown',
responsive: 'vertical',
enableNestedDataAccess: '.', // allows nested data separated by "." (see column names and the data structure above)
};

Ref: Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests