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 ...
随机推荐
- WKWebView实现网页静态资源优先从本地加载
前言:最近微信的小游戏跳一跳特别的火,顺便也让h5小游戏更加的火热.另外微信小程序,以及支付宝的小程序都是用H5写的.无论是小游戏还是小程序,这些都需要加载更多的资源文件,处理更多的业务.这些都对网页 ...
- python __setattr__、__getattr__、__getattribute__全面详解
一.属性引用函数 hasattr(obj,name[,default])getattr(obj,name)setattr(obj,name,value)delattr(obj,name) 二.属性引用 ...
- template.js artTemplate 简洁语法官网下载不了 template.js artTemplate 新下载地址
参考:https://blog.csdn.net/tavatimsa/article/details/82019792
- fastdfs-nginx-module-master的一些奇怪的特点
测试环境和生产环境分别是两套fastdfs,但是网络没有隔离,今天发现一个奇怪的现象就是测试环境的文件,通过生产环境的入口,居然也可以访问到,我草了,奇怪了 研究发现功劳是fastdfs-nginx- ...
- centOs6.5配置启动ssh
.非root用户则执行su或su - 或su root或su - root切换为root用户 2.查看SSH是否安装(检查是否装了SSH包) 输入命令:rpm -qa | grep ssh 注:若没安 ...
- Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]
题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...
- RNN Train和Test Mismatch
李宏毅深度学习 https://www.bilibili.com/video/av9770302/?p=8 在看RNN的时候,你是不是也会觉得有些奇怪, Train的过程中, 是把训练集中的结果作为下 ...
- 10大必备的Intellij插件,大幅提高你的工作效率
转自: https://blog.csdn.net/qq1404510094/article/details/80379375 1. .ignore 生成各种ignore文件,一键创建git igno ...
- 注解@RestController与@Controller的区别
开发RESTful API 时,一般都会在Controller上加上@Controller注解,但是有时候加上@RestController,当同事问为什么的时候,我也一脸懵逼,默默的看了资料,现在就 ...
- Redis的数据结构之List
存储list: ArrayList使用数组方式 LinkedList使用双向链接方式 双向链接表中增加数据 双向链接表中删除数据 存储list常用命令 两端添加 两端弹出 扩展命令 lpush 方式添 ...