基于react+antd可编辑,可增删改表格方案

  1. 可编辑表格:如何实现?

 方案1:在table的columns的render里渲染Form.Item,同时需要自己考虑dataSource的增删逻辑。

代码:

  • columns的render里渲染Form.Item
{
        title: "点击动作",
        dataIndex: "action",
        width: "200px",
        render(text, field, index) {
          return (
            <Form.Item
              name={[field.name, "action"]}
              wrapperCol={{ span: 24 }}
              initialValue={"link"}
              shouldUpdate
              >
              <Select
                style={{
                  marginRight: "8px",
                }}
                placeholder="请选择"
                onChange={(e)=>changeAction(e,index)}
                >
                <Option value={"subFlow"}>跳转路由</Option>
                <Option value={"link"}>跳转外部链接</Option>
                <Option value={"text"}>发送文字消息</Option>
                <Option value={"miniprogram"}>跳转小程序</Option>
              </Select>
            </Form.Item>
          )
        },
      }

方案2:Form.List的增(add)、删(remove)功能结合table的样式

代码:

  • Form.List 包裹table,传递add,remove,整个table数据就是一个Form.List
      <Form.Item label="按钮">
        <Form.List name="buttons">
          {(fields, { add, remove }, index) => {
            return (
                <Table
                  bordered
                  scroll={{ x: 900 }}
                  pagination={false}
                  className="infoTable"
                  size={"small"}
                  rowKey="index"
                  dataSource={fields}
                  columns={this.getMapColumns(remove)}
                  footer={
                    fields.length >= 10 ? null : () => this.setFooter(add)
                  }></Table>
            )
          }}
        </Form.List>
  • table的colums属性渲染Form.Item ,remove直接传到操作列
      {
        title: "操作",
        fixed: "right",
        align: "center",
        ellipsis: true,
        width: 100,
        render: (text, field) => (
          <div className="action-icons">
            <Tooltip title="删除" trigger="hover" placement="top">
              <Popconfirm
                placement="topRight"
                title="确定要删除该记录吗?"
                onConfirm={() => remove(field.name)}>
                <i
                  className="action-icon margin_icon iconfont icon-trash"
                  title="删除"
                />
              </Popconfirm>
            </Tooltip>
          </div>
        ),
      }
  • remove()传入index(上面的代码field.name 就是index),add()第一个参数可自定义初始化的值
  setFooter = (add) => {
    return (
      <Form.Item>
        <Button
          type="link"
          onClick={() => add({
            label:'',
            action:'link',
            actionContent:undefined,
            actionParams:[],
            actionPath:''

          })}
          block
          icon={<PlusCircleOutlined />}>
          添加按钮
        </Button>
      </Form.Item>
    )
  }

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值