antd typescript 可编辑单元格
时间:2019-05-21
本文章向大家介绍antd typescript 可编辑单元格,主要包括antd typescript 可编辑单元格使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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) => ( <EditableContext.Provider value={form}> <tr {...props} /> </EditableContext.Provider> ); const EditableFormRow = Form.create()(EditableRow); class EditableCell extends React.Component<any, any> { 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 ( <td ref={node => (this.cell = node)} {...restProps}> {editable ? ( <EditableContext.Consumer> {(form) => { this.form = form; return ( editing ? ( <FormItem style={{ margin: 0 }}> {this.form.getFieldDecorator(dataIndex, { rules: [{ required: true, message: `${title} is required.`, }], initialValue: record[dataIndex], })( <Input ref={node => (this.input = node)} onPressEnter={this.save} /> )} </FormItem> ) : ( <div className="editable-cell-value-wrap" style={{ paddingRight: 24 }} onClick={this.toggleEdit} > {restProps.children} </div> ) ); }} </EditableContext.Consumer> ) : restProps.children} </td> ); } } export class InputContentConfigurator extends React.Component<any, any> { 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 ? ( <Popconfirm title="Sure to delete?" onConfirm={() => this.handleDelete(record.key)}> <a href="javascript:;">Delete</a> </Popconfirm> ) : 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 ( <div> <h2>Input content configurator</h2> <Row> <Col span={6}> <Form.Item label="选择类型:" labelCol={{ span: 6 }} wrapperCol={{ span: 12 }} required={true}> <Select defaultValue={0}> <Select.Option value={0}>List</Select.Option> <Select.Option value={1}>String</Select.Option> </Select> </Form.Item> </Col> <Col span={6}> <Form.Item label="输入对象名字:" labelCol={{ span: 8 }} wrapperCol={{ span: 12 }} required={true}> <Input placeholder="请输入对象的名字" /> </Form.Item> </Col> </Row> <Button onClick={this.handleAdd} type="primary" > Add a row </Button> <Table components={components} dataSource={dataSource} columns={columns} /> </div> ); } }
原文地址:https://www.cnblogs.com/Denvue/p/10900892.html
- 管理混合云环境的5个要点
- Team Foundation Server 2010 – Basic Installation
- 富文本编辑器的一键排版功能
- 通过ProGet搭建一个内部的Nuget服务器
- Mercury Editor学习心得
- 无意禁止使用:英伟达官方回应GeForce软件条款更改
- Ext JS 6 新特性和工具
- 为你的WordPress 博客文章页面增加多彩排版条
- java: web应用中不经意的内存泄露
- java: web应用中不经意的内存泄露
- velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools
- mac上开启ftp
- Web前端开发的四个阶段
- 使用Visual Studio 2015 开发ASP.NET MVC 5 项目部署到Mono/Jexus
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 使用laravel和ECharts实现折线图效果的例子
- Laravel 将数据表的数据导出,并生成seeds种子文件的方法
- Laravel Eloquent ORM 多条件查询的例子
- laravel5.5添加echarts实现画图功能的方法
- thinkphp5.1框架实现格式化mysql时间戳为日期的方式小结
- Laravel 创建指定表 migrate的例子
- Laravel find in set排序实例
- Laravel 对某一列进行筛选然后求和sum()的例子
- 关于laravel 数据库迁移中integer类型是无法指定长度的问题
- laravel多条件查询方法(and,or嵌套查询)
- thinkphp框架使用JWTtoken的方法详解
- Laravel创建数据库表结构的例子
- laravel按天、按小时,查询数据的实例
- 浅谈laravel中的关联查询with的问题
- 解决Laravel 不能创建 migration 的问题