zoukankan      html  css  js  c++  java
  • EditableProTable使用+自定义封装

    项目使用的是ant-pro模板

    有个可编辑表格的需求,效果图如下
     
    ant-pro提供了EditableProTable 组件,我使用的是这种https://procomponents.ant.design/components/editable-table

    不过这里展示的默认都是input输入框,我这里是需要下拉框和上传组件

    下拉框可以直接用select组件,不过upload需要自己封装一下

    因为这个可编辑表格获取数据是根据绑定的value值,改变数据是onChange事件.upload组件没有value值,对应的是defaultFileList, 而onChange事件返回的是file.fileList.
    我们可以对这两个属性重新封装,改成需要的格式


    可编辑表格代码
    EditZTTable.tsx
    import React, { useState } from 'react'
    import { Row, Col, Select, Button } from 'antd'
    import type { ProColumns } from '@ant-design/pro-table'
    import { EditableProTable } from '@ant-design/pro-table'
    import ComUpload from '@/components/ComUpload'
    
    const { Option } = Select
    
    type DataSourceType = {
      id: React.Key
      title?: string
      role?: string
      state?: string
      created_at?: string
      [propName: string]: any
    }
    
    const EditZTTable: React.FC = () => {
      
      // 表格数据
      const defaultData: DataSourceType[] = [
        {
          id: 624748504,
          title: '活动名称一',
          role: '这个活动真好玩',
          state: 'open',
          created_at: '2020-05-26T09:42:56Z',
          // attachments: [
          //   {
          //     uid: '1',
          //     name: 'xxx.png',
          //     status: 'done',
          //     response: 'Server Error 500', // custom error message to show
          //     url: 'http://www.baidu.com/xxx.png',
          //   }
          // ]
        },
        {
          id: 624691229,
          title: '活动名称二',
          role: '这个活动真好玩',
          state: 'closed',
          created_at: '2020-05-26T08:19:22Z',
          attachments: [
            {
              uid: '1',
              name: 'xxx.png',
              status: 'done',
              response: 'Server Error 500', // custom error message to show
              url: 'http://www.baidu.com/xxx.png',
            },
            {
              uid: '2',
              name: 'yyy.png',
              status: 'done',
              url: 'http://www.baidu.com/yyy.png',
            }
          ]
        },
      ]
    
      // 设置选中的表格key
      const [editableKeys, setEditableRowKeys] = useState<React.Key[]>(() =>
        defaultData.map((item) => item.id),
      )
      // 设置表格数据
      const [dataSource, setDataSource] = useState<DataSourceType[]>(() => defaultData)
    
      const fileColumns: ProColumns<DataSourceType>[] = [
        {
          title: '序号',
          dataIndex: 'num',
           55,
          editable: false,
          render: (_, record, index) => (
            <span>{index + 1}</span>
          )
        },
        {
          title: '附件类型',
          dataIndex: 'businessType',
          renderFormItem: () => {
            return <Select showSearch>
              <Option value="jack">Jack</Option>
              <Option value="lucy">Lucy</Option>
            </Select>
          },
          formItemProps: {
            rules: [
              {
                required: true,
                whitespace: true,
                message: '此项是必填项',
              },
            ],
          },
        },
        {
          title: '附件上传',
           '50%',
          dataIndex: 'attachments',
          renderFormItem: () => (<ComUpload />),
        },
        {
          title: '操作',
          valueType: 'option',
           60,
          render: (text, record) => [
            <a
              key="delete"
              onClick={() => {
                setDataSource(dataSource.filter((item) => item.id !== record.id))
              }}
            >
              删除
            </a>,
          ],
        },
      ]
    
      return (
        <>
          <Row>
            <Col span={24}>
              <EditableProTable<DataSourceType>
                columns={columns}
                rowKey="id"
                value={dataSource}
                recordCreatorProps={{
                  newRecordType: 'dataSource',
                  record: () => ({
                    id: Date.now(),
                  }),
                }}
                editable={{
                  type: 'multiple',
                  editableKeys,
                  actionRender: (row, config, defaultDoms) => {
                    return [defaultDoms.delete]
                  },
                  onValuesChange: (record, recordList) => {
                    setDataSource(recordList)
                  },
                  onChange: (editableKeyss, editableRows: DataSourceType[]) => {
                    setEditableRowKeys(editableKeyss)
                    setDataSource(editableRows)
                  },
                }}
              />
            </Col>
          </Row>
          <Row>
            <Col span={24} style={{ textAlign: 'right' }}>
              <Button style={{ margin: '15px 8px 0' }} onClick={ () => {  }}>重置</Button>
              <Button type="primary" onClick={onSave}>保存</Button>
            </Col>
          </Row>
        </>
      )
    }
    
    export default EditZTTable
    upload组件封装
    ComUpload.tsx
    import React from 'react'
    import { Button, Upload } from 'antd'
    import { UploadOutlined } from '@ant-design/icons'
    
    type comuploadProps = {
      value?: {
        key: string;
        label: string;
      }[]
      onChange?: () => void
    }
    
    const ComUpload: React.FC<comuploadProps> = (props: comuploadProps) => {
      const attachments = props.value
      const { onChange } = props
    
      const action = `${URL_PREFIX}/file/upload`
      const changeFile = ({ file, fileList }) => {
        if (file.status !== 'uploading') {
          // console.log(file, fileList)
          const arr: any[] = []
          fileList.forEach((item: any) => {
            if (item.response) {
              arr.push({
                name: item.name,
                id: item.response.data
              })
            } else {
              arr.push(item)
            }
          })
          onChange(arr)
        }
      }
    
      return (
        <Upload 
          action={action}
          onChange={changeFile}
          defaultFileList={attachments}
        >
          <Button icon={<UploadOutlined />} type="text" />
        </Upload>
      )
    }
    
    export default ComUpload
  • 相关阅读:
    解决Error:com.intellij.util.indexing.StorageException
    Spring AOP (上)
    spring aop expression简单说明
    Spring AOP 详解
    基于Spring AOP实现对外接口的耗时监控
    JavaEE参考示例 SpringSide 4.0 GA版杀青
    MyBatis学习 之 四、MyBatis配置文件
    MyBatis学习 之 三、动态SQL语句
    MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
    MyBatis学习 之 二、SQL语句映射文件(1)resultMap
  • 原文地址:https://www.cnblogs.com/steamed-twisted-roll/p/14583341.html
Copyright © 2011-2022 走看看