Ant Design table 排序 ,點擊頭部只有升序和降序


state = {
sortedInfo: {
columnKey:'proportion',
order:'descend'
},
saveSortedInfo:null
}

每列
const columns = [
{
title: '名稱',
dataIndex: 'name',
key: 'name',
className:'t_center',
width:'120px'
},
{
title: '占比',
dataIndex: 'proportion',
key: 'proportion',
className:'t_center',
sorter: (a, b) => a.proportion - b.proportion,
defaultSortOrder:'descend',
sortDirections: [ 'descend','ascend'],
sortOrder: sortedInfo.columnKey === 'proportion' && sortedInfo.order,

},
  {....},{...}
]

//變化事件
function has_value(data){
return Object.keys(data).length>0
}
handleChange = (pagination, filters, sorter) => {
if(has_value(sorter)){ //判斷sorter 是否為null ,我用的是Object.keys(obj).length
this.setState({
sortedInfo: sorter,
saveSortedInfo: sorter //保存此次點擊的對象
});
}else{
this.setState({
sortedInfo:{
order:this.state.saveSortedInfo.order=='ascend'?'descend':'ascend',
columnKey:this.state.saveSortedInfo.columnKey
}
})
}



}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM