Skip to content

Commit 3419f6a

Browse files
GrandVizierOlafgabrielliwerant
authored andcommitted
Allow for accessing nested object data. (#619)
* Access nested data with dot notation in name * Adding more unique data and resetting entrypoint * Added a check to see if attribute is valid when transforming data
1 parent dc45413 commit 3419f6a

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

examples/data-as-objects/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,27 @@ class Example extends React.Component {
4343
filter: true,
4444
sort: false
4545
}
46-
}
46+
},
47+
{
48+
name: "phone.home",
49+
label: "Home Phone",
50+
},
51+
{
52+
name: "phone.fake",
53+
label: "Not a Phone #",
54+
},
55+
{
56+
name: "phone2.home",
57+
label: "Not An Attribute",
58+
}
4759
];
4860

4961

5062
const data = [
51-
{ name: "Gabby George", title: "Business Analyst", location: "Minneapolis", age: 30, salary: "$100,000" },
52-
{ name: "Aiden Lloyd", title: "Business Consultant", location: "Dallas", age: 55, salary: "$200,000" },
53-
{ name: "Jaden Collins", title: "Attorney", location: "Santa Ana", age: 27, salary: "$500,000" },
54-
{ name: "Franky Rees", title: "Business Analyst", location: "St. Petersburg", age: 22, salary: "$50,000" }
55-
// ["Aaren Rose", "Business Consultant", "Toledo", 28, "$75,000"],
56-
// ["Blake Duncan", "Business Management Analyst", "San Diego", 65, "$94,000"],
57-
// ["Frankie Parry", "Agency Legal Counsel", "Jacksonville", 71, "$210,000"],
58-
// ["Lane Wilson", "Commercial Specialist", "Omaha", 19, "$65,000"]
63+
{ name: "Gabby George", title: "Business Analyst", location: "Minneapolis", age: 30, salary: "$100,000", phone: { home: '867-5309', cell: '123-4567' } },
64+
{ name: "Aiden Lloyd", title: "Business Consultant", location: "Dallas", age: 55, salary: "$200,000", phone: { home: '867-5310', cell: '123-4568' } },
65+
{ name: "Jaden Collins", title: "Attorney", location: "Santa Ana", age: 27, salary: "$500,000", phone: { home: '867-5311', cell: '123-4569' } },
66+
{ name: "Franky Rees", title: "Business Analyst", location: "St. Petersburg", age: 22, salary: "$50,000", phone: { home: '867-5312', cell: '123-4569' } }
5967
];
6068

6169
const options = {

package-lock.json

Lines changed: 21 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MUIDataTable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ class MUIDataTable extends React.Component {
386386
};
387387

388388
transformData = (columns, data) => {
389+
const leaf = (obj, path) => (path.split('.').reduce((value, el) => value ? value[el] : undefined, obj));
390+
389391
return Array.isArray(data[0])
390392
? data.map(row => {
391393
let i = -1;
@@ -395,7 +397,7 @@ class MUIDataTable extends React.Component {
395397
return col.empty ? undefined : row[i];
396398
});
397399
})
398-
: data.map(row => columns.map(col => row[col.name]));
400+
: data.map(row => columns.map(col => leaf(row, col.name)));
399401
};
400402

401403
setTableData(props, status, callback = () => {}) {

0 commit comments

Comments
 (0)