primevue-mirror/apps/showcase/doc/datatable/ContextMenuDoc.vue

200 lines
7.3 KiB
Vue
Raw Permalink Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>
DataTable has exclusive integration with ContextMenu using the <i>contextMenu</i> event to open a menu on right click alont with <i>contextMenuSelection</i> property and <i>row-contextmenu</i> event to control the selection via the menu.
</p>
</DocSectionText>
2023-12-31 14:08:33 +00:00
<DeferredDemo @load="loadDemoData">
<div class="card">
<ContextMenu ref="cm" :model="menuModel" @hide="selectedProduct = null" />
<DataTable v-model:contextMenuSelection="selectedProduct" :value="products" contextMenu @row-contextmenu="onRowContextMenu" tableStyle="min-width: 50rem">
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="price" header="Price">
<template #body="slotProps">
{{ formatCurrency(slotProps.data.price) }}
</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,
selectedProduct: null,
menuModel: [
{ label: 'View', icon: 'pi pi-fw pi-search', command: () => this.viewProduct(this.selectedProduct) },
{ label: 'Delete', icon: 'pi pi-fw pi-times', command: () => this.deleteProduct(this.selectedProduct) }
],
code: {
2023-09-22 12:54:14 +00:00
basic: `
2023-11-09 15:21:13 +00:00
<ContextMenu ref="cm" :model="menuModel" @hide="selectedProduct = null" />
2023-02-28 08:29:30 +00:00
<DataTable v-model:contextMenuSelection="selectedProduct" :value="products" contextMenu
@row-contextmenu="onRowContextMenu" tableStyle="min-width: 50rem">
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="price" header="Price">
<template #body="slotProps">
{{ formatCurrency(slotProps.data.price) }}
</template>
</Column>
2023-10-15 09:38:39 +00:00
</DataTable>
`,
2023-09-22 12:54:14 +00:00
options: `
<template>
2023-11-09 15:21:13 +00:00
<div class="card">
<ContextMenu ref="cm" :model="menuModel" @hide="selectedProduct = null" />
2023-12-31 14:08:33 +00:00
<DataTable :value="products" contextMenu v-model:contextMenuSelection="selectedProduct"
2023-02-28 08:29:30 +00:00
@rowContextmenu="onRowContextMenu" tableStyle="min-width: 50rem">
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="price" header="Price">
<template #body="slotProps">
{{formatCurrency(slotProps.data.price)}}
</template>
</Column>
</DataTable>
<Toast />
</div>
</template>
<script>
import { ProductService } from '@/service/ProductService';
export default {
data() {
return {
products: null,
selectedProduct: null,
menuModel: [
{label: 'View', icon: 'pi pi-fw pi-search', command: () => this.viewProduct(this.selectedProduct)},
{label: 'Delete', icon: 'pi pi-fw pi-times', command: () => this.deleteProduct(this.selectedProduct)}
]
}
},
mounted() {
ProductService.getProductsMini().then((data) => (this.products = data));
},
methods: {
onRowContextMenu(event) {
this.$refs.cm.show(event.originalEvent);
},
viewProduct(product) {
this.$toast.add({severity: 'info', summary: 'Product Selected', detail: product.name, life: 3000});
},
deleteProduct(product) {
this.products = this.products.filter((p) => p.id !== product.id);
this.$toast.add({severity: 'error', summary: 'Product Deleted', detail: product.name, life: 3000});
this.selectedProduct = null;
},
formatCurrency(value) {
return value.toLocaleString('en-US', {style: 'currency', currency: 'USD'});
}
}
}
<\/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-11-09 15:21:13 +00:00
<div class="card">
<ContextMenu ref="cm" :model="menuModel" @hide="selectedProduct = null" />
2023-02-28 08:29:30 +00:00
<DataTable :value="products" contextMenu v-model:contextMenuSelection="selectedProduct"
@rowContextmenu="onRowContextMenu" tableStyle="min-width: 50rem">
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
<Column field="category" header="Category"></Column>
<Column field="price" header="Price">
<template #body="slotProps">
{{formatCurrency(slotProps.data.price)}}
</template>
</Column>
</DataTable>
<Toast />
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { useToast } from 'primevue/usetoast';
import { ProductService } from '@/service/ProductService';
onMounted(() => {
ProductService.getProductsMini().then((data) => (products.value = data));
});
const cm = ref();
const toast = useToast();
const products = ref();
const selectedProduct = ref();
const menuModel = ref([
{label: 'View', icon: 'pi pi-fw pi-search', command: () => viewProduct(selectedProduct)},
{label: 'Delete', icon: 'pi pi-fw pi-times', command: () => deleteProduct(selectedProduct)}
]);
const onRowContextMenu = (event) => {
cm.value.show(event.originalEvent);
};
const viewProduct = (product) => {
toast.add({severity: 'info', summary: 'Product Selected', detail: product.value.name, life: 3000});
};
const deleteProduct = (product) => {
products.value = products.value.filter((p) => p.id !== product.value.id);
toast.add({severity: 'error', summary: 'Product Deleted', detail: product.value.name, life: 3000});
selectedProduct.value = null;
};
const formatCurrency = (value) => {
return value.toLocaleString('en-US', {style: 'currency', currency: 'USD'});
};
<\/script>
2023-03-10 08:19:45 +00:00
`,
data: `
/* ProductService */
{
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
}
};
},
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
onRowContextMenu(event) {
this.$refs.cm.show(event.originalEvent);
},
viewProduct(product) {
this.$toast.add({ severity: 'info', summary: 'Product Selected', detail: product.name, life: 3000 });
},
deleteProduct(product) {
this.products = this.products.filter((p) => p.id !== product.id);
this.$toast.add({ severity: 'error', summary: 'Product Deleted', detail: product.name, life: 3000 });
this.selectedProduct = null;
},
formatCurrency(value) {
return value.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
}
}
};
</script>