ui.table how to set the cell color #1168
-
QuestionHi, there. I want set the ui.table cell color with some condiitons. Can you give me some help? I am looking forward to it. <template>
<div>
<q-table
:rows="data"
:columns="columns"
>
<template v-slot:body-cell(index)="props">
<q-td :props="props" :class="getCellClass(props)">
{{ props.value }}
</q-td>
</template>
</q-table>
</div>
</template>
<script>
export default {
data() {
return {
data: [
{ index: 1, value: 10 },
{ index: 2, value: 5 },
{ index: 3, value: 8 },
],
columns: [
{ name: 'Index', field: 'index' },
{ name: 'Value', field: 'value' },
],
};
},
methods: {
getCellClass(props) {
// 根据单元格的值设置不同的颜色样式
if (props.value > 8) {
return 'bg-green';
} else if (props.value < 5) {
return 'bg-red';
} else {
return 'bg-yellow';
}
},
},
};
</script>
<style scoped>
.bg-green {
background-color: green;
}
.bg-red {
background-color: red;
}
.bg-yellow {
background-color: yellow;
}
</style> |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Jul 14, 2023
Replies: 1 comment 5 replies
-
Hi @akai-1024! Too keep it simple, you can add the JavaScript function directly to the columns = [
{'name': 'name', 'label': 'Name', 'field': 'name'},
{'name': 'age', 'label': 'Age', 'field': 'age'},
]
rows = [
{'id': 0, 'name': 'Alice', 'age': 18},
{'id': 1, 'name': 'Bob', 'age': 21},
{'id': 2, 'name': 'Carol', 'age': 17},
]
table = ui.table(columns=columns, rows=rows, row_key='name')
table.add_slot('body-cell-age', r'''
<q-td :props="props" :class="props.value < 18 ? 'bg-red' : 'bg-green'">
{{ props.value }}
</q-td>
''') |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
akai-1024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @akai-1024!
Too keep it simple, you can add the JavaScript function directly to the
:class
attribute: