primevue-mirror/doc/contextmenu/TemplateDoc.vue

321 lines
12 KiB
Vue
Raw Normal View History

2023-11-07 07:55:48 +00:00
<template>
<DocSectionText v-bind="$attrs">
2023-11-07 18:11:29 +00:00
<p>ContextMenu offers item customization with the <i>item</i> template that receives the menuitem instance from the model as a parameter.</p>
2023-11-07 07:55:48 +00:00
</DocSectionText>
2024-05-20 12:14:38 +00:00
<div class="card flex md:justify-center">
<ul class="m-0 p-0 list-none border border-surface-200 dark:border-surface-700 rounded p-4 flex flex-col gap-2 w-full md:w-[30rem]">
2023-11-07 07:55:48 +00:00
<li
v-for="product in products"
:key="product.id"
2024-05-20 12:14:38 +00:00
:class="['p-2 hover:bg-surface-100 dark:hover:bg-surface-800 rounded border border-transparent transition-all transition-duration-200', { 'border-primary': selectedId === product.id }]"
2023-11-07 07:55:48 +00:00
@contextmenu="onRightClick($event, product.id)"
>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap p-2 items-center gap-4">
<img class="w-16 flex-shrink-0 rounded" :src="'https://primefaces.org/cdn/primevue/images/product/' + product.image" :alt="product.name" />
<div class="flex-1 flex flex-col gap-1">
2023-11-07 07:55:48 +00:00
<span class="font-bold">{{ product.name }}</span>
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-07 07:55:48 +00:00
<i class="pi pi-tag text-sm"></i>
<span>{{ product.category }}</span>
</div>
</div>
2024-05-20 12:14:38 +00:00
<span class="font-bold text-surface-900 ml-8">${{ product.price }}</span>
2023-11-07 07:55:48 +00:00
</div>
</li>
</ul>
<ContextMenu ref="menu" :model="items" @hide="selectedId = null">
<template #item="{ item, props }">
2024-05-20 12:14:38 +00:00
<a v-ripple class="flex items-center" v-bind="props.action">
2023-11-07 07:55:48 +00:00
<span :class="item.icon" />
<span class="ml-2">{{ item.label }}</span>
<Badge v-if="item.badge" class="ml-auto" :value="item.badge" />
2024-05-20 12:14:38 +00:00
<span v-if="item.shortcut" class="ml-auto border border-surface-200 dark:border-surface-700 rounded bg-surface-100 text-xs p-1">{{ item.shortcut }}</span>
2024-01-24 11:05:15 +00:00
<i v-if="item.items" class="pi pi-angle-right ml-auto"></i>
2023-11-07 07:55:48 +00:00
</a>
</template>
</ContextMenu>
</div>
<DocSectionCode :code="code" :service="['ProductService']" />
</template>
<script>
import { ProductService } from '@/service/ProductService';
export default {
data() {
return {
products: null,
selectedId: null,
items: [
{
label: 'Favorite',
icon: 'pi pi-star',
shortcut: '⌘+D'
},
{
label: 'Add',
icon: 'pi pi-shopping-cart',
shortcut: '⌘+A'
},
{
separator: true
},
{
label: 'Share',
icon: 'pi pi-share-alt',
items: [
{
label: 'Whatsapp',
icon: 'pi pi-whatsapp',
badge: 2
},
{
label: 'Instagram',
icon: 'pi pi-instagram',
badge: 3
}
]
}
],
code: {
basic: `
2024-05-20 12:14:38 +00:00
<ul class="m-0 p-0 list-none border border-surface-200 dark:border-surface-700 rounded p-4 flex flex-col gap-2 w-full md:w-[30rem]">
2023-11-07 07:55:48 +00:00
<li
v-for="product in products"
:key="product.id"
2024-05-20 12:14:38 +00:00
:class="['p-2 hover:bg-surface-100 dark:hover:bg-surface-800 rounded border border-transparent transition-all transition-duration-200', { 'border-primary': selectedId === product.id }]"
2023-11-07 07:55:48 +00:00
@contextmenu="onRightClick($event, product.id)"
>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap p-2 items-center gap-4">
<img class="w-16 flex-shrink-0 rounded" :src="'/images/product/' + product.image" :alt="product.name" />
<div class="flex-1 flex flex-col gap-1">
2023-11-07 07:55:48 +00:00
<span class="font-bold">{{ product.name }}</span>
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-07 07:55:48 +00:00
<i class="pi pi-tag text-sm"></i>
<span>{{ product.category }}</span>
</div>
</div>
2024-05-20 12:14:38 +00:00
<span class="font-bold text-surface-900 ml-8">\${{ product.price }}</span>
2023-11-07 07:55:48 +00:00
</div>
</li>
</ul>
<ContextMenu ref="menu" :model="items" @hide="selectedId = null">
<template #item="{ item, props }">
2024-05-20 12:14:38 +00:00
<a v-ripple class="flex items-center" v-bind="props.action">
2023-11-07 07:55:48 +00:00
<span :class="item.icon" />
<span class="ml-2">{{ item.label }}</span>
<Badge v-if="item.badge" class="ml-auto" :value="item.badge" />
2024-05-20 12:14:38 +00:00
<span v-if="item.shortcut" class="ml-auto border border-surface-200 dark:border-surface-700 rounded bg-surface-100 text-xs p-1">{{ item.shortcut }}</span>
2024-01-24 11:05:15 +00:00
<i v-if="item.items" class="pi pi-angle-right ml-auto"></i>
2023-11-07 07:55:48 +00:00
</a>
</template>
</ContextMenu>
`,
options: `
<template>
2024-05-20 12:14:38 +00:00
<div class="card flex md:justify-center">
<ul class="m-0 p-0 list-none border border-surface-200 dark:border-surface-700 rounded p-4 flex flex-col gap-2 w-full md:w-[30rem]">
2023-11-07 07:55:48 +00:00
<li
v-for="product in products"
:key="product.id"
2024-05-20 12:14:38 +00:00
:class="['p-2 hover:bg-surface-100 dark:hover:bg-surface-800 rounded border border-transparent transition-all transition-duration-200', { 'border-primary': selectedId === product.id }]"
2023-11-07 07:55:48 +00:00
@contextmenu="onRightClick($event, product.id)"
>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap p-2 items-center gap-4">
<img class="w-16 flex-shrink-0 rounded" :src="'https://primefaces.org/cdn/primevue/images/product/' + product.image" :alt="product.name" />
<div class="flex-1 flex flex-col gap-1">
2023-11-07 07:55:48 +00:00
<span class="font-bold">{{ product.name }}</span>
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-07 07:55:48 +00:00
<i class="pi pi-tag text-sm"></i>
<span>{{ product.category }}</span>
</div>
</div>
2024-05-20 12:14:38 +00:00
<span class="font-bold text-surface-900 ml-8">\${{ product.price }}</span>
2023-11-07 07:55:48 +00:00
</div>
</li>
</ul>
<ContextMenu ref="menu" :model="items" @hide="selectedId = null">
<template #item="{ item, props }">
2024-05-20 12:14:38 +00:00
<a v-ripple class="flex items-center" v-bind="props.action">
2023-11-07 07:55:48 +00:00
<span :class="item.icon" />
<span class="ml-2">{{ item.label }}</span>
<Badge v-if="item.badge" class="ml-auto" :value="item.badge" />
2024-05-20 12:14:38 +00:00
<span v-if="item.shortcut" class="ml-auto border border-surface-200 dark:border-surface-700 rounded bg-surface-100 text-xs p-1">{{ item.shortcut }}</span>
2024-01-24 11:05:15 +00:00
<i v-if="item.items" class="pi pi-angle-right ml-auto"></i>
2023-11-07 07:55:48 +00:00
</a>
</template>
</ContextMenu>
</div>
</template>
<script>
import { ProductService } from '@/service/ProductService';
export default {
data() {
return {
products: null,
selectedId: null,
items: [
{
label: 'Favorite',
icon: 'pi pi-star',
shortcut: '⌘+D'
},
{
label: 'Add',
icon: 'pi pi-shopping-cart',
shortcut: '⌘+A'
},
{
separator: true
},
{
label: 'Share',
icon: 'pi pi-share-alt',
items: [
{
label: 'Whatsapp',
icon: 'pi pi-whatsapp',
badge: 2
},
{
label: 'Instagram',
icon: 'pi pi-instagram',
badge: 3
}
]
}
]
};
},
mounted() {
ProductService.getProductsSmall().then((data) => (this.products = data));
},
methods: {
onRightClick(event, id) {
this.selectedId = id;
this.$refs.menu.show(event);
}
}
};
<\/script>
`,
composition: `
<template>
2024-05-20 12:14:38 +00:00
<div class="card flex md:justify-center">
<ul class="m-0 p-0 list-none border border-surface-200 dark:border-surface-700 rounded p-4 flex flex-col gap-2 w-full md:w-[30rem]" @hide="selectedId = null">
2023-11-07 07:55:48 +00:00
<li
v-for="product in products"
:key="product.id"
2024-05-20 12:14:38 +00:00
:class="['p-2 hover:bg-surface-100 dark:hover:bg-surface-800 rounded border border-transparent transition-all transition-duration-200', { 'border-primary': selectedId === product.id }]"
2023-11-07 07:55:48 +00:00
@contextmenu="onRightClick($event, product.id)"
>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap p-2 items-center gap-4">
<img class="w-16 flex-shrink-0 rounded" :src="'https://primefaces.org/cdn/primevue/images/product/' + product.image" :alt="product.name" />
<div class="flex-1 flex flex-col gap-1">
2023-11-07 07:55:48 +00:00
<span class="font-bold">{{ product.name }}</span>
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-07 07:55:48 +00:00
<i class="pi pi-tag text-sm"></i>
<span>{{ product.category }}</span>
</div>
</div>
2024-05-20 12:14:38 +00:00
<span class="font-bold text-surface-900 ml-8">\${{ product.price }}</span>
2023-11-07 07:55:48 +00:00
</div>
</li>
</ul>
<ContextMenu ref="menu" :model="items">
<template #item="{ item, props }">
2024-05-20 12:14:38 +00:00
<a v-ripple class="flex items-center" v-bind="props.action">
2023-11-07 07:55:48 +00:00
<span :class="item.icon" />
<span class="ml-2">{{ item.label }}</span>
<Badge v-if="item.badge" class="ml-auto" :value="item.badge" />
2024-05-20 12:14:38 +00:00
<span v-if="item.shortcut" class="ml-auto border border-surface-200 dark:border-surface-700 rounded bg-surface-100 text-xs p-1">{{ item.shortcut }}</span>
2024-01-24 11:05:15 +00:00
<i v-if="item.items" class="pi pi-angle-right ml-auto"></i>
2023-11-07 07:55:48 +00:00
</a>
</template>
</ContextMenu>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { ProductService } from '@/service/ProductService'
const menu = ref();
const items = ref([
{
label: 'Favorite',
icon: 'pi pi-star',
shortcut: '⌘+D'
},
{
label: 'Add',
icon: 'pi pi-shopping-cart',
shortcut: '⌘+A'
},
{
separator: true
},
{
label: 'Share',
icon: 'pi pi-share-alt',
items: [
{
label: 'Whatsapp',
icon: 'pi pi-whatsapp',
badge: 2
},
{
label: 'Instagram',
icon: 'pi pi-instagram',
badge: 3
}
]
}
]);
const products = ref(null);
const selectedId = ref(null);
onMounted(() => {
ProductService.getProductsSmall().then((data) => (products.value = data));
});
const onRightClick = (event, id) => {
selectedId.value = id;
menu.value.show(event);
};
<\/script>
`
},
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
},
...`
};
},
mounted() {
ProductService.getProductsMini().then((data) => (this.products = data));
},
methods: {
onRightClick(event, id) {
this.selectedId = id;
this.$refs.menu.show(event);
}
}
};
</script>