Files
vxe-table/examples/views/table/base/DynamicStyle.vue
xuliangzhan f0a6bc26dc 更新文档
2020-09-05 23:58:04 +08:00

146 lines
4.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div>
<p class="tip">
行的动态样色单元格动态样式表头的动态样式表尾的动态样式全部都可以完全自定义通过设置 <table-api-link prop="cell-style"/><table-api-link prop="header-cell-style"/><table-api-link prop="row-style"/> ...等参数<br>
<span class="red">当自定义样式之后可能会覆盖表格的样式比如选中行..记得自行处理好相关样式</span>
</p>
<vxe-table
border
:header-cell-style="headerCellStyle"
:row-style="rowStyle"
:cell-style="cellStyle"
:data="tableData">
<vxe-table-column type="seq" width="60"></vxe-table-column>
<vxe-table-column field="name" title="Name"></vxe-table-column>
<vxe-table-column field="sex" title="Sex"></vxe-table-column>
<vxe-table-column field="age" title="Age"></vxe-table-column>
<vxe-table-column field="attr1" title="Attr1"></vxe-table-column>
<vxe-table-column field="address" title="Address" show-overflow></vxe-table-column>
</vxe-table>
<p class="demo-code">{{ $t('app.body.button.showCode') }}</p>
<pre>
<code class="xml">{{ demoCodes[0] }}</code>
<code class="javascript">{{ demoCodes[1] }}</code>
<code class="scss">{{ demoCodes[2] }}</code>
</pre>
</div>
</template>
<script>
import hljs from 'highlight.js'
export default {
data () {
return {
tableData: [
{ id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'Shenzhen' },
{ id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
{ id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
{ id: 10004, name: 'Test4', role: 'Designer', sex: 'Women ', age: 24, address: 'Shanghai' }
],
demoCodes: [
`
<vxe-table
border
:header-cell-style="headerCellStyle"
:row-style="rowStyle"
:cell-style="cellStyle"
:data="tableData">
<vxe-table-column type="seq" width="60"></vxe-table-column>
<vxe-table-column field="name" title="Name"></vxe-table-column>
<vxe-table-column field="sex" title="Sex"></vxe-table-column>
<vxe-table-column field="age" title="Age"></vxe-table-column>
<vxe-table-column field="attr1" title="Attr1"></vxe-table-column>
<vxe-table-column field="address" title="Address" show-overflow></vxe-table-column>
</vxe-table>
`,
`
export default {
data () {
return {
tableData: [
{ id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'Shenzhen' },
{ id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
{ id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
{ id: 10004, name: 'Test4', role: 'Designer', sex: 'Women ', age: 24, address: 'Shanghai' }
]
}
},
methods: {
headerCellStyle ({ column, columnIndex }) {
if (column.property === 'name') {
return {
backgroundColor: '#f60',
color: '#ffffff'
}
}
},
rowStyle ({ row, rowIndex }) {
if ([2, 3, 5].includes(rowIndex)) {
return {
backgroundColor: 'red',
color: '#ffffff'
}
}
},
cellStyle ({ row, rowIndex, column, columnIndex }) {
if (column.property === 'sex') {
if (row.sex >= '1') {
return {
backgroundColor: '#187'
}
} else if (row.age === 26) {
return {
backgroundColor: '#2db7f5'
}
}
}
}
}
}
`
]
}
},
mounted () {
Array.from(this.$el.querySelectorAll('pre code')).forEach((block) => {
hljs.highlightBlock(block)
})
},
methods: {
headerCellStyle ({ column }) {
if (column.property === 'name') {
return {
backgroundColor: '#f60',
color: '#ffffff'
}
}
},
rowStyle ({ rowIndex }) {
if ([2, 3, 5].includes(rowIndex)) {
return {
backgroundColor: 'red',
color: '#ffffff'
}
}
},
cellStyle ({ row, column }) {
if (column.property === 'sex') {
if (row.sex >= '1') {
return {
backgroundColor: '#187'
}
} else if (row.age === 26) {
return {
backgroundColor: '#2db7f5'
}
}
}
}
}
}
</script>