Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Client side search in Vue 2
Graham Morby
Graham Morby

Posted on

     

Client side search in Vue 2

So you have some data you want to filter? Or a cheeky little search bar and want to allow the user to search through an array.

In the following tutorial we will be using:

Ok so here goes.

So we have a basic Vue template with our normal set up.

<template><div></div></template><script>exportdefault{computed:{},components:{},data(){return{};},};</script>
Enter fullscreen modeExit fullscreen mode

So I'm assuming you have some Vue experience and have knowledge of how to set up NPM and any other tools you need. So let's first create a search bar at the top of our page.

<template><div><divclass="col-6 mb-3"><b-form><labelclass="mr-sm-2"for="inline-form-custom-select-pref">Search</label><b-form-inputid="inline-form-custom-select-pref"class="mb-2 mr-sm-2 mb-sm-0":value="null"placeholder="Search for a company"v-model="search"></b-form-input></b-form></div></div></template><script>exportdefault{computed:{},components:{},data(){return{search:'',articles:[]};},};</script>
Enter fullscreen modeExit fullscreen mode

So we have a search field and an empty data point for search which will use as the keyword in our search. Ok so I don't have to type out a huge array we will assume our collection has a list of objects inside with the following fields:

  • title
  • article
  • image
  • created_at

So we have a huge array of articles and we have a search field and an empty search data point. So what we can do now is create a computed property that will filter the array.

filteredArticles(){returnthis.articles.filter(blog=>{returnblog.title.toLowerCase().includes(this.title.toLowerCase());})}
Enter fullscreen modeExit fullscreen mode

So our computed property takes the full array, returns a named function that takes the title of each article and puts it to lower case, then takes our search term pops that to lower case and matches the two, if they match it returns it and if not discards it.

So how do we use our newly filtered list? Well if we have a Bootstrap table we can simply call in our filtered articles list as so:

<b-tablestripedhover:items="filteredArticles">
Enter fullscreen modeExit fullscreen mode

And thats it, our search on the client side should work perfectly with all the data flowing through our computed property. Now I have not gone into much detail with the Bootstrap Vue side of things and if you need further information please use the link at the top that I have provided.

And here is the full file you can use and amend as you need:

<template><div><divclass="col-6 mb-3"><b-form><labelclass="mr-sm-2"for="inline-form-custom-select-pref">Search</label><b-form-inputid="inline-form-custom-select-pref"class="mb-2 mr-sm-2 mb-sm-0":value="null"placeholder="Search for a company"v-model="search"></b-form-input></b-form></div><b-tablestripedhover:items="filteredArticles"></div></template><script>exportdefault{computed:{filteredArticles(){returnthis.articles.filter((blog)=>{returnblog.title.toLowerCase().includes(this.title.toLowerCase());});},},components:{},data(){return{search:"",articles:[]};},};</script>
Enter fullscreen modeExit fullscreen mode

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jompa141 profile image
Jompa
I think the Web is a super power

very good and clean! congrats!!

CollapseExpand
 
grahammorby profile image
Graham Morby
Hey guys, 15-year developer! I code with Vue.js and Python, I love to learn but also love to teach! So I try and write informative tutorials and posts. I am new to blogging and would love any feedback
  • Location
    Portsmouth UK
  • Work
    Senior Developer Leighton
  • Joined

Thank you! Hope i helped!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hey guys, 15-year developer! I code with Vue.js and Python, I love to learn but also love to teach! So I try and write informative tutorials and posts. I am new to blogging and would love any feedback
  • Location
    Portsmouth UK
  • Work
    Senior Developer Leighton
  • Joined

More fromGraham Morby

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp