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 ...
随机推荐
- C#串口小助手
做技术的通病,什么都想学,什么都想亲手做一遍.不然总感觉心里不踏实. 考研期间,利用晚上一点时间,照葫芦画瓢,练习使用c#快速开发一个简单的串口小助手. 这种前后端分离的设计方法,大大提高了开发速度, ...
- Laravel使用Form(转载)
laravel到了5.1.*以上版本,便没有了illuminate/html类库的支持, 我试着把illuminate/html类库加入了laravel5.2,依然没有用, 但是laravelcoll ...
- arch 将 普通用户添加到 docker 组
如果还没有 docker group 就添加一个: sudo groupadd docker 如果你想用你的使用者帳戶(非root帳戶)來使用Docker,把你的帳戶加到Docker的群組中 sudo ...
- 10 windows server 2012R2 发布MVC框架网站注意事项
1:网站编译完成之后,需要发布,网站中应包括的文件有:文件夹(bin,Views,Content等其他网站中涉及到的文件夹)文件(favicon.ico.Web.config.Global.asax) ...
- django 初始命令
1.安装django pip3 install django 2.创建一个Django对象 django-admin.py startproject 项目名称 django-admin.py star ...
- 转:arcgis api for js入门开发系列四地图查询
原文地址:arcgis api for js入门开发系列四地图查询 arcgis for js的地图查询方式,一般来说,总共有三种查询方式:FindTask.IdentifyTask.QueryTas ...
- Qt自定义界面
https://blog.csdn.net/zhangxiaoyu_sy/article/details/78925221
- 在不同语言中static的用法
static (计算机高级语言) 编辑 像在VB,C#,C,C++,Java,PHP中我们可以看到static作为关键字和函数出现,在其他的高级计算机语言如FORTRAN.ALGOL.COBOL.BA ...
- gzframework demo搭建
感谢框架作者,这里给出他的博客 http://www.cnblogs.com/GarsonZhang/ 背景:由于作者对代码的持续开发,导致了以前博客中的下载地址和构建方法和目前的项目不对应,这里给出 ...
- 在 vue 中使用 vieiwer 图片预览插件
https://blog.csdn.net/WestLonly/article/details/79801800?utm_source=blogxgwz0 首先,感谢原作者 官网链接 github地址 ...