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

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

一、前言

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



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


二、使用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(可编辑表格)也提供了其它非常方便的功能,例如操作栏的三大金刚, 保存,删除 和 取消,如果我们要实现复制一行,或者需求只需要的 保存和取消,不需要删除按钮就需要自定义了。大大的提高了编码效率,解决了重复性的工作。

目录
相关文章
|
存储 资源调度 JavaScript
轻松搞定 reduxjs/toolkit
轻松搞定 reduxjs/toolkit
592 0
|
前端开发
Antd中Table列表行默认包含修改及删除功能的封装
Antd中Table列表行默认包含修改及删除功能的封装
656 0
|
前端开发 数据格式
jeecgboot前端antd Table组件动态合并单元格
jeecgboot前端antd Table组件动态合并单元格
1180 0
|
数据安全/隐私保护
Ant Design Pro:权限管理
Ant Design Pro:权限管理
554 0
|
7月前
|
人工智能 前端开发 JavaScript
跨境电商shopify开发对接流程
Shopify接口开发已全面转向GraphQL核心,深度融合Shopify Functions(Wasm边缘逻辑)与Shopify Magic(AI赋能)。涵盖Partner账号搭建、OAuth权限管理、Webhook实时推送、AI驱动的元数据生成与智能分析,并强调API版本锁定、安全调用及App审核合规。(239字)
antd table合并行或者列(动态添加合并行、列)
在Ant Design的Table组件中实现行或列的合并,通过动态计算数据源中的`rowSpan`或`colSpan`属性来控制,支持对特定字段进行行或列合并,包括多选框列的合并处理。
2579 3
antd table合并行或者列(动态添加合并行、列)
|
存储 索引
antd中table组件选中单行换样式(比如背景颜色)
在Ant Design (antd)的Table组件中,可以通过设置`onRow`属性来定义行点击事件,从而改变被点击行的样式,如背景颜色。`onRow`是一个函数,返回一个对象,对象包含事件处理函数,如`onClick`。同时,使用`rowClassName`属性来指定行的类名,结合状态管理,可以实现点击某行后改变其背景颜色的效果。具体实现时,需要在组件状态中保存当前被点击行的索引,然后通过`rowClassName`判断并返回相应的类名。
1977 2
antd中table组件选中单行换样式(比如背景颜色)
|
前端开发 UED 开发者
React 日期时间选择器 (DateTime Picker): 从基础到高级
本文详细介绍了如何在React应用中集成日期时间选择器,重点讲解了`react-datepicker`和Material-UI的`DatePicker`组件的安装、基本用法、自定义日期格式和设置日期范围的方法。同时,文章还探讨了常见问题及其解决方法,帮助开发者避免易错点,确保在项目中顺利集成日期时间选择功能。
1253 3
|
存储 缓存 前端开发
灵魂拷问-前端的作用--性能优化篇
作者最近在尝试对负责的平台进行性能优化,本文整理了些前端性能优化的一些常见策略。
|
前端开发 计算机视觉
React使用antd实现可编辑单元格
React结合Ant Design实现可编辑单元格的表格组件,通过EditableRow和EditableCell封装实现单元格编辑功能,并提供saveFun回调保存编辑内容。
854 1