Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
how i make filter search in vue js with javascript

can i write a filter search code with just data and methods function?

i try do this :

var fil = new Vue ({
            el: '#app',
            data: {
                search: '',
                proL: ['css', 'c++', 'cs#'],
            },
            methods: {
                filter: function(){

                        var search_key = this.search.val();
                        
                        this.proL.filter(function(){
                            this.proL.toggle(this.proL.text().indexOf(search_key) > -1);
                        });

                }
            }
        });

and this is the html code

<div id="app">
        <div >
          <input @keyup="filter()" type="text" v-model="search" placeholder="Search title.."/>
        </div>

        <div id="pro">
            <h4 v-for="item in proL">{{item}}</h4>
        </div>
    </div>

๐Ÿ˜…๐Ÿ˜…

about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

Try to use a computed property to define the filtered items and use an arrow function as filter function callback in order to get access to this:

new Vue({
  el: '#demo',
  data: {
      search: '',
      proL: ['css', 'c++', 'cs#'],
  },
  computed: {
    filtered(){
      return this.proL.filter(e => e.toLowerCase().includes(this.search.toLowerCase()))
    }
  }
})

Vue.config.productionTip = false
Vue.config.devtools = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div >
    <input type="text" v-model="search" placeholder="Search title.."/>
  </div>

  <div id="pro">
      <h4 v-for="item in filtered">{{item}}</h4>
  </div>
</div>

about 4 years ago ยท Juan Pablo Isaza Report

0

A better approach would be using computed handler

new Vue({
  el: '#demo',
  data: {
      search: '',
      proL: ['css', 'c++', 'cs#'],
  },
  computed: {
    filteredProL(){
      if(this.search === '') return this.proL; // If search input is empty then display all items
      else return this.proL.filter(e => e.toLowerCase().indexOf(this.search) > -1) // Working logic
    }
  }
})

Vue.config.productionTip = false
Vue.config.devtools = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div >
    <input type="text" v-model="search" placeholder="Search title.."/>
  </div>

  <div id="pro">
      <h4 v-for="item in filteredProl">{{item}}</h4>
  </div>
</div>

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!