table用來展現表格數據很方便,當咱們想要將某一行或者某一列或者是某一格的背景色改變,下面是官方的方法css
行:經過屬性 row-class-name
能夠給某一行指定一個樣式名稱。數組
列:經過給列 columns 設置字段 className
能夠給某一列指定一個樣式。code
單元格:經過給數據 data 設置字段 cellClassName
能夠給任意一個單元格指定樣式對象
行和列的設置比較簡單,能夠直接設置,官方也給出了樣例ip
<style> .ivu-table .demo-table-info-row td{ background-color: #2db7f5; color: #fff; } .ivu-table .demo-table-error-row td{ background-color: #ff6600; color: #fff; } .ivu-table td.demo-table-info-column{ background-color: #2db7f5; color: #fff; } .ivu-table .demo-table-info-cell-name { background-color: #2db7f5; color: #fff; } .ivu-table .demo-table-info-cell-age { background-color: #ff6600; color: #fff; } .ivu-table .demo-table-info-cell-address { background-color: #187; color: #fff; } </style> <template> <Table :row-class-name="rowClassName" :columns="columns1" :data="data1"></Table> </template> <script> export default { data () { return { columns1: [ { title: 'Name', key: 'name' }, { title: 'Age', key: 'age', className: 'demo-table-info-column' }, { title: 'Address', key: 'address' } ], data1: [ { name: 'John Brown', age: 18, address: 'New York No. 1 Lake Park' }, { name: 'Jim Green', age: 25, address: 'London No. 1 Lake Park', cellClassName: { age: 'demo-table-info-cell-age', address: 'demo-table-info-cell-address' } }, { name: 'Joe Black', age: 30, address: 'Sydney No. 1 Lake Park' }, { name: 'Jon Snow', age: 26, address: 'Ottawa No. 2 Lake Park', cellClassName: { name: 'demo-table-info-cell-name' } } ] } }, methods: { rowClassName (row, index) { if (index === 1) { return 'demo-table-info-row'; } else if (index === 3) { return 'demo-table-error-row'; } return ''; } } } </script>
當咱們的數據是動態的,行和列仍然可以直接去定義樣式,但單元格的樣式須要咱們根據需求去動態添加it
發起請求,得到數據,假設爲res.data.list,設置tableData做爲接受數據的數組對象,在這裏我是經過判斷單元格列名sbType的值,添加不一樣的樣式,若是效果沒出現,給對應樣式加 !important設置優先級table
.first{ background-color:green; color:#fff; } .second{ background-color:red; color:#fff; } for(var i=0;i<res.data.list.length;i++){ if(res.data.list[i].sbType=='0'){ that.list.tableData[i].cellClassName={ sbType: 'first'} }else{ that.list.tableData[i].cellClassName={ sbType: 'second'} } }