Event |
Description |
products-selected |
This event emits when the action button is clicked (bottom right of the modal) and provides an array of all of the selected product(s)/variant(s). |
selected |
This event emits when the action button is clicked (bottom right of the modal) and is emitted for each product/variant that has been selected - only a single product/variant is provided. |
Example
The usual use-case for the Product Picker is to allow users to select products and/or variants to be added to a list. This example code is a Blade view that allows the user to select up to 5 products or variants and then displays them in a list with a custom message. It also allows the user to remove the selected products/variants. The next steps to make this code fully functional would be to wrap the table in a form and to add a submit button that saves the products/variants.
@extends('admin::layouts.main')
@section('content')
<h2>
<a href="{{ route('admin.modules') }}" class="btn">@include('admin::icons.back') Back</a>
<span class="ml-4">My Module</span>
</h2>
<div class="card p-0">
<table>
<thead>
<tr class="header">
<th class="whitespace-no-wrap">Model</th>
<th class="whitespace-no-wrap">Sku</th>
<th class="whitespace-no-wrap">Message</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in list" :key="item.buyable_id + item.buyable_type" v-cloak>
<td class="whitespace-no-wrap">
<input type="hidden" :name="'items[' + index + '][buyable_id]'" :value="item.buyable_id">
<input type="hidden" :name="'items[' + index + '][buyable_type]'" :value="item.buyable_type">
<span v-text="item.model"></span>
</td>
<td class="whitespace-no-wrap" v-if="item.sku" v-text="item.sku"></td>
<td class="whitespace-no-wrap" v-else><span class="text-grey">—</span></td>
<td class="whitespace-no-wrap">
<input type="text" :name="'items[' + index + '][message]'" v-model="item.message" placeholder="Message">
</td>
<td>
<div class="flex items-center justify-end">
<a href="#" @click.prevent="list.splice(index, 1)">@include('admin::icons.bin')</a>
</div>
</td>
</tr>
<tr v-if="list.length === 0">
<td colspan="4">No data to show</td>
</tr>
</tbody>
</table>
<product-picker v-slot="picker"
v-if="list.length < 5"
get-products-url="{{ route('admin.catalog.products.picker') }}"
:max-selection="5"
:pick-product="true"
:emit-name="true"
:emit-model="true"
:emit-sku="true"
@selected="selected">
<a href="#" @click.prevent="picker.open" class="btn m-4">Add Product</a>
</product-picker>
</div>
@endsection
@push('scripts')
<script>
window.AeroAdmin.addData('list', []);
window.AeroAdmin.addMethod('selected', function (item) {
if (this.findIndex(item.buyable_id, item.buyable_type) === -1) {
this.list.push({
...item,
message: '',
});
} else {
this.$notify({
type: 'error',
title: (item.sku ?? item.name) + ' has already been added to the list',
});
}
});
window.AeroAdmin.addMethod('findIndex', function (id, type) {
return this.list.findIndex((l) => l.buyable_id === id && l.buyable_type === type);
});
</script>
@endpush