primevue-mirror/doc/datatable/samples/ProductsDoc.vue

959 lines
42 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>CRUD implementation example with a Dialog.</p>
</DocSectionText>
2023-12-31 14:08:33 +00:00
<DeferredDemo @load="loadDemoData">
<div class="card">
2024-05-20 12:14:38 +00:00
<Toolbar class="mb-6">
2023-12-31 14:08:33 +00:00
<template #start>
<Button label="New" icon="pi pi-plus" severity="success" class="mr-2" @click="openNew" />
<Button label="Delete" icon="pi pi-trash" severity="danger" @click="confirmDeleteSelected" :disabled="!selectedProducts || !selectedProducts.length" />
2023-02-28 08:29:30 +00:00
</template>
2023-12-31 14:08:33 +00:00
<template #end>
2024-05-24 20:15:49 +00:00
<FileUpload mode="basic" accept="image/*" :maxFileSize="1000000" label="Import" chooseLabel="Import" class="mr-2" auto />
2023-12-31 14:08:33 +00:00
<Button label="Export" icon="pi pi-upload" severity="help" @click="exportCSV($event)" />
2023-02-28 08:29:30 +00:00
</template>
2023-12-31 14:08:33 +00:00
</Toolbar>
<DataTable
ref="dt"
v-model:selection="selectedProducts"
:value="products"
dataKey="id"
:paginator="true"
:rows="10"
:filters="filters"
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
:rowsPerPageOptions="[5, 10, 25]"
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} products"
>
<template #header>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap gap-2 items-center justify-between">
2023-12-31 14:08:33 +00:00
<h4 class="m-0">Manage Products</h4>
2024-04-08 10:43:16 +00:00
<IconField>
2024-01-30 08:04:48 +00:00
<InputIcon>
<i class="pi pi-search" />
</InputIcon>
2023-12-31 14:08:33 +00:00
<InputText v-model="filters['global'].value" placeholder="Search..." />
2024-01-30 08:04:48 +00:00
</IconField>
2023-12-31 14:08:33 +00:00
</div>
2023-02-28 08:29:30 +00:00
</template>
2023-12-31 14:08:33 +00:00
<Column selectionMode="multiple" style="width: 3rem" :exportable="false"></Column>
<Column field="code" header="Code" sortable style="min-width: 12rem"></Column>
<Column field="name" header="Name" sortable style="min-width: 16rem"></Column>
<Column header="Image">
<template #body="slotProps">
2024-05-20 12:14:38 +00:00
<img :src="`https://primefaces.org/cdn/primevue/images/product/${slotProps.data.image}`" :alt="slotProps.data.image" class="rounded" style="width: 64px" />
2023-12-31 14:08:33 +00:00
</template>
</Column>
<Column field="price" header="Price" sortable style="min-width: 8rem">
<template #body="slotProps">
{{ formatCurrency(slotProps.data.price) }}
</template>
</Column>
<Column field="category" header="Category" sortable style="min-width: 10rem"></Column>
<Column field="rating" header="Reviews" sortable style="min-width: 12rem">
<template #body="slotProps">
<Rating :modelValue="slotProps.data.rating" :readonly="true" :cancel="false" />
</template>
</Column>
<Column field="inventoryStatus" header="Status" sortable style="min-width: 12rem">
<template #body="slotProps">
<Tag :value="slotProps.data.inventoryStatus" :severity="getStatusLabel(slotProps.data.inventoryStatus)" />
</template>
</Column>
<Column :exportable="false" style="min-width: 12rem">
<template #body="slotProps">
<Button icon="pi pi-pencil" outlined rounded class="mr-2" @click="editProduct(slotProps.data)" />
<Button icon="pi pi-trash" outlined rounded severity="danger" @click="confirmDeleteProduct(slotProps.data)" />
</template>
</Column>
</DataTable>
</div>
</DeferredDemo>
2023-02-28 08:29:30 +00:00
2024-05-24 20:15:49 +00:00
<Dialog v-model:visible="productDialog" :style="{ width: '450px' }" header="Product Details" :modal="true">
<div class="flex flex-col gap-6 p-fluid">
<img v-if="product.image" :src="`https://primefaces.org/cdn/primevue/images/product/${product.image}`" :alt="product.image" class="block m-auto pb-4" />
<div>
<label for="name" class="block font-bold mb-3">Name</label>
<InputText id="name" v-model.trim="product.name" required="true" autofocus :invalid="submitted && !product.name" />
<small v-if="submitted && !product.name" class="text-red-500">Name is required.</small>
</div>
<div>
<label for="description" class="block font-bold mb-3">Description</label>
<Textarea id="description" v-model="product.description" required="true" rows="3" cols="20" />
</div>
<div>
<label for="inventoryStatus" class="block font-bold mb-3">Inventory Status</label>
<Select id="inventoryStatus" v-model="product.inventoryStatus" :options="statuses" optionLabel="label" placeholder="Select a Status"></Select>
</div>
2023-02-28 08:29:30 +00:00
2024-05-24 20:15:49 +00:00
<div>
<span class="block font-bold mb-4">Category</span>
<div class="grid grid-cols-12 gap-4">
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category1" v-model="product.category" name="category" value="Accessories" />
<label for="category1">Accessories</label>
2023-02-28 08:29:30 +00:00
</div>
2024-05-24 20:15:49 +00:00
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category2" v-model="product.category" name="category" value="Clothing" />
<label for="category2">Clothing</label>
</div>
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category3" v-model="product.category" name="category" value="Electronics" />
<label for="category3">Electronics</label>
</div>
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category4" v-model="product.category" name="category" value="Fitness" />
<label for="category4">Fitness</label>
2023-02-28 08:29:30 +00:00
</div>
</div>
2024-05-24 20:15:49 +00:00
</div>
<div class="grid grid-cols-12 gap-4">
<div class="col-span-6">
<label for="price" class="block font-bold mb-3">Price</label>
<InputNumber id="price" v-model="product.price" mode="currency" currency="USD" locale="en-US" />
2023-02-28 08:29:30 +00:00
</div>
2024-05-24 20:15:49 +00:00
<div class="col-span-6">
<label for="quantity" class="block font-bold mb-3">Quantity</label>
<InputNumber id="quantity" v-model="product.quantity" integeronly />
2023-02-28 08:29:30 +00:00
</div>
</div>
</div>
<template #footer>
2023-03-03 12:15:20 +00:00
<Button label="Cancel" icon="pi pi-times" text @click="hideDialog" />
2024-05-24 20:15:49 +00:00
<Button label="Save" icon="pi pi-check" @click="saveProduct" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
<Dialog v-model:visible="deleteProductDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
2024-05-24 20:15:49 +00:00
<div class="flex items-center gap-4">
<i class="pi pi-exclamation-triangle !text-3xl" />
2023-02-28 08:29:30 +00:00
<span v-if="product"
>Are you sure you want to delete <b>{{ product.name }}</b
>?</span
>
</div>
<template #footer>
2023-03-03 12:15:20 +00:00
<Button label="No" icon="pi pi-times" text @click="deleteProductDialog = false" />
2024-05-24 20:15:49 +00:00
<Button label="Yes" icon="pi pi-check" @click="deleteProduct" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
<Dialog v-model:visible="deleteProductsDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
2024-05-24 20:15:49 +00:00
<div class="flex items-center gap-4">
<i class="pi pi-exclamation-triangle !text-3xl" />
2023-02-28 08:29:30 +00:00
<span v-if="product">Are you sure you want to delete the selected products?</span>
</div>
<template #footer>
2023-03-03 12:15:20 +00:00
<Button label="No" icon="pi pi-times" text @click="deleteProductsDialog = false" />
<Button label="Yes" icon="pi pi-check" text @click="deleteSelectedProducts" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
<DocSectionCode :code="code" :service="['ProductService']" />
</template>
<script>
import { ProductService } from '@/service/ProductService';
import { FilterMatchMode } from 'primevue/api';
export default {
data() {
return {
products: null,
productDialog: false,
deleteProductDialog: false,
deleteProductsDialog: false,
product: {},
selectedProducts: null,
filters: {},
submitted: false,
statuses: [
{ label: 'INSTOCK', value: 'instock' },
{ label: 'LOWSTOCK', value: 'lowstock' },
{ label: 'OUTOFSTOCK', value: 'outofstock' }
],
code: {
2023-09-22 12:54:14 +00:00
basic: `
2024-05-20 12:14:38 +00:00
<Toolbar class="mb-6">
2023-02-28 08:29:30 +00:00
<template #start>
2023-03-10 08:19:45 +00:00
<Button label="New" icon="pi pi-plus" severity="success" class="mr-2" @click="openNew" />
2023-03-03 12:15:20 +00:00
<Button label="Delete" icon="pi pi-trash" severity="danger" @click="confirmDeleteSelected" :disabled="!selectedProducts || !selectedProducts.length" />
2023-02-28 08:29:30 +00:00
</template>
2024-05-24 20:15:49 +00:00
2023-02-28 08:29:30 +00:00
<template #end>
2024-05-24 20:15:49 +00:00
<FileUpload mode="basic" accept="image/*" :maxFileSize="1000000" label="Import" chooseLabel="Import" class="mr-2" auto />
<Button label="Export" icon="pi pi-upload" severity="help" @click="exportCSV($event)" />
2023-02-28 08:29:30 +00:00
</template>
</Toolbar>
2024-05-24 20:15:49 +00:00
<DataTable
ref="dt"
v-model:selection="selectedProducts"
:value="products"
dataKey="id"
:paginator="true"
:rows="10"
:filters="filters"
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
:rowsPerPageOptions="[5, 10, 25]"
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} products"
>
2023-02-28 08:29:30 +00:00
<template #header>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap gap-2 items-center justify-between">
2023-02-28 08:29:30 +00:00
<h4 class="m-0">Manage Products</h4>
2024-04-08 10:43:16 +00:00
<IconField>
2024-01-30 08:04:48 +00:00
<InputIcon>
<i class="pi pi-search" />
</InputIcon>
2023-02-28 08:29:30 +00:00
<InputText v-model="filters['global'].value" placeholder="Search..." />
2024-01-30 08:04:48 +00:00
</IconField>
2023-02-28 08:29:30 +00:00
</div>
</template>
2024-05-24 20:15:49 +00:00
2023-02-28 08:29:30 +00:00
<Column selectionMode="multiple" style="width: 3rem" :exportable="false"></Column>
2024-05-24 20:15:49 +00:00
<Column field="code" header="Code" sortable style="min-width: 12rem"></Column>
<Column field="name" header="Name" sortable style="min-width: 16rem"></Column>
2023-02-28 08:29:30 +00:00
<Column header="Image">
<template #body="slotProps">
2024-05-20 12:14:38 +00:00
<img :src="\`https://primefaces.org/cdn/primevue/images/product/\${slotProps.data.image}\`" :alt="slotProps.data.image" class="rounded" style="width: 64px" />
2023-02-28 08:29:30 +00:00
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="price" header="Price" sortable style="min-width: 8rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
2024-05-24 20:15:49 +00:00
{{ formatCurrency(slotProps.data.price) }}
2023-02-28 08:29:30 +00:00
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="category" header="Category" sortable style="min-width: 10rem"></Column>
<Column field="rating" header="Reviews" sortable style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
<Rating :modelValue="slotProps.data.rating" :readonly="true" :cancel="false" />
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="inventoryStatus" header="Status" sortable style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
<Tag :value="slotProps.data.inventoryStatus" :severity="getStatusLabel(slotProps.data.inventoryStatus)" />
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column :exportable="false" style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
2023-03-03 12:15:20 +00:00
<Button icon="pi pi-pencil" outlined rounded class="mr-2" @click="editProduct(slotProps.data)" />
<Button icon="pi pi-trash" outlined rounded severity="danger" @click="confirmDeleteProduct(slotProps.data)" />
2023-02-28 08:29:30 +00:00
</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>
<div class="card">
2024-05-20 12:14:38 +00:00
<Toolbar class="mb-6">
2023-02-28 08:29:30 +00:00
<template #start>
2023-03-10 08:19:45 +00:00
<Button label="New" icon="pi pi-plus" severity="success" class="mr-2" @click="openNew" />
2023-03-03 12:15:20 +00:00
<Button label="Delete" icon="pi pi-trash" severity="danger" @click="confirmDeleteSelected" :disabled="!selectedProducts || !selectedProducts.length" />
2023-02-28 08:29:30 +00:00
</template>
<template #end>
2024-05-24 20:15:49 +00:00
<FileUpload mode="basic" accept="image/*" :maxFileSize="1000000" label="Import" chooseLabel="Import" class="mr-2" auto />
<Button label="Export" icon="pi pi-upload" severity="help" @click="exportCSV($event)" />
2023-02-28 08:29:30 +00:00
</template>
</Toolbar>
2024-05-24 20:15:49 +00:00
<DataTable
ref="dt"
v-model:selection="selectedProducts"
:value="products"
dataKey="id"
:paginator="true"
:rows="10"
:filters="filters"
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
:rowsPerPageOptions="[5, 10, 25]"
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} products"
>
2023-02-28 08:29:30 +00:00
<template #header>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap gap-2 items-center justify-between">
2023-02-28 08:29:30 +00:00
<h4 class="m-0">Manage Products</h4>
2024-05-24 20:15:49 +00:00
<IconField>
2024-01-30 08:04:48 +00:00
<InputIcon>
<i class="pi pi-search" />
</InputIcon>
2023-02-28 08:29:30 +00:00
<InputText v-model="filters['global'].value" placeholder="Search..." />
2024-01-30 08:04:48 +00:00
</IconField>
2024-05-24 20:15:49 +00:00
</div>
2023-02-28 08:29:30 +00:00
</template>
<Column selectionMode="multiple" style="width: 3rem" :exportable="false"></Column>
2024-05-24 20:15:49 +00:00
<Column field="code" header="Code" sortable style="min-width: 12rem"></Column>
<Column field="name" header="Name" sortable style="min-width: 16rem"></Column>
2023-02-28 08:29:30 +00:00
<Column header="Image">
<template #body="slotProps">
2024-05-20 12:14:38 +00:00
<img :src="\`https://primefaces.org/cdn/primevue/images/product/\${slotProps.data.image}\`" :alt="slotProps.data.image" class="rounded" style="width: 64px" />
2023-02-28 08:29:30 +00:00
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="price" header="Price" sortable style="min-width: 8rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
2024-05-24 20:15:49 +00:00
{{ formatCurrency(slotProps.data.price) }}
2023-02-28 08:29:30 +00:00
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="category" header="Category" sortable style="min-width: 10rem"></Column>
<Column field="rating" header="Reviews" sortable style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
<Rating :modelValue="slotProps.data.rating" :readonly="true" :cancel="false" />
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="inventoryStatus" header="Status" sortable style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
<Tag :value="slotProps.data.inventoryStatus" :severity="getStatusLabel(slotProps.data.inventoryStatus)" />
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column :exportable="false" style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
2023-03-03 12:15:20 +00:00
<Button icon="pi pi-pencil" outlined rounded class="mr-2" @click="editProduct(slotProps.data)" />
<Button icon="pi pi-trash" outlined rounded severity="danger" @click="confirmDeleteProduct(slotProps.data)" />
2023-02-28 08:29:30 +00:00
</template>
</Column>
</DataTable>
</div>
2024-05-24 20:15:49 +00:00
<Dialog v-model:visible="productDialog" :style="{ width: '450px' }" header="Product Details" :modal="true">
<div class="flex flex-col gap-6 p-fluid">
<img v-if="product.image" :src="\`https://primefaces.org/cdn/primevue/images/product/\${product.image}\`" :alt="product.image" class="block m-auto pb-4" />
<div>
<label for="name" class="block font-bold mb-3">Name</label>
<InputText id="name" v-model.trim="product.name" required="true" autofocus :invalid="submitted && !product.name" />
<small v-if="submitted && !product.name" class="text-red-500">Name is required.</small>
</div>
<div>
<label for="description" class="block font-bold mb-3">Description</label>
<Textarea id="description" v-model="product.description" required="true" rows="3" cols="20" />
</div>
<div>
<label for="inventoryStatus" class="block font-bold mb-3">Inventory Status</label>
<Select id="inventoryStatus" v-model="product.inventoryStatus" :options="statuses" optionLabel="label" placeholder="Select a Status"></Select>
</div>
2023-02-28 08:29:30 +00:00
2024-05-24 20:15:49 +00:00
<div>
<span class="block font-bold mb-4">Category</span>
<div class="grid grid-cols-12 gap-4">
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category1" v-model="product.category" name="category" value="Accessories" />
<label for="category1">Accessories</label>
2023-02-28 08:29:30 +00:00
</div>
2024-05-24 20:15:49 +00:00
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category2" v-model="product.category" name="category" value="Clothing" />
<label for="category2">Clothing</label>
</div>
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category3" v-model="product.category" name="category" value="Electronics" />
<label for="category3">Electronics</label>
</div>
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category4" v-model="product.category" name="category" value="Fitness" />
<label for="category4">Fitness</label>
2023-02-28 08:29:30 +00:00
</div>
</div>
2024-05-24 20:15:49 +00:00
</div>
<div class="grid grid-cols-12 gap-4">
<div class="col-span-6">
<label for="price" class="block font-bold mb-3">Price</label>
<InputNumber id="price" v-model="product.price" mode="currency" currency="USD" locale="en-US" />
2023-02-28 08:29:30 +00:00
</div>
2024-05-24 20:15:49 +00:00
<div class="col-span-6">
<label for="quantity" class="block font-bold mb-3">Quantity</label>
<InputNumber id="quantity" v-model="product.quantity" integeronly />
2023-02-28 08:29:30 +00:00
</div>
</div>
</div>
<template #footer>
2024-05-24 20:15:49 +00:00
<Button label="Cancel" icon="pi pi-times" text @click="hideDialog" />
<Button label="Save" icon="pi pi-check" @click="saveProduct" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
2024-05-24 20:15:49 +00:00
<Dialog v-model:visible="deleteProductDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
<div class="flex items-center gap-4">
<i class="pi pi-exclamation-triangle !text-3xl" />
<span v-if="product"
>Are you sure you want to delete <b>{{ product.name }}</b
>?</span
>
2023-02-28 08:29:30 +00:00
</div>
<template #footer>
2024-05-24 20:15:49 +00:00
<Button label="No" icon="pi pi-times" text @click="deleteProductDialog = false" />
<Button label="Yes" icon="pi pi-check" @click="deleteProduct" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
2024-05-24 20:15:49 +00:00
<Dialog v-model:visible="deleteProductsDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
<div class="flex items-center gap-4">
<i class="pi pi-exclamation-triangle !text-3xl" />
2023-02-28 08:29:30 +00:00
<span v-if="product">Are you sure you want to delete the selected products?</span>
</div>
<template #footer>
2024-05-24 20:15:49 +00:00
<Button label="No" icon="pi pi-times" text @click="deleteProductsDialog = false" />
2023-03-03 12:15:20 +00:00
<Button label="Yes" icon="pi pi-check" text @click="deleteSelectedProducts" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
</div>
</template>
<script>
import { FilterMatchMode } from 'primevue/api';
import { ProductService } from '@/service/ProductService';
export default {
data() {
return {
products: null,
productDialog: false,
deleteProductDialog: false,
deleteProductsDialog: false,
product: {},
selectedProducts: null,
filters: {},
submitted: false,
statuses: [
{label: 'INSTOCK', value: 'instock'},
{label: 'LOWSTOCK', value: 'lowstock'},
{label: 'OUTOFSTOCK', value: 'outofstock'}
]
}
},
created() {
this.initFilters();
},
mounted() {
ProductService.getProducts().then((data) => (this.products = data));
},
methods: {
formatCurrency(value) {
if(value)
return value.toLocaleString('en-US', {style: 'currency', currency: 'USD'});
return;
},
openNew() {
this.product = {};
this.submitted = false;
this.productDialog = true;
},
hideDialog() {
this.productDialog = false;
this.submitted = false;
},
saveProduct() {
this.submitted = true;
2024-03-19 06:58:22 +00:00
if (this.product?.name?.trim()) {
2023-02-28 08:29:30 +00:00
if (this.product.id) {
this.product.inventoryStatus = this.product.inventoryStatus.value ? this.product.inventoryStatus.value: this.product.inventoryStatus;
this.products[this.findIndexById(this.product.id)] = this.product;
this.$toast.add({severity:'success', summary: 'Successful', detail: 'Product Updated', life: 3000});
}
else {
this.product.id = this.createId();
this.product.code = this.createId();
this.product.image = 'product-placeholder.svg';
this.product.inventoryStatus = this.product.inventoryStatus ? this.product.inventoryStatus.value : 'INSTOCK';
this.products.push(this.product);
this.$toast.add({severity:'success', summary: 'Successful', detail: 'Product Created', life: 3000});
}
this.productDialog = false;
this.product = {};
}
},
editProduct(product) {
this.product = {...product};
this.productDialog = true;
},
confirmDeleteProduct(product) {
this.product = product;
this.deleteProductDialog = true;
},
deleteProduct() {
this.products = this.products.filter(val => val.id !== this.product.id);
this.deleteProductDialog = false;
this.product = {};
this.$toast.add({severity:'success', summary: 'Successful', detail: 'Product Deleted', life: 3000});
},
findIndexById(id) {
let index = -1;
for (let i = 0; i < this.products.length; i++) {
if (this.products[i].id === id) {
index = i;
break;
}
}
return index;
},
createId() {
let id = '';
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for ( var i = 0; i < 5; i++ ) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
},
exportCSV() {
this.$refs.dt.exportCSV();
},
confirmDeleteSelected() {
this.deleteProductsDialog = true;
},
deleteSelectedProducts() {
this.products = this.products.filter(val => !this.selectedProducts.includes(val));
this.deleteProductsDialog = false;
this.selectedProducts = null;
this.$toast.add({severity:'success', summary: 'Successful', detail: 'Products Deleted', life: 3000});
},
initFilters() {
this.filters = {
'global': {value: null, matchMode: FilterMatchMode.CONTAINS},
}
2023-03-02 14:17:54 +00:00
},
getStatusLabel(status) {
switch (status) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
2024-04-16 07:35:56 +00:00
return 'warn';
2023-03-02 14:17:54 +00:00
case 'OUTOFSTOCK':
return 'danger';
default:
return null;
}
2023-02-28 08:29:30 +00:00
}
}
}
<\/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>
<div class="card">
2024-05-20 12:14:38 +00:00
<Toolbar class="mb-6">
2023-02-28 08:29:30 +00:00
<template #start>
2023-03-10 08:19:45 +00:00
<Button label="New" icon="pi pi-plus" severity="success" class="mr-2" @click="openNew" />
2023-03-03 12:15:20 +00:00
<Button label="Delete" icon="pi pi-trash" severity="danger" @click="confirmDeleteSelected" :disabled="!selectedProducts || !selectedProducts.length" />
2023-02-28 08:29:30 +00:00
</template>
<template #end>
2024-05-24 20:15:49 +00:00
<FileUpload mode="basic" accept="image/*" :maxFileSize="1000000" label="Import" chooseLabel="Import" class="mr-2" auto />
<Button label="Export" icon="pi pi-upload" severity="help" @click="exportCSV($event)" />
2023-02-28 08:29:30 +00:00
</template>
</Toolbar>
2024-05-24 20:15:49 +00:00
<DataTable
ref="dt"
v-model:selection="selectedProducts"
:value="products"
dataKey="id"
:paginator="true"
:rows="10"
:filters="filters"
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
:rowsPerPageOptions="[5, 10, 25]"
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} products"
>
2023-02-28 08:29:30 +00:00
<template #header>
2024-05-20 12:14:38 +00:00
<div class="flex flex-wrap gap-2 items-center justify-between">
2023-02-28 08:29:30 +00:00
<h4 class="m-0">Manage Products</h4>
2024-05-24 20:15:49 +00:00
<IconField>
2024-01-30 08:04:48 +00:00
<InputIcon>
<i class="pi pi-search" />
</InputIcon>
2023-02-28 08:29:30 +00:00
<InputText v-model="filters['global'].value" placeholder="Search..." />
2024-01-30 08:04:48 +00:00
</IconField>
2024-05-24 20:15:49 +00:00
</div>
2023-02-28 08:29:30 +00:00
</template>
<Column selectionMode="multiple" style="width: 3rem" :exportable="false"></Column>
2024-05-24 20:15:49 +00:00
<Column field="code" header="Code" sortable style="min-width: 12rem"></Column>
<Column field="name" header="Name" sortable style="min-width: 16rem"></Column>
2023-02-28 08:29:30 +00:00
<Column header="Image">
<template #body="slotProps">
2024-05-20 12:14:38 +00:00
<img :src="\`https://primefaces.org/cdn/primevue/images/product/\${slotProps.data.image}\`" :alt="slotProps.data.image" class="rounded" style="width: 64px" />
2023-02-28 08:29:30 +00:00
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="price" header="Price" sortable style="min-width: 8rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
2024-05-24 20:15:49 +00:00
{{ formatCurrency(slotProps.data.price) }}
2023-02-28 08:29:30 +00:00
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="category" header="Category" sortable style="min-width: 10rem"></Column>
<Column field="rating" header="Reviews" sortable style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
<Rating :modelValue="slotProps.data.rating" :readonly="true" :cancel="false" />
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column field="inventoryStatus" header="Status" sortable style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
<Tag :value="slotProps.data.inventoryStatus" :severity="getStatusLabel(slotProps.data.inventoryStatus)" />
</template>
</Column>
2024-05-24 20:15:49 +00:00
<Column :exportable="false" style="min-width: 12rem">
2023-02-28 08:29:30 +00:00
<template #body="slotProps">
2023-03-03 12:15:20 +00:00
<Button icon="pi pi-pencil" outlined rounded class="mr-2" @click="editProduct(slotProps.data)" />
<Button icon="pi pi-trash" outlined rounded severity="danger" @click="confirmDeleteProduct(slotProps.data)" />
2023-02-28 08:29:30 +00:00
</template>
</Column>
</DataTable>
</div>
2024-05-24 20:15:49 +00:00
<Dialog v-model:visible="productDialog" :style="{ width: '450px' }" header="Product Details" :modal="true">
<div class="flex flex-col gap-6 p-fluid">
<img v-if="product.image" :src="\`https://primefaces.org/cdn/primevue/images/product/\${product.image}\`" :alt="product.image" class="block m-auto pb-4" />
<div>
<label for="name" class="block font-bold mb-3">Name</label>
<InputText id="name" v-model.trim="product.name" required="true" autofocus :invalid="submitted && !product.name" />
<small v-if="submitted && !product.name" class="text-red-500">Name is required.</small>
</div>
<div>
<label for="description" class="block font-bold mb-3">Description</label>
<Textarea id="description" v-model="product.description" required="true" rows="3" cols="20" />
</div>
<div>
<label for="inventoryStatus" class="block font-bold mb-3">Inventory Status</label>
<Select id="inventoryStatus" v-model="product.inventoryStatus" :options="statuses" optionLabel="label" placeholder="Select a Status"></Select>
</div>
2023-02-28 08:29:30 +00:00
2024-05-24 20:15:49 +00:00
<div>
<span class="block font-bold mb-4">Category</span>
<div class="grid grid-cols-12 gap-4">
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category1" v-model="product.category" name="category" value="Accessories" />
<label for="category1">Accessories</label>
2023-02-28 08:29:30 +00:00
</div>
2024-05-24 20:15:49 +00:00
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category2" v-model="product.category" name="category" value="Clothing" />
<label for="category2">Clothing</label>
</div>
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category3" v-model="product.category" name="category" value="Electronics" />
<label for="category3">Electronics</label>
</div>
<div class="flex items-center gap-2 col-span-6">
<RadioButton id="category4" v-model="product.category" name="category" value="Fitness" />
<label for="category4">Fitness</label>
2023-02-28 08:29:30 +00:00
</div>
</div>
2024-05-24 20:15:49 +00:00
</div>
<div class="grid grid-cols-12 gap-4">
<div class="col-span-6">
<label for="price" class="block font-bold mb-3">Price</label>
<InputNumber id="price" v-model="product.price" mode="currency" currency="USD" locale="en-US" />
2023-02-28 08:29:30 +00:00
</div>
2024-05-24 20:15:49 +00:00
<div class="col-span-6">
<label for="quantity" class="block font-bold mb-3">Quantity</label>
<InputNumber id="quantity" v-model="product.quantity" integeronly />
2023-02-28 08:29:30 +00:00
</div>
</div>
</div>
<template #footer>
2024-05-24 20:15:49 +00:00
<Button label="Cancel" icon="pi pi-times" text @click="hideDialog" />
<Button label="Save" icon="pi pi-check" @click="saveProduct" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
2024-05-24 20:15:49 +00:00
<Dialog v-model:visible="deleteProductDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
<div class="flex items-center gap-4">
<i class="pi pi-exclamation-triangle !text-3xl" />
<span v-if="product"
>Are you sure you want to delete <b>{{ product.name }}</b
>?</span
>
2023-02-28 08:29:30 +00:00
</div>
<template #footer>
2024-05-24 20:15:49 +00:00
<Button label="No" icon="pi pi-times" text @click="deleteProductDialog = false" />
<Button label="Yes" icon="pi pi-check" @click="deleteProduct" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
2024-05-24 20:15:49 +00:00
<Dialog v-model:visible="deleteProductsDialog" :style="{ width: '450px' }" header="Confirm" :modal="true">
<div class="flex items-center gap-4">
<i class="pi pi-exclamation-triangle !text-3xl" />
2023-02-28 08:29:30 +00:00
<span v-if="product">Are you sure you want to delete the selected products?</span>
</div>
<template #footer>
2024-05-24 20:15:49 +00:00
<Button label="No" icon="pi pi-times" text @click="deleteProductsDialog = false" />
2023-03-03 12:15:20 +00:00
<Button label="Yes" icon="pi pi-check" text @click="deleteSelectedProducts" />
2023-02-28 08:29:30 +00:00
</template>
</Dialog>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { FilterMatchMode } from 'primevue/api';
import { useToast } from 'primevue/usetoast';
import { ProductService } from '@/service/ProductService';
onMounted(() => {
ProductService.getProducts().then((data) => (products.value = data));
});
const toast = useToast();
const dt = ref();
const products = ref();
const productDialog = ref(false);
const deleteProductDialog = ref(false);
const deleteProductsDialog = ref(false);
const product = ref({});
const selectedProducts = ref();
const filters = ref({
'global': {value: null, matchMode: FilterMatchMode.CONTAINS},
});
const submitted = ref(false);
const statuses = ref([
{label: 'INSTOCK', value: 'instock'},
{label: 'LOWSTOCK', value: 'lowstock'},
{label: 'OUTOFSTOCK', value: 'outofstock'}
]);
const formatCurrency = (value) => {
if(value)
return value.toLocaleString('en-US', {style: 'currency', currency: 'USD'});
return;
};
const openNew = () => {
product.value = {};
submitted.value = false;
productDialog.value = true;
};
const hideDialog = () => {
productDialog.value = false;
submitted.value = false;
};
const saveProduct = () => {
submitted.value = true;
2024-03-19 06:58:22 +00:00
if (product?.value.name?.trim()) {
2023-02-28 08:29:30 +00:00
if (product.value.id) {
product.value.inventoryStatus = product.value.inventoryStatus.value ? product.value.inventoryStatus.value : product.value.inventoryStatus;
products.value[findIndexById(product.value.id)] = product.value;
toast.add({severity:'success', summary: 'Successful', detail: 'Product Updated', life: 3000});
}
else {
product.value.id = createId();
product.value.code = createId();
product.value.image = 'product-placeholder.svg';
product.value.inventoryStatus = product.value.inventoryStatus ? product.value.inventoryStatus.value : 'INSTOCK';
products.value.push(product.value);
toast.add({severity:'success', summary: 'Successful', detail: 'Product Created', life: 3000});
}
productDialog.value = false;
product.value = {};
}
};
const editProduct = (prod) => {
product.value = {...prod};
productDialog.value = true;
};
const confirmDeleteProduct = (prod) => {
product.value = prod;
deleteProductDialog.value = true;
};
const deleteProduct = () => {
products.value = products.value.filter(val => val.id !== product.value.id);
deleteProductDialog.value = false;
product.value = {};
toast.add({severity:'success', summary: 'Successful', detail: 'Product Deleted', life: 3000});
};
const findIndexById = (id) => {
let index = -1;
for (let i = 0; i < products.value.length; i++) {
if (products.value[i].id === id) {
index = i;
break;
}
}
return index;
};
const createId = () => {
let id = '';
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for ( var i = 0; i < 5; i++ ) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
}
const exportCSV = () => {
dt.value.exportCSV();
};
const confirmDeleteSelected = () => {
deleteProductsDialog.value = true;
};
const deleteSelectedProducts = () => {
products.value = products.value.filter(val => !selectedProducts.value.includes(val));
deleteProductsDialog.value = false;
selectedProducts.value = null;
toast.add({severity:'success', summary: 'Successful', detail: 'Products Deleted', life: 3000});
};
2023-03-02 14:17:54 +00:00
const getStatusLabel = (status) => {
switch (status) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
2024-04-16 07:35:56 +00:00
return 'warn';
2023-03-02 14:17:54 +00:00
case 'OUTOFSTOCK':
return 'danger';
default:
return null;
}
};
2023-02-28 08:29:30 +00:00
<\/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.initFilters();
},
methods: {
2023-12-31 14:08:33 +00:00
loadDemoData() {
ProductService.getProducts().then((data) => (this.products = data));
},
2023-02-28 08:29:30 +00:00
formatCurrency(value) {
if (value) return value.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
return;
},
openNew() {
this.product = {};
this.submitted = false;
this.productDialog = true;
},
hideDialog() {
this.productDialog = false;
this.submitted = false;
},
saveProduct() {
this.submitted = true;
2024-03-19 06:58:22 +00:00
if (this.product?.name?.trim()) {
2023-02-28 08:29:30 +00:00
if (this.product.id) {
this.product.inventoryStatus = this.product.inventoryStatus.value ? this.product.inventoryStatus.value : this.product.inventoryStatus;
this.products[this.findIndexById(this.product.id)] = this.product;
this.$toast.add({ severity: 'success', summary: 'Successful', detail: 'Product Updated', life: 3000 });
} else {
this.product.id = this.createId();
this.product.code = this.createId();
this.product.image = 'product-placeholder.svg';
this.product.inventoryStatus = this.product.inventoryStatus ? this.product.inventoryStatus.value : 'INSTOCK';
this.products.push(this.product);
this.$toast.add({ severity: 'success', summary: 'Successful', detail: 'Product Created', life: 3000 });
}
this.productDialog = false;
this.product = {};
}
},
editProduct(product) {
this.product = { ...product };
this.productDialog = true;
},
confirmDeleteProduct(product) {
this.product = product;
this.deleteProductDialog = true;
},
deleteProduct() {
this.products = this.products.filter((val) => val.id !== this.product.id);
this.deleteProductDialog = false;
this.product = {};
this.$toast.add({ severity: 'success', summary: 'Successful', detail: 'Product Deleted', life: 3000 });
},
findIndexById(id) {
let index = -1;
for (let i = 0; i < this.products.length; i++) {
if (this.products[i].id === id) {
index = i;
break;
}
}
return index;
},
createId() {
let id = '';
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < 5; i++) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
},
exportCSV() {
this.$refs.dt.exportCSV();
},
confirmDeleteSelected() {
this.deleteProductsDialog = true;
},
deleteSelectedProducts() {
this.products = this.products.filter((val) => !this.selectedProducts.includes(val));
this.deleteProductsDialog = false;
this.selectedProducts = null;
this.$toast.add({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 });
},
initFilters() {
this.filters = {
global: { value: null, matchMode: FilterMatchMode.CONTAINS }
};
},
getStatusLabel(status) {
switch (status) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
2024-04-16 07:35:56 +00:00
return 'warn';
2023-02-28 08:29:30 +00:00
case 'OUTOFSTOCK':
return 'danger';
default:
return null;
}
}
}
};
</script>