bee-table/demo/demolist/Demo0501.js

171 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-12-21 09:58:52 +08:00
/**
2019-04-24 11:32:19 +08:00
*
* @title 行编辑
* @description 可以对行进行编辑的表格
*
*/
import React, { Component } from "react";
2019-03-20 17:44:05 +08:00
import Table from "../../src";
2019-04-24 11:32:19 +08:00
import { Button, FormControl } from "tinper-bee";
2018-12-21 09:58:52 +08:00
2019-04-24 11:32:19 +08:00
class EditableCell extends Component {
constructor(props, context) {
super(props);
this.state = {
value: this.props.value,
editable: false
};
}
2018-12-21 09:58:52 +08:00
2019-04-24 11:32:19 +08:00
handleChange = value => {
this.setState({ value });
if (this.props.onChange) {
this.props.onChange(value);
}
};
2018-12-21 09:58:52 +08:00
2019-04-24 11:32:19 +08:00
render() {
return (
<div className="editable-cell">
<div className="editable-cell-input-wrapper">
{this.props.editable ? (
<FormControl
value={this.state.value}
onChange={this.handleChange}
/>
) : (
this.state.value || " "
)}
</div>
</div>
);
2018-12-21 09:58:52 +08:00
}
2019-04-24 11:32:19 +08:00
}
let dataSource = [
{ a: "ASVAL_201903280005", b: "小张", c: "男", d: "财务二科", key: "1" },
{ a: "ASVAL_201903200004", b: "小明", c: "男", d: "财务一科", key: "2" },
{ a: "ASVAL_201903120002", b: "小红", c: "女", d: "财务一科", key: "3" }
2018-12-21 09:58:52 +08:00
];
2019-04-24 11:32:19 +08:00
for (let i = 4; i < 10; i++) {
dataSource.push({
a: "ASVAL_201903120002",
b: "小红",
c: "女",
d: "财务一科",
key: i + ""
});
}
class Demo0501 extends Component {
constructor(props, context) {
2019-03-20 17:44:05 +08:00
super(props);
this.columns = [
{
2019-04-24 11:32:19 +08:00
title: "员工编号",
dataIndex: "a",
key: "a"
2019-03-20 17:44:05 +08:00
},
{
2019-04-24 11:32:19 +08:00
title: "名字",
dataIndex: "b",
key: "b",
2019-03-20 17:44:05 +08:00
render: (text, record, index) => (
2019-04-24 11:32:19 +08:00
<EditableCell
editable={this.state.editingRows.indexOf(index) > -1}
2019-03-20 17:44:05 +08:00
value={text}
2019-04-24 11:32:19 +08:00
onChange={this.onCellChange(index, "quality")}
2019-03-20 17:44:05 +08:00
/>
)
},
{
2019-04-24 11:32:19 +08:00
title: "性别",
dataIndex: "c",
key: "c",
width: 100,
2019-03-20 17:44:05 +08:00
render: (text, record, index) => (
2019-04-24 11:32:19 +08:00
<EditableCell
editable={this.state.editingRows.indexOf(index) > -1}
value={text}
onChange={this.onCellChange(index, "level")}
2019-03-20 17:44:05 +08:00
/>
)
},
{
2019-04-24 11:32:19 +08:00
titile: "部门",
dataIndex: "d",
key: "d",
render: (text, record, index) => text
2019-03-20 17:44:05 +08:00
}
];
2018-12-21 09:58:52 +08:00
2019-04-24 11:32:19 +08:00
this.state = {
dataSource: dataSource,
editingRows: []
2019-03-20 17:44:05 +08:00
};
2019-04-24 11:32:19 +08:00
this.dataBuffer = {};
this.currentIndex = null;
this.currentRecord = null;
this.__OPTS_BTN_GROUP__ = null;
}
createBtn = (text, props, event) => {
let btn = document.createElement("button");
btn.innerText = text;
for (let pKey in props) {
btn.setAttribute(pKey, props[pKey]);
}
for (let eKey in event) {
btn.addEventListener(eKey, event[eKey]);
}
return btn;
2019-03-20 17:44:05 +08:00
};
2019-04-24 11:32:19 +08:00
edit = index => () => {
if (index === null) return;
let editingRows = [...this.state.editingRows];
editingRows.push(index);
this.dataBuffer[index] = Object.assign({}, dataSource[index]);
this.setState({ editingRows });
2019-03-20 17:44:05 +08:00
};
2019-04-24 11:32:19 +08:00
abortEdit = () => {
let editingRows = [...this.state.editingRows];
editingRows.splice(index, 1);
delete this.dataBuffer[index];
this.setState({ editingRows });
2019-03-20 17:44:05 +08:00
};
2019-04-24 11:32:19 +08:00
commitChange = index => () => {
let editingRows = [...this.state.editingRows];
let dataSource = [...this.state.dataSource];
editingRows.splice(editingRows.indexOf(index), 1);
dataSource[index] = this.dataBuffer[index];
delete this.dataBuffer[index];
this.setState({ editingRows, dataSource });
2019-03-20 17:44:05 +08:00
};
2019-04-24 11:32:19 +08:00
onCellChange = (index, key) => value => {
this.dataBuffer[index][key] = value;
2019-03-20 17:44:05 +08:00
};
2019-04-24 11:32:19 +08:00
2018-12-21 09:58:52 +08:00
render() {
2019-03-20 17:44:05 +08:00
const { dataSource } = this.state;
const columns = this.columns;
return (
2019-04-24 11:32:19 +08:00
<div className="demo0501">
2019-03-20 17:44:05 +08:00
<Table
data={dataSource}
columns={columns}
2019-04-24 11:32:19 +08:00
onRowHover={this.handleRowHover}
hoverContent={this.renderRowHover}
2019-03-20 17:44:05 +08:00
/>
</div>
);
2018-12-21 09:58:52 +08:00
}
}
2019-04-24 11:32:19 +08:00
export default Demo0501;