溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

antd中實現(xiàn)table展開行默認展示的方法

發(fā)布時間:2020-11-04 15:30:32 來源:億速云 閱讀:6043 作者:Leah 欄目:開發(fā)技術

這篇文章運用簡單易懂的例子給大家介紹antd中實現(xiàn)table展開行默認展示的方法,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

在table中配置

 <Table
 expandedRowKeys={this.store.chargeTableData.map(item => item.key)} //展開的行
 expandIconAsCell={false}
 expandIconColumnIndex={-1}
 bordered     //展示邊框
 defaultExpandAllRows={true}  //初始時展開所有行
 pagination={{ pageSize: 5 }}  //分頁器
 expandedRowRender={this.expandedRowRender} //額外展開的行
 columns={columns}    //數(shù)據(jù)
 dataSource={this.store.chargeTableData} //數(shù)據(jù)數(shù)組
 />

補充知識:antd Table 利用自己生成cell結(jié)合expandedRowKeys配置,實現(xiàn)任意cell控制展開列

因為項目需要,antd實現(xiàn)一個形如這樣的表格

antd中實現(xiàn)table展開行默認展示的方法

但是奈何翻了好幾遍api文檔并沒有發(fā)現(xiàn)這樣的東西,只好自己改造了,

首先table是這樣的

 <Table 
  columns={this.columns}
  dataSource={tableData} 
  bordered 
  pagination={false}
  size='small'
  expandIconAsCell={false} 
  expandIconColumnIndex={-1}
  expandedRowRender={record=>this.expandedRowTable(record)}
  expandedRowKeys={this.state.expandArray}
  />

實現(xiàn)了隱藏自帶按鈕、并確定了控制展開行的數(shù)組,

接下來就是控制數(shù)組了,

先綁定下方法

onClick={()=>this.expandTable(row)}

然后 是點擊cell的方法

expandTable = row =>{
 const filtered = this.state.expandArray
 const text = this.state.expandBtnText 
 if(this.state.expandArray.includes(row.key)){
 filtered.splice(filtered.findIndex(element => element === row.key),1 );
 this.expandTdNum(parseInt(row.key,10),'reduce') 
 text[parseInt(row.key,10)-1] = '詳情'
 }else{
 filtered.push(row.key)
 this.expandTdNum(parseInt(row.key,10),'add') 
 text[parseInt(row.key,10)-1] = '關閉' 
 }
 this.setState({
 expandArray:filtered,
 })

最后控制數(shù)組的方法

expandTdNum = (key,operation) =>{
 let temp = 0
 if(operation==='add'){
  temp++
 }else if(operation==='reduce'){
 temp--
 }else{
 return false
 }
 if(key>0 && key<7){
 this.setState({
  firstTdNum:this.state.firstTdNum + temp,
 })
 }else if(key>6 && key<10){
 this.setState({
  middleTdNum:this.state.middleTdNum + temp,
 })
 }else if(key>9 && key<13){
 this.setState({
  lastTdNum:this.state.lastTdNum + temp,
 })
 }
 }

關于antd中實現(xiàn)table展開行默認展示的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI