antd-procomponent中编辑表格动态数据设置的使用

简介: antd-procomponent中编辑表格动态数据设置的使用

一、前言

如果我们现在有这样的一个需求,在设置列的值时要同时改变列的值该怎么做呢?



如果使用antd-procomponent的EditableProTable组件能够很方便的实现,它提供了setRowData、getRowsData的方法来获取和设置编辑表格的值:


二、使用EditableProTable实现上述需求

我们实现刚才的需求可以这些写:

1)定义editorFormRef

const editorFormRef = useRef<any>();

2)传递给EditableProTable

<EditableProTable<DataSourceType>
        rowKey="id"
        headerTitle="可编辑表格"
        editableFormRef={editorFormRef}
    ...
      />

3)通过renderFormItem自定义单元格

{
      title: '列C',
      dataIndex: 'decs',
      renderFormItem: ({ index }: any) => <ParamValue index={index} />,
    },

ParamValue 组件:

const ParamValue = ({ onChange, value, reqParamType, index }: any) => {
    const [inputValue, setInputValue] = useState<any>(value || '');
    const handleInputChange = (v: string) => {
      setInputValue(v);
      onChange(v);
      const data = editorFormRef.current?.getRowData(index);
      if (data) {
        const setData = { title: '123' };
        editorFormRef.current?.setRowData?.(index, { title: '123' });
      }
    };
    return (
        <Input
          placeholder="请输入值"
          value={inputValue}
          onChange={(e) => handleInputChange(e.target.value)}
        />
    );
  };

这样我们在修改列C值的同时将列A的值也做了修改了,上述的代码会将列A值改为123:


三、完整源码如下

完整代码如下:

import type { ProColumns } from '@ant-design/pro-components';
import { EditableProTable } from '@ant-design/pro-components';
import React, { useState, useRef } from 'react';
import { Input } from 'antd';
type DataSourceType = {
  id: React.Key;
  title?: string;
  readonly?: string;
  decs?: string;
  state?: string;
  created_at?: string;
  update_at?: string;
  children?: DataSourceType[];
};
const defaultData: DataSourceType[] = [
  {
    id: 624748504,
    title: '活动名称一',
    readonly: '活动名称一',
    decs: '这个活动真好玩',
    created_at: '1590486176000',
    update_at: '1590486176000',
  },
  {
    id: 624691229,
    title: '活动名称二',
    readonly: '活动名称二',
    decs: '这个活动真好玩',
    created_at: '1590481162000',
    update_at: '1590481162000',
  },
];
export default () => {
  const editorFormRef = useRef<any>();
  const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([624748504, 624691229]);
  const [dataSource, setDataSource] = useState<readonly DataSourceType[]>([]);
  const [position, setPosition] = useState<'top' | 'bottom' | 'hidden'>('bottom');
  const ParamValue = ({ onChange, value, reqParamType, index }: any) => {
    const [inputValue, setInputValue] = useState<any>(value || '');
    const handleInputChange = (v: string) => {
      setInputValue(v);
      onChange(v);
      const data = editorFormRef.current?.getRowData(index);
      if (data) {
        const setData = { title: '123' };
        editorFormRef.current?.setRowData?.(index, { title: '123' });
      }
    };
    return (
      <Input placeholder="请输入值" value={inputValue} onChange={(e) => handleInputChange(e.target.value)} />
    );
  };
  const columns: ProColumns<DataSourceType>[] = [
    {
      title: '列A',
      dataIndex: 'title',
      width: '15%',
    },
    {
      title: '列B',
      dataIndex: 'readonly',
      readonly: true,
      width: '15%',
    },
    {
      title: '列C',
      dataIndex: 'decs',
      renderFormItem: ({ index }: any) => <ParamValue index={index} />,
    },
    {
      title: '活动时间',
      dataIndex: 'created_at',
      valueType: 'date',
    },
  ];
  return (
    <>
      <EditableProTable<DataSourceType>
        rowKey="id"
        headerTitle="可编辑表格"
        editableFormRef={editorFormRef}
        maxLength={5}
        scroll={{
          x: 960,
        }}
        recordCreatorProps={
          position !== 'hidden'
            ? {
                position: position as 'top',
                record: () => ({ id: (Math.random() * 1000000).toFixed(0) }),
              }
            : false
        }
        loading={false}
        columns={columns}
        request={async () => ({
          data: defaultData,
          total: 3,
          success: true,
        })}
        value={dataSource}
        onChange={setDataSource}
        editable={{
          type: 'multiple',
          editableKeys,
          onChange: setEditableRowKeys,
        }}
      />
    </>
  );
};

实现起来非常的容易简单,除此之外,EditableProTable(可编辑表格)也提供了其它非常方便的功能,例如操作栏的三大金刚, 保存,删除 和 取消,如果我们要实现复制一行,或者需求只需要的 保存和取消,不需要删除按钮就需要自定义了。大大的提高了编码效率,解决了重复性的工作。

目录
相关文章
|
前端开发 数据格式
jeecgboot前端antd Table组件动态合并单元格
jeecgboot前端antd Table组件动态合并单元格
1243 0
|
7月前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
52301 73
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
|
10月前
|
人工智能 前端开发 JavaScript
跨境电商shopify开发对接流程
Shopify接口开发已全面转向GraphQL核心,深度融合Shopify Functions(Wasm边缘逻辑)与Shopify Magic(AI赋能)。涵盖Partner账号搭建、OAuth权限管理、Webhook实时推送、AI驱动的元数据生成与智能分析,并强调API版本锁定、安全调用及App审核合规。(239字)
|
机器学习/深度学习 人工智能 前端开发
终端里的 AI 编程助手:OpenCode 使用指南
OpenCode 是开源的终端 AI 编码助手,支持 Claude、GPT-4 等模型,可在命令行完成代码编写、Bug 修复、项目重构。提供原生终端界面和上下文感知能力,适合全栈开发者和终端用户使用。
61795 11
|
SQL XML Java
8、Mybatis-Plus 分页插件、自定义分页
这篇文章介绍了Mybatis-Plus的分页功能,包括如何配置分页插件、使用Mybatis-Plus提供的Page对象进行分页查询,以及如何在XML中自定义分页SQL。文章通过具体的代码示例和测试结果,展示了分页插件的使用和自定义分页的方法。
8、Mybatis-Plus 分页插件、自定义分页
|
JSON JavaScript 定位技术
Echarts 绘制地图(中国、省市、区县),保姆级教程!
Echarts 绘制地图(中国、省市、区县),保姆级教程!
29809 154