ant design 中实现表格头部可删除和添加
我是用antd pro做一个项目。有一个小需求是表格头部栏可操作。具体是表头的每一项都带一个“x”按钮,当不想展示这一栏的时候,直接点“x”,这一栏就不展示了。不展示的头部标签放一边,也可以随时加入到表格中。
先看图:
1.表头信息有个“x”,当点击了某一项,该项在表格中消失,并且该标签会在可添加表头上展示。
2.此时我“x”掉了序号,用户名两项,得到下面的效果。
3.此时我点击了 “+用户名” 标签,表格中再次展示了用户名,同时,可添加表头栏少了 “+用户名” 标签。
这项功能在ant design 上的表格组件是没有的,所以我自己在现有组件的基础上实现的。如果你想看懂下面的代码,你需要了解ant design 的表格组件的使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | import React from 'react' ; import { connect } from 'dva' ; import GridContent from '@/components/PageHeaderWrapper/GridContent' ; import { Table, Card, Form, Input, Button, Tag, Select, Row, Col, DatePicker, message, Tooltip, Icon, } from 'antd' ; import { ABNORMAL_AUDIT_STATUS, ABNORMAL_AUDIT_STATUS_COLOR } from '@/constants' ; import moment from 'moment' ; const { Option } = Select; const { RangePicker } = DatePicker; const FormItem = Form.Item; const risk = [ '无' , '低' , '中' , '高' , '严重' ]; @connect(({ riskEvent, abnormalList, loading }) => ({ riskEvent, abnormalList, loading: loading.models.abnormalList, })) @Form.create() class AbnormalList extends React.Component { constructor(props) { super (props); this .state = { selectedData: [], newCol: [], colKey: { id: 0, username: 1, riskLevel: 2, riskScene: 3, department: 4, createTime: 5, status: 6, baseInfo: 7, operatePlatform: 8, }, columns: [ { title: ( <div> <a onClick={() => this .columsControl( 'id' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a>{ ' ' } 序号 </div> ), dataIndex: 'id' , key: '序号' , }, { title: ( <div> <a onClick={() => this .columsControl( 'username' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a>{ ' ' } 用户名 </div> ), dataIndex: 'username' , key: '用户名' , }, { title: ( <div> <a onClick={() => this .columsControl( 'riskLevel' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a>{ ' ' } 风险等级 </div> ), dataIndex: 'riskLevel' , key: '风险等级' , sorter: true , sortDirections: [ 'descend' ], render: text => risk[text], }, { title: ( <div> <a onClick={() => this .columsControl( 'riskScene' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a>{ ' ' } 风险场景 </div> ), dataIndex: 'riskScene' , key: '风险场景' , render: riskScene => { const isLong = riskScene.length > 10; return ( <span> {isLong ? ( <Tooltip title={riskScene}>`${riskScene.slice(0, 10)}...`</Tooltip> ) : ( <span>{riskScene}</span> )} </span> ); }, }, { title: ( <div> <a onClick={() => this .columsControl( 'department' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a>{ ' ' } 部门 </div> ), dataIndex: 'department' , key: '部门' , render: dep => { const isLong = dep.length > 10; return ( <span> {isLong ? ( <Tooltip title={dep}>`${dep.slice(0, 10)}...`</Tooltip> ) : ( <span>{dep}</span> )} </span> ); }, }, { title: ( <div> <a onClick={() => this .columsControl( 'createTime' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a>{ ' ' } 告警时间 </div> ), dataIndex: 'createTime' , key: '告警时间' , }, { title: ( <div> <a onClick={() => this .columsControl( 'status' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a>{ ' ' } 状态 </div> ), dataIndex: 'status' , key: '状态' , render: status => ( <Tag color={ABNORMAL_AUDIT_STATUS_COLOR[status]}>{ABNORMAL_AUDIT_STATUS[status]}</Tag> ), }, { title: ( <div> <a onClick={() => this .columsControl( 'baseInfo' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a> 文件名 </div> ), dataIndex: 'baseInfo' , key: '文件名' , render: text => { const jsonData = JSON.parse(text); const name = jsonData.filename; return name ? <span>{name}</span> : '' ; }, }, { title: ( <div> <a onClick={() => this .columsControl( 'operatePlatform' )}> <Icon style={{ fontSize: '6px' }} type= "close" /> </a> 操作平台 </div> ), dataIndex: 'operatePlatform' , key: '操作平台' , }, { title: '操作' , fixed: 'right' , width: 80, render: (_, record) => ( <div> {record.status === 0 ? ( <RoamMoudle onOk={ this .pushRoma}> <a onClick={() => { this .roamId(record.id); }} > 流转 </a> </RoamMoudle> ) : ( <span>流转</span> )} </div> ), }, ], }; } componentWillMount() { const { dispatch } = this .props; dispatch({ type: 'abnormalList/fetchList' , payload: { page: 1, pageSize: 10, filter: [], }, }); } columsControl = str => { const { colKey, columns, newCol } = this .state; const id = colKey[str]; newCol.push(columns[id]); delete columns[id]; // 此处用delete方便后续添加某项表头的时候能回到最开始的位置 this .setState({ columns, newCol }); }; addCol = (one, str, i) => { const { colKey, columns, newCol } = this .state; const id = colKey[str]; columns.splice(id, 1, one); // 替换 newCol.splice(i, 1); // 删除 this .setState({ columns, newCol }); }; handleTableChange = (pagination, filters, sorter) => { const { current, pageSize } = pagination; const { dispatch, abnormalList: { filter }, } = this .props; dispatch({ type: 'abnormalList/fetchList' , payload: { page: current, pageSize, filter, sorter, }, }); }; render() { const { abnormalList, loading } = this .props; const { selectedData, columns, newCol } = this .state; const { data, page, total } = abnormalList; const rowSelection = { onChange: (selectedRowKeys, selectedRows) => { console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ' , selectedRows); this .setState({ selectedData: selectedRowKeys }); }, getCheckboxProps: record => ({ disabled: record.name === 'Disabled User' , // Column configuration not to be checked name: record.name, }), }; return ( <GridContent> <Card bordered={ false }> <div style={{ border: '1px solid #CAE7FD' , padding: '5px 10px' , borderRadius: '4px' , background: '#E9F7FE' , }} > 可添加表头: {newCol.map((item, index) => ( <Tag key={item.dataIndex}> <a onClick={() => { this .addCol(item, item.dataIndex, index); }} > +{item.key} </a> </Tag> ))} </div> <br /> <Table columns={columns} dataSource={data} loading={loading} rowSelection={rowSelection} scroll={{ x: 1300 }} rowKey= "id" pagination={{ showSizeChanger: true , current: page, total, pageSizeOptions: [ '10' , '20' , '50' , '100' ], }} onChange={ this .handleTableChange} /> </Card> </GridContent> ); } } export default AbnormalList; |
主要是在columns 中的title中添加点击事件。columsControl,addCol两个函数是实现的关键。
转载请说明来源,谢谢
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步