antd 设置表头属性_动态向表头添加列的表格(基于antdesign)

该博客介绍了如何在antd表格中动态添加列并设置表头属性。通过自定义组件HeaderSettingTable,可以实现点击图标展示下拉框选择要添加的列,并在确认后将所选列添加到表格头部。同时,博客提供了组件的代码实现和相关参数说明。

git地址的compont文件夹中

表格效果

使用参数的参数列表:

下拉框实现的效果和antdesign自己声明的过滤器效果一样,但是由于我们的操作逻辑和过滤的逻辑不尽相同,所以自己封装了点击下拉的那个部分。

参数使用详情

表格源代码:

/**

*

* dataSource用来传入表格中想要显示的数据

* headerSetting用来传入初始表头的选项内容 eg:[{ title: '序号', dataIndex: 'order', key: 'num' }]

* choice用来传入点击图标后,出现的复选框的内容 eg:[{ title: 'Male0', key: 'male0', dataIndex: 'male0' }]

* onInquireItem={this.onInquireItem} 对应操作中的查询按钮

* onCloseItem={this.onInquireItem} 对应操作中的关闭按钮

* onDeleteItem={this.onInquireItem} 对应操作中的删除按钮

*/

import React, { Component } from 'react';

import { Icon, Table, Checkbox } from 'antd';

import { findIndex } from 'lodash';

import iconFont from 'Assets/iconfont.js';

import PropTypes from 'prop-types';

import './style.less';

const { Column } = Table;

const IconFont = Icon.createFromIconfontCN({

script: iconFont

})

class HeaderSettingTable extends Component {

state = { tableHeader: [], tableHeaderOlder: [], additionChoice: [], showChoice: false, checkState: [] }

// 绑定鼠标的点击事件

componentWillUnmount() {

window.removeEventListener('mouseup', this.handleMouseUp)

}

// 当页面挂载的时候,将需要的数据进行整理

componentDidMount() {

let olderHeader = [];

for (let i = 0; i < this.props.choice.length; i++) {

this.state.checkState.push(false)

}

olderHeader = JSON.parse(JSON.stringify(this.props.headerSetting));

window.addEventListener('mouseup', this.handleMouseUp)

this.setState({

tableHeader: this.props.headerSetting,

tableHeaderOlder: olderHeader

});

}

//将不必要的数据置空

resetCheckBox = (arr, flag) => {

let num = 0;

for (let i = 0; i < arr.length; i++) {

if (arr[i] === true) {

num++

}

}

if (flag && num !== this.state.additionChoice.length) {

for (let i = 0; i < this.props.choice.length; i++) {

let result = findIndex(this.state.additionChoice, this.props.choice[i]);

if (result === -1) {

arr[i] = false;

} else {

arr[i] = true;

}

}

this.setState({

checkState: arr

})

}

}

//点击页面空白区域,更多弹层消失

handleMouseUp = (newData) => {

let arr = JSON.parse(JSON.stringify(this.state.checkState));

if (this.state.additionChoice.length === 0) {

for (let i = 0; i < this.state.additionChoice.length; i++) {

arr[i] = false;

this.setState({

checkChoice: arr

})

}

}

document.onmouseup = (event) => {

let flag = event.target.id !== 'choiceModal' &&

event.target.parentElement.id !== 'iconId' &&

(event.target.parentElement.parentElement.parentNode.id || event.target.parentElement.parentElement.id) !== 'choiceModal'

this.resetCheckBox(newData, flag);

if (event.target.id !== 'choiceModal' &&

event.target.parentElement.id !== 'iconId') {

this.setState({

showChoice: false

})

}

}

}

// 点击多选框的时候,对多选框中的值进行查看

checkChoice = (index, event) => {

let newData = JSON.parse(JSON.stringify(this.state.checkState));

newData[index] = !newData[index];

this.setState({

showChoice: true,

checkState: newData

});

let value = JSON.parse(JSON.stringify(newData))

this.handleMouseUp(value)

}

// 点击确认按钮的时候,将选中列加入到表格的表头中去

confirmClick = () => {

this.setState({

tableHeader: this.state.tableHeaderOlder,

});

for (let i = 0; i < this.state.checkState.length; i++) {

let index = findIndex(this.state.additionChoice, this.props.choice[i]);

if (this.state.checkState[i]) {

if (index === -1) {

this.state.additionChoice.push(this.props.choice[i]);

}

}

else {

if (index !== -1) {

this.state.additionChoice.splice(index, 1);

}

}

}

let tempArr = JSON.parse(JSON.stringify(this.state.tableHeaderOlder));

for (let i = this.state.additionChoice.length - 1; i > -1; i--) {

let objectItem = this.state.additionChoice[i]

tempArr.splice(2, 0, objectItem);

this.setState({

tableHeader: tempArr

});

}

}

// 点击取消按钮

clearClick = () => {

let clearArr = [];

for (let i = 0; i < this.props.choice.length; i++) {

clearArr[i] = false;

}

let tempArr = JSON.parse(JSON.stringify(this.state.tableHeaderOlder));

this.setState({

tableHeader: tempArr,

checkState: clearArr,

additionChoice: []

});

}

// 点击图标的

clickIcon = (event) => {

this.setState({

showChoice: !this.state.showChoice

})

}

operateCell = (text, recorder) => {

return (

this.props.onInquireItem(recorder)}>查询

this.props.onCloseItem(recorder)}>关闭

this.props.onDeleteItem(recorder)}>删除

)

}

