Skip to content

Commit 2ffdd59

Browse files
committed
update table
1 parent 86aec17 commit 2ffdd59

File tree

3 files changed

+74
-39
lines changed

3 files changed

+74
-39
lines changed

src/components/Cell.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ export default class Cell extends React.Component {
99
render() {
1010
const {value, onEdit, data} = this.props
1111

12-
if (Array.isArray(value)) {
12+
if (value instanceof Object) {
1313
return <td>
1414
<select className="form-control"
1515
data-column={data.columnIndex}
1616
data-row={data.rowIndex}
1717
onChange={onEdit}
18-
value={value}>
19-
{value.map((item, i) => <option key={i}>{item}</option>)}
18+
value={value.selected}>
19+
{value.items.map((item, i) => <option key={i}>{item}</option>)}
2020
</select>
2121
</td>
2222
}

src/components/CellDropdown.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react'
2+
3+
export default class CellDropdown extends React.Component {
4+
state = {}
5+
6+
componentDidMount() {
7+
this.setState({value: this.props.value})
8+
}
9+
onChange(e,data){
10+
this.props.onChange(e,this.state.value,data)
11+
}
12+
13+
render() {
14+
const {value, data} = this.props
15+
return <select className="form-control"
16+
onChange={this.onChange.bind(this, {data, value: this.state.value})}
17+
value={value.selected}>
18+
{value.items.map((item, i) => <option key={i}>{item}</option>)}
19+
</select>
20+
}
21+
}

src/pages/Index.jsx

+50-36
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,60 @@ import React, {Component} from 'react'
33
import Layout from "../components/Layout";
44
import DinamicTable from "../components/DinamicTable";
55

6+
const table = {
7+
dataSource: [
8+
[
9+
"asdasd",
10+
23,
11+
343,
12+
{
13+
selected: 'hello',
14+
items: ["hello", 'rich', 23]
15+
}
616

7-
export default class Index extends Component {
17+
],
18+
[
19+
"asdasd",
20+
23,
21+
343,
22+
],
23+
[
24+
"asdasd",
25+
23,
26+
343,
27+
],
28+
[
29+
"asdasd",
30+
23,
31+
343,
32+
],
33+
],
34+
columns: [
35+
'TEXT',
36+
'NUMBER 1',
37+
'NUMBER 2',
38+
'SUM',
39+
'DROPDOWN',
40+
]
41+
}
842

43+
export default class Index extends Component {
944

1045
state = {
11-
dataSource: [
12-
[
13-
"asdasd",
14-
23,
15-
343,
16-
["hello", 'rich', 23]
17-
],
18-
[
19-
"asdasd",
20-
23,
21-
343,
22-
],
23-
[
24-
"asdasd",
25-
23,
26-
343,
27-
],
28-
[
29-
"asdasd",
30-
23,
31-
343,
32-
],
33-
],
34-
columns: [
35-
'TEXT',
36-
'NUMBER 1',
37-
'NUMBER 2',
38-
'SUM',
39-
'DROPDOWN',
40-
]
46+
columns: [],
47+
dataSource: []
48+
}
49+
50+
componentDidMount() {
51+
const dataSource = table.dataSource
52+
const columns = table.columns
53+
54+
dataSource.forEach((item, index) => {
55+
while (item.length < columns.length) {
56+
item.push("")
57+
}
58+
})
59+
this.setState({dataSource, columns})
4160
}
4261

4362
editCell({target}) {
@@ -68,7 +87,6 @@ export default class Index extends Component {
6887
handleAddColumn() {
6988
const {columns, dataSource} = this.state
7089

71-
7290
columns.push('column name')
7391

7492
dataSource.forEach((item, index) => {
@@ -81,11 +99,7 @@ export default class Index extends Component {
8199

82100
handleAddRow() {
83101
const {columns, dataSource} = this.state
84-
85-
86102
dataSource.push(Array(columns.length).fill(""))
87-
88-
89103
this.setState({dataSource})
90104
}
91105

0 commit comments

Comments
 (0)