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

代码如下:

// 引入 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})
}
}   
// 子列表
expandedRowRender=()=>{
// 子列表也要编辑(字列表不用编辑可以不用,省略了一些子列表编辑代码)
const childComponents = {
body: {
 row: EdiChildTableFormRow, 
 cell: EdiChildTableCell,
},
};
const childColumns = childOriColumns.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),
}),
};
});
const { countKey, childTabData ,editingKey} = this.state;
return (
<div className="childTab">
<Table
rowClassName="tableInput"
columns={childColumns}
dataSource={this.state.childTabData}
rowKey={record=>record.id}
components={childComponents}
rowSelection={childRowSelection}
/>
<div className="childBtnBox">
<Button className="mr20" onClick={addGlj} disabled={editingKey && editingKey.includes('new')? true:false}>新增</Button>
<Button disabled={this.state.onDeleteChildBtn} onClick={delChild}>删除</Button>
</div>
</div>
);
}

 

react 表格扩展与编辑的更多相关文章

  1. js 实现表格的可编辑状态

    实现表格的可编辑,点击修改以后可以编辑,代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="utf ...

  2. Griddle, griddle-react 一个REACT 表格组件

    Griddle, griddle-react 一个REACT 表格组件: http://griddlegriddle.github.io/Griddle/index.html

  3. AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除

    AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...

  4. jquery-easyui 中表格的行编辑功能

    具体实现代码如下: <table id="tt"></table> $('#tt').datagrid({ title:'Editable DataGrid ...

  5. 【react表格组件】material-table 基本用法 & 组件override

    教程: https://mbrn.github.io/material-table/#/ https://material-ui.com/api/table/ github: https://gith ...

  6. ExtJs 日期相加,Grid表格列可编辑

    1.日期相加: Ext.Date.add(new Date(), Ext.Date.DAY, 15) 2.Grid表格列可编辑: {    header : "实际已交货量",   ...

  7. vue-split-table【表格合并和编辑插件】

    前言 vue-split-table应用的效果图 vue-split-table开源地址,欢迎star,现在已经开源和同步到npm上轻松搞定表格拆分或者合并,编辑,再也不怕被产品怼啦 1.核心源码分析 ...

  8. Bootstrap:Bootstrap_table第一篇:快速用bootstrap_table(支持参数)筛选并展示数据,固定表格前几列,实现表格单元格编辑

    1.准备好css和js文件 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstr ...

  9. 用react编写一个可以编辑的表格

    这只一个雏形,但是可以用了.难点是如何点击每行后面的编辑按钮,让当前行的格子都变成input. import {Component} from 'react' const Action = props ...

随机推荐

  1. python基础类型—字符串

    字符串str 用引号引起开的就是字符串(单引号,双引号,多引号) 1.字符串的索引与切片. 索引即下标,就是字符串组成的元素从第一个开始,初始索引为0以此类推. a = 'ABCDEFGHIJK' p ...

  2. js中 函数参数的 传值/传引用 问题

    如果  传入function的参数是  (数值.字符串.布尔值) 此时是以 传值 的方式 进行. 如果  传入function的参数是  (数组.对象.其他函数) 此时是以 传引用 的方式 进行. 1

  3. vue2中使用 better-scroll

    使用时有三个要点: 一:html部分 <div class="example" ref="divScroll"> <div> <p ...

  4. 类似于Mimikatz的Linux Hash Dump工具

    项目主页 https://github.com/huntergregal/mimipenguin 需要root权限 支持 Kali 4.3.0 (rolling) x64 (gdm3) Ubuntu ...

  5. 19. vue的原理

    vue:原理1 => Object.defineProperty 当你把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项,Vue 将遍历此对象所有的属性,并使用 Obj ...

  6. mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    错误情况 解决办法: 首先查看mysql的命令 其次修改root用户的密码 set password for 'root'@'localhost' = password('123456'); 最后退出 ...

  7. Linux下Solr单机版、集群版安装与配置

    一.安装 1.需要的安装包有apache-tomcat-7.0.47.tar.gz.solr-4.10.3.tgz.tgz(jdk自行安装) 这里默认大家已经安装好jdk与tomcat,所以在这里不做 ...

  8. 3.1.2 Spring之IoC

    二.Spring之IoC 1. IoC与DI (1) IoC 控制反转( IoC, Inversion of Control) , 是一个概念, 是一种思想. 控制反转就是对对象控制权的转移, 从程序 ...

  9. 【UML】NO.51.EBook.5.UML.1.011-【UML 大战需求分析】- 时序图(Timing Diagram)

    1.0.0 Summary Tittle:[UML]NO.51.EBook.1.UML.1.011-[UML 大战需求分析]- 时序图(Timing Diagram) Style:DesignPatt ...

  10. asp.net 导出excel--NPOI

    1.使用OLEDB导出Excel ,这种方式有点慢,慎用 /// <summary> /// 使用OLEDB导出Excel /// </summary> /// <par ...