React Antd可编辑单元格,非官网写法,不使用可编辑行和form验证

antd3以上的写法乍一看还挺复杂,自己写了个精简版

没用EditableRow+Cell的结构,也不使用Context、高阶组件等,不使用form验证

最终效果:

React Antd可编辑单元格,非官网写法,不使用可编辑行和form验证_第1张图片

React Antd可编辑单元格,非官网写法,不使用可编辑行和form验证_第2张图片

class EditableCell extends React.Component {
    state = {
        editing: false
    };
    toggleEdit = () => {
        const editing = !this.state.editing
        this.setState({ editing }, () => {
            if (editing) {
                this.input.focus()
            }
        })
    };
    save = e => {
        const { record, handleSave } = this.props;
        this.toggleEdit();
        handleSave(record, e.target.value)

    };  // save主要处理两件事,一是切换editing状态,二是提交更新的数据
    render() {
        const { children } = this.props
        const { editing } = this.state;
        return editing ? (
            (this.input=node)} onPressEnter={this.save} onBlur={this.save} />
        ) : (
            {children}
            )
    }
};

最后使用的时候直接在column元素的render里面 就好啦, props一定要传处理保存修改的方法

render: (text, record) => {
    return ({text}) //记得传props
    }

现在这个可编辑单元格组件在鼠标失焦或者回车后,列数据会变回修改前的数据,在state里面加个text,把最后显示的 {children} 换成 {text} 就可以。

该组件也许很多页面都会使用,单独放在一个文件里再引入会优雅很多:

import React from 'react';
import {Input, Icon} from 'antd';

class EditableCell extends React.Component {
    state = {
        editing: false,
        text: this.props.children
    };
    toggleEdit = () => {
        const editing = !this.state.editing
        this.setState({ editing }, () => {
            if (editing) {
                this.input.focus()
            }
        })
    };
    save = e => {
        const { record, handleSave } = this.props;
        this.setState({text: e.target.value});
        this.toggleEdit();
        handleSave(record, e.target.value)

    };
    render() {
        const { editing, text } = this.state;
        return editing ? (
            (this.input=node)} onPressEnter={this.save} onBlur={this.save} />
        ) : (
            {text}
            )
    }
};

export default EditableCell;

引入的时候:

import { EditableCell } from '../EditableCell'

全部页面index.jsx大概是这样的

import React, { useEffect } from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Card, Input, Select, Row, message, Col, Table, Button, Icon, Upload, Form, DatePicker } from 'antd';
import { connect } from 'dva';
import download from '@/utils/download';
import styles from './style.less';

const { Option } = Select;

class EditableCell extends React.Component {
    state = {
        editing: false
    };
    toggleEdit = () => {
        const editing = !this.state.editing
        this.setState({ editing }, () => {
            if (editing) {
                this.input.focus()
            }
        })
    };
    save = e => {
        const { record, handleSave } = this.props;
        this.toggleEdit();
        handleSave(record, e.target.value)

    };
    render() {
        const { children } = this.props
        const { editing } = this.state;
        return editing ? (
            (this.input=node)} onPressEnter={this.save} onBlur={this.save} />
        ) : (
            {children}
            )
    }
};

const Aabbb = props => {
    const { form, dispatch, dataLoading } = props;
    const { getFieldDecorator } = form;
    const { pageInfo, res }  = props;
    const formItemLayout = {
        labelCol: { span: 8 },
        wrapperCol: { span: 16 },
    };
    const columns = [
        { title: '序号', dataIndex: 'id', align: 'center', width: 80, fixed: 'left', render: (text, record, index) =>
            ({(pageInfo.current - 1) * pageInfo.pageSize + index + 1})
        },
        ...
        { title: '结果', dataIndex: 'results', align: 'center', render: (text, record) => (
            
        )},
        { title: '备注', dataIndex: 'notes', align: 'center', width: 120, render: (text, record) => {
            return ({text})
        }}
    ];
    const handleModifyNote = (record, value) => {
        console.log('save', {...record, notes: value})
        dispatch({})
    };
    const handleModifyResult = (value, record) => {
        dispatch({})
        console.log({...record, inspectionResults: value});
    };
    
    
    useEffect(() => {
        
    }, []);
    const queryData = () => {}
        
    return (
        
            
                
... ...
index} pagination={} onChange={} /> ); } export default connect(({ aabbb, loading }) => ({ res: aabbb.res, dataLoading: loading.effects['aabbb/QueryAabbb'], }))(Form.create()(Aabbb));

你可能感兴趣的:(前端,react,antd,可编辑单元格)