antd design table更改某行數(shù)據(jù)的樣式操作
antd Table里面有一個rowClassName方法 表格行的類名 Function(record, index):string-->返回類型是String類型。
例子:
import styless from './component/record.css';--->引入css樣式。
css:
.csbsTypes{
font-family:微軟雅黑, "Open Sans", "宋體";
font-weight: bold;
}
代碼:
<Table
title={()=><div style={{textAlign: 'center',backgroundColor:'#170A29'}}></div>}
columns={R1columns}
dataSource={this.state.RateData}
pagination={false}
rowClassName={(record, index) => record.csbsType ==='不限范圍'?'csbsTypes':''}-->因為rowClassName方法返回的是String類型,所以直接將樣式名字填進去,就會自動查找此樣式
/>
顯示結(jié)果就是:在csbsType ===“不限范圍“”的這一行字體會被加粗```
補充知識:vue 給ant design table 組件自定義點擊行(選中行)樣式和斑馬紋樣式
寫在開頭:
element-ui的table組件有幾個屬性很好用,但是ant-design中table組件是沒有的。
比如:
stripe: 是否為斑馬紋 table。
highlight-current-row: 是否要高亮當前行。
當然,還有好幾個其他的屬性,但是本文先只講這兩個。既然element-ui有,ant-design沒有,那我在用ant-design的table組件時,想要實現(xiàn)這兩個功能怎么辦?
答案是涼拌。既然它沒有,那就自己寫,也就是二次封裝。
ok,先來實現(xiàn)這個屬性的功能:highlight-current-row。
highlight-current-row
首先,當然是給定義prop變量:highlightCurrentRow;再定義一個另外一個prop變量currentRow。
然后在watch中監(jiān)聽currentRow的變化,每次當currentRow變化的時候,渲染一下上一個選中行和當前選中行的樣式。
currentRow (val) {
if (this.highlightCurrentRow) {
this.renderRowStyleAfterRowClick(val)
}
}
highlightCurrentRow為true的時候,才需要渲染新的樣式。
renderRowStyleAfterRowClick:
// 選中某一行后,渲染表格行的樣式
renderRowStyleAfterRowClick (currentRow) {
const elements = document.getElementsByClassName('ant-table-row')
const rowSelectionElement = document.getElementsByClassName('row-selection')
// 獲取上一個選中行,并移除它的選中樣式
if (rowSelectionElement.length > 0) {
rowSelectionElement[0].classList.remove('row-selection')
}
// 給當前選中行添加選中行的樣式
if (elements.length > 0) {
const rowList = [...elements]
rowList.find(t => t.dataset.rowKey === currentRow.id).classList.add('row-selection')
}
}
代碼其實很簡單:
先拿表格當前頁的所有row元素(table組件沒有提供當前點擊行的原生class)和當前選中row元素。
如果當前有選中的行,先移除這個之前添加過的css class 'row-selection'。
然后再給當前選中行添class 'row-selection'。
那個這里就有疑問了,我怎么樣才能找到當前行呢?table組件并沒有提供當前選中行的class(至少我沒有找到),所有我只能t通過class name 'ant-table-row' 拿到所有row, 然后再從中找出你當前點擊的那一行。
這個時候需要利用一個很關(guān)鍵的屬性: rowKey。
還記得ant-design table組件的api文件最后面的那個注意嗎?

這里提醒你,rowKey用來指定數(shù)據(jù)列的住建,也就是每一行的唯一標志,那么好辦了 。
我們引用table組件的時候,將rowKey設(shè)置為表格數(shù)據(jù)源的主鍵,這樣我們就能從元素中的dataset中獲取到rowKey,然后找出當前點擊行。
rowList.find(t => t.dataset.rowKey === currentRow.id)
然后給這個元素動態(tài)添加class ‘'row-selection'。
// 給表格添加懸停樣式和當前點擊行添加選中樣式
.ant-table-row {
&:hover > td {
background-color: @background-color !important;
color: #fff !important;
}
&.row-selection {
background-color: @background-color !important;
color: #fff !important;
}
}
這里設(shè)置hover時行樣式和點擊時行樣式一樣,是為了不讓行點擊后,該行懸停時,出現(xiàn)其他不一樣的樣式。如果不想設(shè)置成一樣,可以單獨設(shè)置行點擊時的hover樣式和行點擊時的樣式一樣。
// 給表格添加懸停樣式和當前點擊行添加選中樣式
.ant-table-row {
&.row-selection {
background-color: @background-color !important;
color: #fff !important;
&:hover > td {
background-color: @background-color !important;
color: #fff !important;
}
}
}
這樣,我們的目的就達到了。
在行點擊時,修改currentRow,table組件內(nèi)部通過watch監(jiān)測到currentRow的變化,就會觸發(fā)改變樣式的方法。
<s-table
ref="table"
size="default"
rowKey="id"
:columns="columns"
:verticalScroll="false"
:data="loadData"
:stripe="true"
:highlightCurrentRow="true"
:currentRow="selectedCustomer"
:customRow="rowClick">
...
rowClick: record => ({
// 事件
on: {
click: () => {
this.handleCurrentRowChanged(record)
}
}
})
handleCustomerChanged (record) {
this.selectedCustomer = record
}
這樣就可以了。
stripe(斑馬紋行)
實現(xiàn)行的stripe功能,還是比較簡單的。
添加prop 變量 stripe, 在組件的update函數(shù)鉤子內(nèi),調(diào)用實現(xiàn)斑馬紋的方法就可以了
updated () {
if (this.stripe) {
this.renderStripe()
}
}
實現(xiàn)斑馬紋的方式有多種,這里只展示期中一種:
// 對表格行進行斑馬行格式顯示
renderStripe () {
const table = document.getElementsByClassName('ant-table-row')
if (table.length > 0) {
const rowList = [...table]
rowList.forEach(row => {
const index = rowList.indexOf(row)
if (index % 2 !== 0) {
row.style.backgroundColor = '#f7f7f7'
} else {
row.style.backgroundColor = '#ffffff'
}
})
}
},
獲取到table的所有行,然后對其進行隔行設(shè)置不一樣的行背景色,目的就達到了。
有其他更多方式的朋友歡迎補充。
以上這篇antd design table更改某行數(shù)據(jù)的樣式操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中點擊active并第一個默認選中功能的實現(xiàn)
這篇文章主要介紹了Vue中點擊active并第一個默認選中功能的實現(xiàn)代碼,代碼簡單易懂,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
vue使用element-ui的el-input監(jiān)聽不了回車事件的解決方法
小編在使用element-ui時,el-input組件監(jiān)聽不了回車事件,怎么回事呢?下面小編給大家?guī)砹藇ue使用element-ui的el-input監(jiān)聽不了回車事件的解決方法,一起看看吧2018-01-01
如何使用Webstorm和Chrome來調(diào)試Vue項目
這篇文章主要介紹了如何使用Webstorm和Chrome來調(diào)試Vue項目,對Vue感興趣的同學(xué),一定要看一下2021-05-05
詳解.vue文件中監(jiān)聽input輸入事件(oninput)
本篇文章主要介紹了詳解.vue文件中監(jiān)聽input輸入事件(oninput),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

