react 表格扩展与编辑
项目里有个需求是点击表格某行的工料机,显示对应定额下的工料机的表格数据,并能对两个表格进行增删改查,效果如下:

代码如下:
// 引入 Component 组件
import React, { Component } from 'react'; //定义子表显示
const EditableContext = React.createContext();
const EditableRow = ({ form, index, ...props }) => (
<EditableContext.Provider value={form}>
<tr {...props} />
</EditableContext.Provider>
); const EditableFormRow = Form.create()(EditableRow);
class EditableCell extends React.Component {
getInput = () => {
// 这里也可以通过其他值判断显示下拉框(比如dataIndex)
if (this.props.inputType === 'number') {
return <InputNumber />;
}
return <Input />;
}; render() {
const {
editing,
dataIndex,
title,
inputType,
record,
index,
...restProps
} = this.props;
return (
<EditableContext.Consumer>
{(form) => {
const { getFieldDecorator } = form;
return (
<td {...restProps}>
{editing ? (
<FormItem style={{ margin: 0 }}>
{getFieldDecorator(dataIndex, {
rules: [{
required: true,
message: `请填写${title}!`,
}],
initialValue: record[dataIndex],
})(this.getInput())}
</FormItem>
) : restProps.children}
</td>
);
}}
</EditableContext.Consumer>
);
}
} // 将上面的配置应用到子表中
const components = {
body: {
row: EditableFormRow,
cell: EditableCell,
},
}; // 原始colums要把想要编辑的editable设置为true
const columns = OriginalColumns.map((col) => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: record => ({
record,
inputType: col.dataIndex === 'age' ? 'number' : 'text',
dataIndex: col.dataIndex,
title: col.title,
editing: this.isEditing(record), // 判断编辑哪一行
}),
};
}); // 判断编辑哪一行的函数
isEditing = record => record.id === this.state.editingKey; <Table
rowClassName="tableInput"
dataSource={tableData}
columns={columns}
pagination={false}
rowKey={record=>record.id}
getTableData={this.getTableData}
components={components}
rowSelection={rowSelection}
expandedRowRender={this.expandedRowRender} // 字列表扩展函数
expandIconColumnIndex={-1} // 隐藏默认展开样式
expandIconAsCell={false} // 隐藏默认展开样式
expandedRowKeys={expandedRowKeys} // 展开的行的id
defaultExpandedRowKeys={expandedRowKeys}
/> // 点击工料机,获取对应数据并展开,收起上一个展开的
expandTable=(record,index)=>{
this.setState({childTabData:[],parentTabId:record.id})
// 如果已经展开则收回,否则展开
let expandedRows = this.state.expandedRowKeys;
if(expandedRows.includes(record.id)){
this.setState({expandedRowKeys: []})
}else{
// 获取数据并展开当前行
let arr = []
arr.push(record.id)
this.getStuffListById(record.id)
this.setState({expandedRowKeys:arr})
}
}
react 表格扩展与编辑的更多相关文章
- js 实现表格的可编辑状态
实现表格的可编辑,点击修改以后可以编辑,代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="utf ...
- Griddle, griddle-react 一个REACT 表格组件
Griddle, griddle-react 一个REACT 表格组件: http://griddlegriddle.github.io/Griddle/index.html
- AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除
AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...
- jquery-easyui 中表格的行编辑功能
具体实现代码如下: <table id="tt"></table> $('#tt').datagrid({ title:'Editable DataGrid ...
- 【react表格组件】material-table 基本用法 & 组件override
教程: https://mbrn.github.io/material-table/#/ https://material-ui.com/api/table/ github: https://gith ...
- ExtJs 日期相加,Grid表格列可编辑
1.日期相加: Ext.Date.add(new Date(), Ext.Date.DAY, 15) 2.Grid表格列可编辑: { header : "实际已交货量", ...
- vue-split-table【表格合并和编辑插件】
前言 vue-split-table应用的效果图 vue-split-table开源地址,欢迎star,现在已经开源和同步到npm上轻松搞定表格拆分或者合并,编辑,再也不怕被产品怼啦 1.核心源码分析 ...
- Bootstrap:Bootstrap_table第一篇:快速用bootstrap_table(支持参数)筛选并展示数据,固定表格前几列,实现表格单元格编辑
1.准备好css和js文件 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstr ...
- 用react编写一个可以编辑的表格
这只一个雏形,但是可以用了.难点是如何点击每行后面的编辑按钮,让当前行的格子都变成input. import {Component} from 'react' const Action = props ...
随机推荐
- 10.17小结:table.copy() 和 distinct 查询
1. 当datatable 已存在于一个dataset中时,可以使用 ds.tables.add(dt.copy()) 来向dataset 中添加datatable; 2. 当datarow已存在于一 ...
- JQuery限制文本框只能输入数字和小数点的方法
<input type="text" class="txt NumText" Width="100px" /> $(func ...
- PHP(方法 函数 循环 和 数组 查找)
循环 和 数组 查找 顺序查找 二分法查找 冒泡排序 方法 函数 定义:一堆代码的集合叫做函数(满足条件下“一堆”) 语法,定义,调用,参数列表(形参,实参),返回值 两种方法: function 方 ...
- makefile编写规则
cc = g++ -std=c++11 prom = calc deps = FtTest.h obj = FtTest.o newft.o LIBS = -lgtest_c11 $(prom): $ ...
- Linux 中如何用源代码安装软件,以及如何卸载它
https://www.linuxidc.com/Linux/2017-12/149839.htm http://www.openssh.com/ http://www.openssh.com/por ...
- 16.0-uC/OS-III同步
同步 uC/OS-III中用于同步的两种机制:信号量和事件标志组 . 1.信号量 信号量最初用于控制共享资源的访问.信号量可用于ISR与任务间.任务与任务间的同步. “ N”表示信号量可以被累计.初始 ...
- Date Calendar
1 毫秒值概念 时间和日期的计算,必须依赖毫秒值获取当前日期的毫秒值:System.currentTimeMillis() 返回值long类型参数, 时间原点:公元1970年1月1日,午夜0:00:0 ...
- [elk]elasticsearch dsl语句
例子1 统计1,有唱歌兴趣的 2,按年龄分组 3,求每组平均年龄 4,按平均年龄降序排序 sql转为dsl例子 # 每种型号车的颜色数 > 1的 SELECT model,COUNT(DISTI ...
- Domain Driven Development相关概念
Entity 与 Value Object1,Entity有唯一的身份标识,是可变的对象.Value Object是immutable,创建了就不能改变.2,Value Object可以在多个领域之间 ...
- Ch04 映射和元组 - 练习
1. 设置一个映射,其中包含你想要的一些装备,以及它们的价格.然后构建另一个映射,采用同一组键,但在价格上打9折. import scala.collection.JavaConversions.as ...