Skip to content

Commit 1e32ea2

Browse files
authored
Support nested data (#15)
fix #14
1 parent 7c424a2 commit 1e32ea2

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

packages/react-bootstrap-table2/src/body.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4+
import _ from './utils';
45
import Row from './row';
56

67
const Body = ({ columns, data, keyField }) => (
78
<tbody>
89
{
910
data.map((row, index) => (
1011
<Row
11-
key={ row[keyField] }
12+
key={ _.get(row, keyField) }
1213
row={ row }
1314
rowIndex={ index }
1415
columns={ columns }

packages/react-bootstrap-table2/src/cell.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4+
import _ from './utils';
45

56
const Cell = ({ row, rowIndex, column }) => {
6-
let content = row[column.dataField];
7+
let content = _.get(row, column.dataField);
78
if (column.formatter) {
89
content = column.formatter(content, row, rowIndex, column.formatExtraData);
910
}

packages/react-bootstrap-table2/src/row.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4+
import _ from './utils';
45
import Cell from './cell';
56

67
const Row = ({ row, rowIndex, columns }) => (
@@ -9,7 +10,7 @@ const Row = ({ row, rowIndex, columns }) => (
910
columns.map(column =>
1011
(
1112
<Cell
12-
key={ row[column.dataField] }
13+
key={ _.get(row, column.dataField) }
1314
row={ row }
1415
rowIndex={ rowIndex }
1516
column={ column }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint no-empty: 0 */
2+
3+
function get(target, field) {
4+
const pathArray = [field]
5+
.join('.')
6+
.replace(/\[/g, '.')
7+
.replace(/\]/g, '')
8+
.split('.');
9+
let result;
10+
try {
11+
result = pathArray.reduce((curr, path) => curr[path], target);
12+
} catch (e) {}
13+
return result;
14+
}
15+
16+
export default {
17+
get
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import _ from '../src/utils';
2+
3+
describe('Utils', () => {
4+
describe('get', () => {
5+
const data = {
6+
name: 'A',
7+
address: {
8+
road: 'BCD',
9+
postal: '1234-12345',
10+
city: {
11+
name: 'B'
12+
}
13+
}
14+
};
15+
16+
it('should return correct data', () => {
17+
expect(_.get(data, 'name')).toEqual(data.name);
18+
expect(_.get(data, 'address.road')).toEqual(data.address.road);
19+
expect(_.get(data, 'address.city.name')).toEqual(data.address.city.name);
20+
expect(_.get(data, 'address.notExist')).toEqual(undefined);
21+
expect(_.get(data, 'address.not.exist')).toEqual(undefined);
22+
});
23+
});
24+
});

0 commit comments

Comments
 (0)