博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
antd typescript 可编辑单元格
阅读量:4363 次
发布时间:2019-06-07

本文共 5400 字,大约阅读时间需要 18 分钟。

import { Row, Col, Select, Table, Input, Popconfirm, Button, Form } from 'antd';import * as React from 'react'const FormItem = Form.Item;const EditableContext = React.createContext({});const EditableRow = ({ form, index, ...props }: any) => (  
);const EditableFormRow = Form.create()(EditableRow);class EditableCell extends React.Component
{ public state = { editing: false, } public editable: any; public input: any; public cell: any; public form: any; public componentDidMount() { console.log(this.props.editable); if (this.props.editable) { document.addEventListener('click', this.handleClickOutside, true); } } public componentWillUnmount() { if (this.props.editable) { document.removeEventListener('click', this.handleClickOutside, true); } } public toggleEdit = () => { const editing = !this.state.editing; this.setState({ editing }, () => { if (editing) { this.input.focus(); } }); } public handleClickOutside = (e: any) => { const { editing } = this.state; if (editing && this.cell !== e.target && !this.cell.contains(e.target)) { this.save(); } } public save = () => { const { record, handleSave }: any = this.props; this.form.validateFields((error: any, values: any) => { if (error) { return; } this.toggleEdit(); handleSave({ ...record, ...values }); }); } public render() { const { editing } = this.state; const { editable, dataIndex, title, record, index, handleSave, ...restProps }: any = this.props; console.log(this.props); return ( (this.cell = node)} {...restProps}> {editable ? (
{(form) => { this.form = form; return ( editing ? (
{
this.form.getFieldDecorator(dataIndex, { rules: [{ required: true, message: `${title} is required.`, }], initialValue: record[dataIndex], })(
(this.input = node)} onPressEnter={
this.save} /> )}
) : (
{restProps.children}
) ); }}
) : restProps.children} ); }}export class InputContentConfigurator extends React.Component
{ public dataSource: any; public columns: any; public constructor(props: any) { super(props); this.state = { dataSource: [{ key: 0, name: 'Edward King 0', checkrules: '32', defaultValue: 'null', }, { key: 1, name: 'Edward King 1', checkrules: '32', defaultValue: 'null', }], count: 2, }; this.columns = [{ title: 'name', dataIndex: 'name', width: '30%', editable: true, }, { title: 'checkrules', dataIndex: 'checkrules', editable: true, }, { title: 'defaultValue', dataIndex: 'defaultValue', editable: true, } , { title: 'operation', dataIndex: 'operation', render: (text: any, record: any) => { return ( this.state.dataSource.length >= 1 ? (
this.handleDelete(record.key)}>
Delete
) : null ); }, } ]; } public handleDelete = (key: any) => { console.log(this.props); const { dataSource }: any = { ...this.state }; this.setState({ dataSource: dataSource.filter((item: any) => item.key !== key) }); } public handleAdd = () => { const { count, dataSource }: any = { ...this.state }; const newData = { key: count, name: count, checkrules: '请输入', defaultValue: '请输入', }; this.setState({ dataSource: [...dataSource, newData], count: count + 1, }); } public handleSave = (row: any) => { const newData = [...this.state.dataSource]; const index = newData.findIndex((item: any) => row.key === item.key); const item = newData[index]; newData.splice(index, 1, { ...item, ...row, }); this.setState({ dataSource: newData }); } public render() { const { dataSource }: any = { ...this.state }; const components = { body: { row: EditableFormRow, cell: EditableCell, }, }; const columns = this.columns.map((col: any) => { if (!col.editable) { return col; } return { ...col, onCell: (record: any) => ({ record, editable: col.editable, dataIndex: col.dataIndex, title: col.title, handleSave: this.handleSave, }), }; }); return (

Input content configurator

); }}

 

转载于:https://www.cnblogs.com/Denvue/p/10900892.html

你可能感兴趣的文章
DictVectorizer中的fit_transform
查看>>
HDFS优缺点
查看>>
排序算法(1) 快速排序 C++实现
查看>>
伙伴分配器的一个极简实现
查看>>
$.ajax所犯的错误。success后面不执行
查看>>
Spring注入方式及注解配置
查看>>
cocos2dx blender 骨骼动画实现
查看>>
ARM基础
查看>>
eclipse
查看>>
Mybatis参数传递及返回类型
查看>>
关于Ubuntu使用笔记
查看>>
调整Tomcat上的参数提高性能[转]
查看>>
在Ajax方式产生的浮动框中,点击选项包含某个关键字的选项
查看>>
SDK 操作 list-view control 实例 -- 遍历进程
查看>>
由于SSH配置文件的不匹配,导致的Permission denied (publickey)及其解决方法
查看>>
65. Valid Number
查看>>
检查MySQL主从数据一致性
查看>>
结构化日志:出错时你最想要的好朋友
查看>>
Git常用命令总结
查看>>
[算法练习]Excel Sheet Column Title
查看>>