primevue-mirror/doc/datatable/ConditionalStyleDoc.vue

190 lines
6.1 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>Particular rows and cells can be styled based on conditions. The <i>rowClass</i> receives a row data as a parameter to return a style class for a row whereas cells are customized using the <i>body</i> template.</p>
</DocSectionText>
2023-12-31 14:08:33 +00:00
<DeferredDemo @load="loadDemoData">
<div class="card">
<DataTable :value="products" :rowClass="rowClass" :rowStyle="rowStyle" tableStyle="min-width: 50rem">
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="quantity" header="Quantity">
<template #body="slotProps">
<div :class="stockClass(slotProps.data)">
{{ slotProps.data.quantity }}
</div>
</template>
</Column>
</DataTable>
</div>
</DeferredDemo>
2023-02-28 08:29:30 +00:00
<DocSectionCode :code="code" :service="['ProductService']" />
</template>
<script>
import { ProductService } from '@/service/ProductService';
export default {
data() {
return {
products: null,
code: {
2023-09-22 12:54:14 +00:00
basic: `
<DataTable :value="products" :rowClass="rowClass" :rowStyle="rowStyle" tableStyle="min-width: 50rem">
2023-02-28 08:29:30 +00:00
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="quantity" header="Quantity">
<template #body="slotProps">
<div :class="stockClass(slotProps.data)">
{{ slotProps.data.quantity }}
</div>
</template>
</Column>
2023-10-15 09:38:39 +00:00
</DataTable>
`,
2023-09-22 12:54:14 +00:00
options: `
<template>
2023-02-28 08:29:30 +00:00
<div class="card">
<DataTable :value="products" :rowClass="rowClass" :rowStyle="rowStyle" tableStyle="min-width: 50rem">
2023-02-28 08:29:30 +00:00
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="quantity" header="Quantity">
<template #body="slotProps">
<div :class="stockClass(slotProps.data)">
{{ slotProps.data.quantity }}
</div>
</template>
</Column>
</DataTable>
</div>
</template>
<script>
import { ProductService } from '@/service/ProductService';
export default {
data() {
return {
products: null
};
},
mounted() {
ProductService.getProductsSmall().then((data) => (this.products = data));
},
methods: {
rowClass(data) {
2024-05-20 12:14:38 +00:00
return [{ 'bg-primary text-primary-contrast': data.category === 'Fitness' }];
2023-02-28 08:29:30 +00:00
},
rowStyle(data) {
if (data.quantity === 0) {
return { fontWeight: 'bold', fontStyle: 'italic' };
}
},
2023-02-28 08:29:30 +00:00
stockClass(data) {
return [
2024-05-20 12:14:38 +00:00
'rounded-full w-8 h-8 inline-flex font-bold justify-center items-center text-sm',
2023-02-28 08:29:30 +00:00
{
'bg-red-100 text-red-900': data.quantity === 0,
'bg-blue-100 text-blue-900': data.quantity > 0 && data.quantity < 10,
'bg-teal-100 text-teal-900': data.quantity > 10
}
];
}
}
};
2023-10-15 09:38:39 +00:00
<\/script>
`,
2023-09-22 12:54:14 +00:00
composition: `
<template>
2023-02-28 08:29:30 +00:00
<div class="card">
<DataTable :value="products" :rowClass="rowClass" :rowStyle="rowStyle" tableStyle="min-width: 50rem">
2023-02-28 08:29:30 +00:00
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="quantity" header="Quantity">
<template #body="slotProps">
<div :class="stockClass(slotProps.data)">
{{ slotProps.data.quantity }}
</div>
</template>
</Column>
</DataTable>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { ProductService } from '@/service/ProductService';
onMounted(() => {
ProductService.getProductsSmall().then((data) => (this.products = data));
});
2023-12-31 14:08:33 +00:00
2023-02-28 08:29:30 +00:00
const products = ref();
const rowClass = (data) => {
2024-05-20 12:14:38 +00:00
return [{ 'bg-primary text-primary-contrast': data.category === 'Fitness' }];
2023-02-28 08:29:30 +00:00
};
const rowStyle = (data) => {
if (data.quantity === 0) {
return { fontWeight: 'bold', fontStyle: 'italic' };
}
};
2023-02-28 08:29:30 +00:00
const stockClass = (data) => {
return [
2024-05-20 12:14:38 +00:00
'rounded-full w-8 h-8 inline-flex font-bold justify-center items-center text-sm',
2023-02-28 08:29:30 +00:00
{
'bg-red-100 text-red-900': data.quantity === 0,
'bg-blue-100 text-blue-900': data.quantity > 0 && data.quantity < 10,
'bg-teal-100 text-teal-900': data.quantity > 10
}
];
};
2023-10-15 09:38:39 +00:00
<\/script>
`,
2023-02-28 08:29:30 +00:00
data: `
{
id: '1000',
code: 'f230fh0g3',
name: 'Bamboo Watch',
description: 'Product Description',
image: 'bamboo-watch.jpg',
price: 65,
category: 'Accessories',
quantity: 24,
inventoryStatus: 'INSTOCK',
rating: 5
},
...`
}
};
},
methods: {
2023-12-31 14:08:33 +00:00
loadDemoData() {
ProductService.getProductsSmall().then((data) => (this.products = data));
},
2023-02-28 08:29:30 +00:00
rowClass(data) {
2024-05-20 12:14:38 +00:00
return [{ 'bg-primary text-primary-contrast': data.category === 'Fitness' }];
2023-02-28 08:29:30 +00:00
},
rowStyle(data) {
if (data.quantity === 0) {
return { fontWeight: 'bold', fontStyle: 'italic' };
}
},
2023-02-28 08:29:30 +00:00
stockClass(data) {
return [
2024-05-20 12:14:38 +00:00
'rounded-full w-8 h-8 inline-flex font-bold justify-center items-center text-sm',
2023-02-28 08:29:30 +00:00
{
'bg-red-100 text-red-900': data.quantity === 0,
'bg-blue-100 text-blue-900': data.quantity > 0 && data.quantity < 10,
'bg-teal-100 text-teal-900': data.quantity > 10
}
];
}
}
};
</script>