// 下拉框的样式的编写

titleHandle = () => {

return (

操作

this.clickIcon(event)} />

'ant-table-filter-dropdown item-style show-item' : 'ant-table-filter-dropdown item-style hidden-item'} >

{this.props.choice.map((item, index) => {

return (

className='checkBox-style' key={item.title} checked={this.state.checkState[index]}>{item.key}

)

})}

this.confirmClick()} >确定

this.clearClick()} >重置

)

}

render() {

return (

{index + 1}} />

{

this.state.tableHeader.map((item) => {

return ()

})

}

this.operateCell(text, recorder)} />

)

}

}

HeaderSettingTable.propTypes = {

headerSetting: PropTypes.array,

onInquireItem: PropTypes.func,

onCloseItem: PropTypes.func,

onDeleteItem: PropTypes.func,

choice: PropTypes.array,

dataSource: PropTypes.array

}

export default HeaderSettingTable;

要在React项目中使用Ant Design的Table组件动态生成表头,需要完成以下步骤: 1. 定义表格数据源 首先需要定义表格的数据源,可以使用一个数组来存储表格的数据,例如: ```javascript const data = [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', }, ]; ``` 2. 定义表头数据源 接下来需要定义表头的数据源,可以使用一个数组来存储表头的数据,例如: ```javascript const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ]; ``` 3. 动态生成表头 在Ant Design的Table组件中,可以使用columns属性来指定表头数据源。为了动态生成表头,需要在渲染组件时动态生成表头的数据源,并将其传递给Table组件的columns属性,例如: ```javascript import React, { useState } from 'react'; import { Table } from 'antd'; const DynamicTable = () => { const [columns, setColumns] = useState([ { title: 'Name', dataIndex: 'name', key: 'name', }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ]); const handleAddColumn = () => { const newColumn = { title: `Column ${columns.length + 1}`, dataIndex: `column${columns.length + 1}`, key: `column${columns.length + 1}`, }; setColumns([...columns, newColumn]); }; return ( <> <button onClick={handleAddColumn}>Add Column</button> <Table dataSource={data} columns={columns} /> </> ); }; export default DynamicTable; ``` 在上面的代码中,我们使用useState钩子来定义表头的数据源,并渲染一个添加的按钮。当点击添加按钮时,会动态生成一个新的,并将其添加表头的数据源中。这样就可以动态生成表头了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值