解決Antd Table表頭加Icon和氣泡提示的坑
對(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),類似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),類似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 }}
/>
這樣就能正常的顯示氣泡文本了:


以上這篇解決Antd Table表頭加Icon和氣泡提示的坑就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue雙擊事件2.0事件監(jiān)聽(tīng)(點(diǎn)擊-雙擊-鼠標(biāo)事件)和事件修飾符操作
這篇文章主要介紹了vue雙擊事件2.0事件監(jiān)聽(tīng)(點(diǎn)擊-雙擊-鼠標(biāo)事件)和事件修飾符操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue computed無(wú)法得到this的屬性或方法的解決
這篇文章主要介紹了vue computed無(wú)法得到this的屬性或方法的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue圖片加載失敗時(shí)用默認(rèn)圖片替換的方法
這篇文章主要給大家介紹了關(guān)于vue圖片加載失敗時(shí)用默認(rèn)圖片替換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
如何在Vue項(xiàng)目中應(yīng)用TypeScript類
與如何在React項(xiàng)目中應(yīng)用TypeScript類類似在VUE項(xiàng)目中應(yīng)用typescript,我們需要引入一個(gè)庫(kù)vue-property-decorator,需要的小伙伴可續(xù)看下文具體介紹2021-09-09
Vue?ECharts實(shí)現(xiàn)機(jī)艙座位選擇展示功能代碼詳解
這篇文章主要介紹了Vue?ECharts實(shí)現(xiàn)機(jī)艙座位選擇展示,本文給大家分享一段簡(jiǎn)短的代碼通過(guò)效果圖展示給大家介紹的非常明白,需要的朋友可以參考下2022-05-05
Vue實(shí)現(xiàn)二維碼數(shù)組的全選與反選功能
在開(kāi)發(fā)Web應(yīng)用程序時(shí),表格數(shù)據(jù)的展示和操作是非常常見(jiàn)的需求之一,特別是在處理表格中的復(fù)選框選擇時(shí),我們經(jīng)常需要實(shí)現(xiàn)全選、反選等功能,這篇文章將帶你深入了解如何在Vue.js中實(shí)現(xiàn)對(duì)二維數(shù)組數(shù)據(jù)的全選和反選功能,需要的朋友可以參考下2024-09-09

