React+antd实现table动态列添加删除

实现效果

核心思想:columns是数组,作为table表头数据,动态添加删除列,主要是对columns数组进行操作。定义columns[0]作为左侧表头,通过fixed固定在左侧。每一列绑定addColspan()和deleteColspan()函数和当前列的index,方便后续的添加删除时通过index修改columns数组。

本次的核心还包括,每次添加删除列元素后,都会对列元素进行重新编排index索引。

添加列:

colspanId:当前点击的列的索引,意味着要在该列后添加一列

column:当前要添加的列

rightColumns:当前点击列之后的集合

定义一个newColumns新数组,通过concat对当前点击列之前的所有列(包括当前点击列)、新添加列、当前点击列之后的所有列(不包括点击列)进行重组,通过map重新编排列的index索引,再把新数组赋值给columns进行列表渲染

1、添加列核心代码

addColspan = (colspanId) => {
      console.log(colspanId,'colspanId')
        let { columns, data, id} = this.state
        let column = {
            title: '',
            dataIndex: '',
            render:(text,record,index)=>{
                return {
                    children:text,
                    props:{
                        style:{
                            minWidth: "100px",
                        }
                    }
                }
            }
        }
        let newColumns = []
        const rightColumns = columns.splice(colspanId + 1)
        console.log(rightColumns,'rightColumns')
        newColumns = columns
          .concat(column)
          .concat(rightColumns )
          .map((item,index)=>
            ({
              ...item, 
              title: <div>{index != 0 ?<Icon type="minus-circle" onClick={()=>this.deleteColspan(index)}/> : null}<Icon type="plus-circle" className="add-icon" onClick={()=>this.addColspan(index)}/></div>,
              dataIndex: 'column-' + index})
          )
        columns = [...newColumns]
        let arr = []

        arr = data.map((item,index)=>{
            const obj = item
            obj[`column-${id}`] = id
            return obj
        })
        id += 1
        this.setState({
            columns,
            data:arr,
            id,
            showOptions: columns.map(item => item.dataIndex)
        })
    }

类似于添加列操作,获取当前点击的列的索引,通过filter过滤掉该索引的列,并且重新编排index

2、删除列核心代码

deleteColspan = (colspanId)=>{
      console.log(colspanId,'colspanId')
      let {columns} = this.state
      let currentColspanId = 'column-'+colspanId
      columns = columns.filter(item=>item.dataIndex != currentColspanId)
      columns = columns.map((item,index)=>({
        ...item, 
        title: <div>{index != 0 ?<Icon type="minus-circle" onClick={()=>this.deleteColspan(index)}/> : null}<Icon type="plus-circle" className="add-icon" onClick={()=>this.addColspan(index)}/></div>,
        dataIndex: 'column-' + index
      }))
      this.setState({
        columns
      })
    }

 3、完整代码

class Cp extends Component {
    constructor(props) {
        super(props)

        this.state = {
            columns: [
                { title: <div><Icon type="plus-circle" onClick={()=>this.addColspan(0)} className="add-icon"/></div>, dataIndex: 'index', fixed: 'left', width: 110, render: (text, record, index) => {
                    return (index == 0 && "序号") || (index == 1 && "字段1") || (index == 2 && "字段2") || (index == 3 && "字段3") 
                }}
            ],
            data:[
                { head:""},
                { head:""},
                { head:""},
                { head:""},
            ],
            id:1,
        }
    }

    addColspan = (colspanId) => {
      console.log(colspanId,'colspanId')
        let { columns, data, id} = this.state
        let column = {
            title: '',
            dataIndex: '',
            render:(text,record,index)=>{
                return {
                    children:text,
                    props:{
                        style:{
                            minWidth: "100px",
                        }
                    }
                }
            }
        }
        let newColumns = []
        const rightColumns = columns.splice(colspanId + 1)
        console.log(rightColumns,'rightColumns')
        newColumns = columns
          .concat(column)
          .concat(rightColumns )
          .map((item,index)=>
            ({
              ...item, 
              title: <div>{index != 0 ?<Icon type="minus-circle" onClick={()=>this.deleteColspan(index)}/> : null}<Icon type="plus-circle" className="add-icon" onClick={()=>this.addColspan(index)}/></div>,
              dataIndex: 'column-' + index})
          )
        columns = [...newColumns]
        let arr = []

        arr = data.map((item,index)=>{
            const obj = item
            obj[`column-${id}`] = id
            return obj
        })
        id += 1
        this.setState({
            columns,
            data:arr,
            id,
            showOptions: columns.map(item => item.dataIndex)
        })
    }

    deleteColspan = (colspanId)=>{
      console.log(colspanId,'colspanId')
      let {columns} = this.state
      let currentColspanId = 'column-'+colspanId
      columns = columns.filter(item=>item.dataIndex != currentColspanId)
      columns = columns.map((item,index)=>({
        ...item, 
        title: <div>{index != 0 ?<Icon type="minus-circle" onClick={()=>this.deleteColspan(index)}/> : null}<Icon type="plus-circle" className="add-icon" onClick={()=>this.addColspan(index)}/></div>,
        dataIndex: 'column-' + index
      }))
      this.setState({
        columns
      })
    }

    render() {
        const { columns, data} = this.state

        return (
          <Table
            columns={columns}
            dataSource={data}
            rowKey="id"
            bordered={true}
            pagination={false}
          />
        )
    }
}

export default createForm()(Cp)

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
React中,使用Ant Design的Table组件,在表格添加操作并结合Dropdown和Menu组件可以实现更丰富的交互效果。 首先,我们需要导入相关的组件和样式: ```jsx import { Table, Dropdown, Menu, Button } from 'antd'; import { DownOutlined } from '@ant-design/icons'; import 'antd/dist/antd.css'; ``` 然后,在表格的columns中定义操作,并使用Dropdown和Menu组件来定制操作菜单: ```jsx const columns = [ ...其他定义, { title: '操作', key: 'action', render: (text, record) => ( <Dropdown overlay={menu}> <Button> 操作 <DownOutlined /> </Button> </Dropdown> ), }, ]; const menu = ( <Menu> <Menu.Item key="1"> <a target="_blank" rel="noopener noreferrer" href="#"> 编辑 </a> </Menu.Item> <Menu.Item key="2"> <a target="_blank" rel="noopener noreferrer" href="#"> 删除 </a> </Menu.Item> </Menu> ); ``` 以上代码中,columns定义了表格,其中操作使用render属性来渲染。在render方法中,我们使用Dropdown组件来包裹Button,并将Menu作为overlay传入。 menu定义了操作菜单的内容,其中使用Menu.Item来定义菜单项,通过设置key属性来区分不同的菜单项。菜单项可以是一段文本或者其他React元素,这里我们使用a标签来模拟菜单项的操作。 最后,在Table组件中将columns配置为表格: ```jsx <Table columns={columns} dataSource={data} /> ``` 以上是在React中使用Ant Design的Table组件添加操作并结合Dropdown和Menu的写法,通过这种方式,我们可以方便地在表格中定制各种操作的菜单。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小灰灰学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值