Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e32ea2

Browse files
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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 }
Lines changed: 18 additions & 0 deletions
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+
};
Lines changed: 24 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /