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

目录
相关文章
|
2天前
|
前端开发 JavaScript
react 修改 antdesign 的 组件默认样式
react 修改 antdesign 的 组件默认样式
|
1天前
|
资源调度 JavaScript 前端开发
将Elementui里的Table表格做成响应式并且和Pagination分页组件封装在一起(Vue实战系列)
将Elementui里的Table表格做成响应式并且和Pagination分页组件封装在一起(Vue实战系列)
|
2天前
|
JavaScript
vue页面如何单独给背景色全方案
vue页面如何单独给背景色全方案
|
6月前
|
JavaScript 前端开发
Vue中动态树形菜单,以及
Vue中动态树形菜单,以及
|
8月前
|
JavaScript 前端开发 UED
Vue中的动态DOM加载:实现更灵活的前端界面
Vue.js是一个流行的JavaScript框架,提供了强大的工具来构建交互式和动态的前端界面。在本博客中,我们将深入探讨Vue中动态加载DOM的方法和实例,以帮助您创建更灵活、响应式的用户界面。
264 0
|
9月前
|
JavaScript
详解——Vue3列展示功能及原理
详解——Vue3列展示功能及原理
133 0
|
JavaScript 前端开发
Vue 打包后自定义样式无法覆盖elementUI组件原有样式问题
Vue 打包后自定义样式无法覆盖elementUI组件原有样式问题
238 0
antd组件库封装74-subMenu下拉菜单编码-完美组件2
antd组件库封装74-subMenu下拉菜单编码-完美组件2
63 0
antd组件库封装74-subMenu下拉菜单编码-完美组件2
antd组件库封装42-样式解决方案分析
antd组件库封装42-样式解决方案分析
89 0
antd组件库封装42-样式解决方案分析
|
JavaScript
vue动态组件的应用场景讲解-以tab切换效果为例
vue动态组件的应用场景讲解-以tab切换效果为例
184 0