Create a searchable select field with InertiaJS and vue multiselect
- Install vue-multiselect
1npm install vue-multiselect --save
- Import and register the
Multiselect
component
1import Multiselect from 'vue-multiselect';2 3export default {4 components: {Multiselect}5}
- Import the styles in your
app.css
file
1@import "../../node_modules/vue-multiselect/dist/vue-multiselect.min.css";
- Prepare your InertiaJS form
1export default { 2 data() { 3 return { 4 form: this.$inertia.form({ 5 mechanic_id: '', 6 // other fields 7 }) 8 } 9 }10}
- Add the multiselect directive in your template and setup it's
options
, property to be used aslabel
, an unique property fortrack-by
to identify each option, and listen to the@search-change
and@input
events.
1<template> 2 <form> 3 <label for="mechanic_id">Mechanic</label> 4 <multiselect 5 v-model="selectedMechanic" 6 id="mechanic_id" placeholder="Search mechanic" 7 :options="mechanics" label="name" track-by="id" 8 @search-change="onSearchMechanicsChange" 9 @input="onSelectedMechanic"10 />11 </form>12</template>
- Define the above properties and methods.
mechanics
will come from the server, so it will be defined as a prop with an empty array as default, whileonSearchMechanicsChange
andonSelectedMechanic
will defined as methods.
1export default { 2 props: { 3 mechanics: { 4 type: Array, 5 default: () => [] 6 } 7 }, 8 data() { 9 return {10 selectedMechanic: undefined,11 form: this.$inertia.form({12 mechanic_id: '',13 // other fields14 })15 }16 },17 methods: {18 onSearchMechanicsChange(term) {19 this.$inertia.get('/jobs/create', {term}, {20 preserveState: true,21 preserveScroll: true,22 replace: true23 })24 },25 onSelectedMechanic(consultant) {26 this.form.consultant_id = consultant.id;27 },28 }29}
The reason why we set the multiselect v-model
to selectedMechanic
and not form.mechanic_id
is because the multiselect component expects its v-model
to be an element from the mechanics array we pass in as options.
The onSearchMechanicsChange
will make an inertia call to the server while passing the entered term
in the search field. We also need to make sure the local state and scroll will stay the same, so we'll instruct inertia to preserveState
and preserveScroll
.
Since the search request will fire multiple times, we also want to replace
the current request in the browser history such that when we browse back, we browse to the previous page, and not to the previous term that was typed in the search box.
Lastly, the onSelectedMechanic
will set the form.consultant_id
to the id of the selected mechanic
.
-
1Determining active routes05:24
-
2Create a searchable list09:05
-
3Handling regular forms16:41
-
4Toast notifications17:32
-
5Inline forms validation09:15
-
6Authorization08:45
-
7Structuring layouts10:43
-
8Infinite scrolling with InertiaJs and Laravel15:57
-
9Toggling likes13:27
-
10Notifications with Laravel Echo and Pusher in InertiaJS applications (part one)14:47
-
11Realtime Notifications with Laravel Echo and Pusher in InertiaJS applications (part two)09:07
-
12Building a user following / unfollowing system with Laravel and InertiaJS22:39
-
13Changing button labels on hover and InertiaJS progress indicators05:34
-
14Building the home feed and infinite scroll component with Laravel and InertiaJS17:52
-
15Building a form component to post new tweets with Laravel and InertiaJS19:38
-
16Media attachments with Laravel and InertiaJS41:46
-
17Avatar image input component with VueJS, Inertia, and Tailwind CSS14:44
-
18Bugfixing the avatar input component04:16
-
19New InertiaJs form helper - less boilerplate, more awesomeness08:30
-
20InertiaJS scroll management13:34
-
21InertiaJS submitting form arrays and error handling11:46
-
Create a searchable select field with InertiaJS and vue multiselect13:10
-
23InertiaJS data evaluation in Laravel and VueJS applications04:37
-
24Creating a datatable with Laravel and InertiaJS10:58
-
25Customize the default Jetstream view responses in Laravel apps using the Inertia stack03:46
-
26Fuzzy searching with Fuse.js in Laravel & InertiaJS applications10:38
-
27Handling translations in Laravel and InertiaJS applications12:24
-
28Replace auth redirect with modal dialog in InertiaJS & Laravel applications06:49
-
29Prevalidate forms in InertiaJS and Laravel applications16:06
-
30Prevent recently logged out users from seeing private pages when navigating back04:08
-
31InertiaJS frontend validation using Intus11:50
-
32InertiaJS & Vue 3 toast notifications18:33
-
1Determining active routes05:24
-
2Create a searchable list09:05
-
3Handling regular forms16:41
-
4Toast notifications17:32
-
5Inline forms validation09:15
-
6Authorization08:45
-
7Structuring layouts10:43
-
8Infinite scrolling with InertiaJs and Laravel15:57
-
9Toggling likes13:27
-
10Notifications with Laravel Echo and Pusher in InertiaJS applications (part one)14:47
-
11Realtime Notifications with Laravel Echo and Pusher in InertiaJS applications (part two)09:07
-
12Building a user following / unfollowing system with Laravel and InertiaJS22:39
-
13Changing button labels on hover and InertiaJS progress indicators05:34
-
14Building the home feed and infinite scroll component with Laravel and InertiaJS17:52
-
15Building a form component to post new tweets with Laravel and InertiaJS19:38
-
16Media attachments with Laravel and InertiaJS41:46
-
17Avatar image input component with VueJS, Inertia, and Tailwind CSS14:44
-
18Bugfixing the avatar input component04:16
-
19New InertiaJs form helper - less boilerplate, more awesomeness08:30
-
20InertiaJS scroll management13:34
-
21InertiaJS submitting form arrays and error handling11:46
-
Create a searchable select field with InertiaJS and vue multiselect13:10
-
23InertiaJS data evaluation in Laravel and VueJS applications04:37
-
24Creating a datatable with Laravel and InertiaJS10:58
-
25Customize the default Jetstream view responses in Laravel apps using the Inertia stack03:46
-
26Fuzzy searching with Fuse.js in Laravel & InertiaJS applications10:38
-
27Handling translations in Laravel and InertiaJS applications12:24
-
28Replace auth redirect with modal dialog in InertiaJS & Laravel applications06:49
-
29Prevalidate forms in InertiaJS and Laravel applications16:06
-
30Prevent recently logged out users from seeing private pages when navigating back04:08
-
31InertiaJS frontend validation using Intus11:50
-
32InertiaJS & Vue 3 toast notifications18:33