primevue-mirror/doc/datatable/ColumnToggleDoc.vue

162 lines
5.2 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>Column visibility based on a condition can be implemented with dynamic columns, in this sample a MultiSelect is used to manage the visible columns.</p>
</DocSectionText>
2023-12-31 14:08:33 +00:00
<DeferredDemo @load="loadDemoData">
<div class="card">
<DataTable :value="products" tableStyle="min-width: 50rem">
<template #header>
<div style="text-align: left">
<MultiSelect :modelValue="selectedColumns" :options="columns" optionLabel="header" @update:modelValue="onToggle" display="chip" placeholder="Select Columns" />
</div>
</template>
<Column field="code" header="Code" />
<Column v-for="(col, index) of selectedColumns" :key="col.field + '_' + index" :field="col.field" :header="col.header"></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 {
selectedColumns: null,
columns: null,
products: null,
code: {
2023-09-22 12:54:14 +00:00
basic: `
<DataTable :value="products" tableStyle="min-width: 50rem">
2023-02-28 08:29:30 +00:00
<template #header>
<div style="text-align:left">
2023-12-31 14:08:33 +00:00
<MultiSelect :modelValue="selectedColumns" :options="columns" optionLabel="header" @update:modelValue="onToggle"
2023-02-28 08:29:30 +00:00
display="chip" placeholder="Select Columns" />
</div>
</template>
<Column field="code" header="Code" />
<Column v-for="(col, index) of selectedColumns" :field="col.field" :header="col.header" :key="col.field + '_' + index"></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>
<DataTable :value="products" tableStyle="min-width: 50rem">
<template #header>
<div style="text-align:left">
2023-12-31 14:08:33 +00:00
<MultiSelect :modelValue="selectedColumns" :options="columns" optionLabel="header" @update:modelValue="onToggle"
2023-02-28 08:29:30 +00:00
display="chip" placeholder="Select Columns" />
</div>
</template>
<Column field="code" header="Code" />
<Column v-for="(col, index) of selectedColumns" :field="col.field" :header="col.header" :key="col.field + '_' + index"></Column>
</DataTable>
</div>
</template>
<script>
import { ProductService } from '@/service/ProductService';
export default {
data() {
return {
selectedColumns: null,
columns: null,
products: null
}
},
created() {
this.columns = [
{field: 'name', header: 'Name'},
{field: 'category', header: 'Category'},
{field: 'quantity', header: 'Quantity'}
];
this.selectedColumns = this.columns;
},
mounted() {
ProductService.getProductsMini().then((data) => (this.products = data));
},
methods: {
onToggle(value) {
this.selectedColumns = this.columns.filter(col => value.includes(col));
}
}
}
<\/script>
2023-10-15 09:38:39 +00:00
2023-02-28 08:29:30 +00:00
`,
2023-09-22 12:54:14 +00:00
composition: `
<template>
2023-02-28 08:29:30 +00:00
<div>
<DataTable :value="products" tableStyle="min-width: 50rem">
<template #header>
<div style="text-align:left">
<MultiSelect :modelValue="selectedColumns" :options="columns" optionLabel="header" @update:modelValue="onToggle"
display="chip" placeholder="Select Columns" />
</div>
</template>
<Column field="code" header="Code" />
<Column v-for="(col, index) of selectedColumns" :field="col.field" :header="col.header" :key="col.field + '_' + index"></Column>
</DataTable>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { ProductService } from '@/service/ProductService';
onMounted(() => {
ProductService.getProductsMini().then((data) => (products.value = data));
});
const columns = ref([
{field: 'name', header: 'Name'},
{field: 'category', header: 'Category'},
{field: 'quantity', header: 'Quantity'}
]);
const selectedColumns = ref(columns.value);
const products = ref();
const onToggle = (val) => {
selectedColumns.value = columns.value.filter(col => val.includes(col));
};
<\/script>
2023-03-10 08:19:45 +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
},
...`
2023-02-28 08:29:30 +00:00
}
};
},
created() {
this.columns = [
{ field: 'name', header: 'Name' },
{ field: 'category', header: 'Category' },
{ field: 'quantity', header: 'Quantity' }
];
this.selectedColumns = this.columns;
},
methods: {
2023-12-31 14:08:33 +00:00
loadDemoData() {
ProductService.getProductsMini().then((data) => (this.products = data));
},
2023-02-28 08:29:30 +00:00
onToggle(value) {
this.selectedColumns = this.columns.filter((col) => value.includes(col));
}
}
};
</script>