解決Antd Table表頭加Icon和氣泡提示的坑
對于Antd Table組件的columns數(shù)組來說,它需要接受2個(gè)屬性(filterDropdown,filterIcon)才能在表頭某個(gè)屬性旁展示圖標(biāo)Icon:
columns: [{
title: '表達(dá)式', dataIndex: 'formulaTenderAmount', key: 'formulaTenderAmount', width: 150, placeholder: '請輸入表達(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í)添加懸浮和排序,另,只需支持文字懸浮對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 沒有提供兩者同時(shí)存在的api;直接添加sorter,同時(shí)對我們的title封裝方法,出現(xiàn)點(diǎn)擊排序,只會觸發(fā)單一的一個(gè)排序,這不是我們最終達(dá)成的結(jié)果。那么在不對title做處理的情況下,實(shí)現(xiàn)信息提示和排序的方法
解決
const columns = [{
title: '姓名',
dataIndex: 'name',
key: 'name',
sorter: true, // 實(shí)現(xiàn)排序Icon出現(xiàn),開始交互排序
filterDropdown: true, // 自定義的列篩選功能,我們占位為信息提示Icon的位置
filterIcon: () => {
return (
<Tooltip placement="top" onVisibleChange={() => onVisibleChange(1)}>
// 在這不寫title的原因是ant design 內(nèi)部有很多title,內(nèi)部結(jié)構(gòu)并沒有對特殊的情況做處理,只接收一個(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),類似onmouseenter 進(jìn)入離開事件,用來顯示我們不同的信息提醒
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)是防止控制臺的報(bào)錯(cuò),移除以后造成filterTitle為空,失??;
}}
/>
樣式需要自己去調(diào)整
簡易解釋
ant design table 中 filterIcon api 相關(guān)的源碼解析 ,一些我們未能解決的問題,我們可以通過研究源代碼去分析或可供我們
使用的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,來解決我們現(xiàn)實(shí)遇到的問題
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)的具體過程,小編不才,只展示部分實(shí)現(xiàn)的過程,詳細(xì)的原理小編未給出,敬請諒解...
好了~ 回歸正題吧!
如此,我改成了以下的代碼,并且新增了onVisibleChange方法,新增了state的屬性filterTitleKey,并且在Table組件屬性中增加了locale對象:
this.state = {
filterTitleKey: '',
}
columns: [{
title: '表達(dá)式', dataIndex: 'formulaTenderAmount', key: 'formulaTenderAmount', width: 150, placeholder: '請輸入表達(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),類似onmouseenter 進(jìn)入離開事件,用來顯示我們不同的信息提醒
let str = '';
switch (key) {
case 1:
str = '函數(shù)計(jì)算,x表示發(fā)行規(guī)模';
default:
break;
}
this.setState({
filterTitleKey: str,
});
}
這邊會有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)是防止控制臺的報(bào)錯(cuò),移除以后造成filterTitle為空,失??;
}}
dataSource={dataSource}
pagination={pagination}
scroll={{ x: 2400, y: tableScrollHeight }}
/>
這樣就能正常的顯示氣泡文本了:


以上這篇解決Antd Table表頭加Icon和氣泡提示的坑就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)標(biāo)簽頁切換/制作tab組件詳細(xì)教程
在項(xiàng)目開發(fā)中需要使用vue實(shí)現(xiàn)tab頁簽切換功能,所以這里總結(jié)下,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)標(biāo)簽頁切換/制作tab組件的相關(guān)資料,需要的朋友可以參考下2023-11-11
解決VUE自定義拖拽指令時(shí) onmouseup 與 click事件沖突問題
這篇文章主要介紹了解決VUE自定義拖拽指令時(shí) onmouseup 與 click事件沖突問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Vue 樣式切換及三元判斷樣式關(guān)聯(lián)操作
這篇文章主要介紹了Vue 樣式切換及三元判斷樣式關(guān)聯(lián)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
基于vue-cli3+typescript的tsx開發(fā)模板搭建過程分享
這篇文章主要介紹了搭建基于vue-cli3+typescript的tsx開發(fā)模板,本文通過實(shí)例代碼截圖的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Vue 權(quán)限控制的兩種方法(路由驗(yàn)證)
這篇文章主要介紹了Vue 權(quán)限控制的兩種方法(路由驗(yàn)證),每種方法給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
親自動(dòng)手實(shí)現(xiàn)vue日歷控件
這篇文章主要記錄了親自動(dòng)手實(shí)現(xiàn)vue日歷控件的詳細(xì)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06

