您好,登錄后才能下訂單哦!
本篇文章為大家展示了Antd中Table表頭如何實(shí)現(xiàn)加Icon,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
對(duì)于Antd Table組件的columns數(shù)組來(lái)說(shuō),它需要接受2個(gè)屬性(filterDropdown,filterIcon)才能在表頭某個(gè)屬性旁展示圖標(biāo)Icon:
columns: [{ title: '表達(dá)式', dataIndex: 'formulaTenderAmount', key: 'formulaTenderAmount', width: 150, placeholder: '請(qǐng)輸入表達(dá)式', filterDropdown: (<div></div>), filterIcon: <Tooltip placement="top" title="氣泡懸浮提示文本" > <Icon type='question-circle-o' style={{ marginLeft: 1 }} /> </Tooltip>, },{ title: '操作', dataIndex: 'operation', key: 'operation', width: 305, fixed: 'right', }, ],
然后,結(jié)果是怎樣呢?
結(jié)果是氣泡提示框的文本并不是我們期望的 “氣泡懸浮提示文本” ,而是 “篩選” 兩個(gè)字
為什么?
看這里吧~
react ant design 中如何在表頭中加個(gè)Icon和排序,懸浮icon又觸發(fā)Tooltip
需求:
本篇文章適用于表頭同時(shí)添加懸浮和排序,另,只需支持文字懸浮對(duì)title封一層方法即可eg:
const TooltipTitle = (text, title) => { // text 展示的thead title 展示的提醒文字 return ( <Fragment> <span style={{ marginRight: 8 }}>{text}</span> <Tooltip placement="top" title={title}> <Icon type="question-circle" theme="outlined" /> </Tooltip> </Fragment> ); };
ant design中的table中的thead支持信息提示和遠(yuǎn)程加載排序。
困難點(diǎn)
ant design 沒(méi)有提供兩者同時(shí)存在的api;直接添加sorter,同時(shí)對(duì)我們的title封裝方法,出現(xiàn)點(diǎn)擊排序,只會(huì)觸發(fā)單一的一個(gè)排序,這不是我們最終達(dá)成的結(jié)果。那么在不對(duì)title做處理的情況下,實(shí)現(xiàn)信息提示和排序的方法
解決
const columns = [{ title: '姓名', dataIndex: 'name', key: 'name', sorter: true, // 實(shí)現(xiàn)排序Icon出現(xiàn),開(kāi)始交互排序 filterDropdown: true, // 自定義的列篩選功能,我們占位為信息提示Icon的位置 filterIcon: () => { return ( <Tooltip placement="top" onVisibleChange={() => onVisibleChange(1)}> // 在這不寫(xiě)title的原因是ant design 內(nèi)部有很多title,內(nèi)部結(jié)構(gòu)并沒(méi)有對(duì)特殊的情況做處理,只接收一個(gè)title, // 并覆蓋不了默認(rèn)是篩選。 <Icon type="question-circle" theme="outlined" /> </Tooltip> ); }, }, { title: '年齡', dataIndex: 'age', key: 'age', }, { title: '住址', dataIndex: 'address', key: 'address', }]; onVisibleChange = (key) => { //Tooltip 顯示隱藏的回調(diào),類(lèi)似onmouseenter 進(jìn)入離開(kāi)事件,用來(lái)顯示我們不同的信息提醒 let str = ''; switch (key) { case 1: str = '你的姓名'; default: break; } this.setState({ filterTitleKey: str, }); } handleTableChange = (pagination, filters, sorter) => { console.log(pagination, filters, sorter); } <Table dataSource={dataSource} columns={columns} onChange={() => this.handleTableChange} locale={{ filterTitle: filterTitleKey || '默認(rèn)', // 設(shè)一個(gè)默認(rèn)是防止控制臺(tái)的報(bào)錯(cuò),移除以后造成filterTitle為空,失?。? }} />
樣式需要自己去調(diào)整
簡(jiǎn)易解釋
ant design table 中 filterIcon api 相關(guān)的源碼解析 ,一些我們未能解決的問(wèn)題,我們可以通過(guò)研究源代碼去分析或可供我們
使用的api方法。
renderFilterIcon = () => { const { column, locale, prefixCls, selectedKeys } = this.props; const filtered = selectedKeys && selectedKeys.length > 0; let filterIcon = column.filterIcon as any; if (typeof filterIcon === 'function') { filterIcon = filterIcon(filtered); } const dropdownIconClass = classNames({ [`${prefixCls}-selected`]: filtered, [`${prefixCls}-open`]: this.getDropdownVisible(), }); return filterIcon ? ( // 重點(diǎn)在這,官網(wǎng)提供了filterIcon api,并未提供filterTitle,來(lái)解決我們現(xiàn)實(shí)遇到的問(wèn)題 React.cloneElement(filterIcon as any, { title: locale.filterTitle, // 因源碼內(nèi)部有個(gè)title,我們實(shí)現(xiàn)讓它動(dòng)態(tài)展示,層疊掉默認(rèn)的title className: classNames(`${prefixCls}-icon`, dropdownIconClass, filterIcon.props.className), onClick: stopPropagation, }) ) : ( <Icon title={locale.filterTitle} // 同理上,供我們使用的api type="filter" theme="filled" className={dropdownIconClass} onClick={stopPropagation} /> ); };
有興趣的同學(xué)可以看一看完整的代碼,看看實(shí)現(xiàn)的具體過(guò)程,小編不才,只展示部分實(shí)現(xiàn)的過(guò)程,詳細(xì)的原理小編未給出,敬請(qǐng)諒解...
好了~ 回歸正題吧!
如此,我改成了以下的代碼,并且新增了onVisibleChange方法,新增了state的屬性filterTitleKey,并且在Table組件屬性中增加了locale對(duì)象:
this.state = { filterTitleKey: '', } columns: [{ title: '表達(dá)式', dataIndex: 'formulaTenderAmount', key: 'formulaTenderAmount', width: 150, placeholder: '請(qǐng)輸入表達(dá)式', filterDropdown: (<div></div>), filterIcon: <Tooltip onVisibleChange={() => this.onVisibleChange(1)} placement="top" > <Icon type='question-circle-o' style={{ marginLeft: 1 }} /> </Tooltip>, },{ title: '操作', dataIndex: 'operation', key: 'operation', width: 305, fixed: 'right', }, ], onVisibleChange = (key) => { //Tooltip 顯示隱藏的回調(diào),類(lèi)似onmouseenter 進(jìn)入離開(kāi)事件,用來(lái)顯示我們不同的信息提醒 let str = ''; switch (key) { case 1: str = '函數(shù)計(jì)算,x表示發(fā)行規(guī)模'; default: break; } this.setState({ filterTitleKey: str, }); }
這邊會(huì)有Table的一個(gè)屬性locate,官網(wǎng)是這樣解釋的:
<Table loading={loading} className='editableTable' size="small" style={{ height: tableHeight - 40 }} columns={columns} locale={{ filterTitle: filterTitleKey || '默認(rèn)', // 設(shè)一個(gè)默認(rèn)是防止控制臺(tái)的報(bào)錯(cuò),移除以后造成filterTitle為空,失?。? }} dataSource={dataSource} pagination={pagination} scroll={{ x: 2400, y: tableScrollHeight }} />
這樣就能正常的顯示氣泡文本了:
上述內(nèi)容就是Antd中Table表頭如何實(shí)現(xiàn)加Icon,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。