Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

filter table search

new Vue({
  el: '#demo',

  data: {
    sortKey: 'name',
    reverse: false,
    searchName: '',
    searchOperator: '',
    searchAge: '',
    columns: ['name', 'age'],
    newUser: {},
    search: "",
    name: "",
    age: "",

    users: [
      { name: 'John', age: 50 },
      { name: 'Jane', age: 22 },
      { name: 'Paul', age: 34 },
      { name: 'Kate', age: 15 },
      { name: 'Amanda', age: 65 },
      { name: 'Steve', age: 38 },
      { name: 'Keith', age: 21 },
      { name: 'Don', age: 50 },
      { name: 'Susan', age: 21 }
    ]
  },
  methods: {
    sortBy: function (sortKey) {
      this.reverse = (this.sortKey == sortKey) ? !this.reverse : false;

      this.sortKey = sortKey;
    }, 
    filterByName : function(user) {
      // no search, don't filter : 
      if (this.searchName.length === 0) {
        return true;
      }

      return  (user.name.toLowerCase().indexOf(this.searchName.toLowerCase()) > -1);
    }, 
    filterByAge : function (user) {
      // no operator selected or no age typed, don't filter : 
      if (this.searchOperator.length === 0 || this.age.length === 0) {
        return true;
      }

      if (this.searchOperator === '>') {
        return (user.age > this.age); 
      } else  if (this.searchOperator === '<') {
        return (user.age < this.age);
      }      
    }, 
    orderBy : function (userA, userB) {
      let condition = (userA[this.sortKey] > userB[this.sortKey]);
      if (this.reverse) {
        return !condition;
      } else {
        return condition;
      }
    }
  },
  computed: {
    filteredPersons: function () {
      return this.users
      .filter(this.filterByName)
      .filter(this.filterByAge)
      .sort(this.orderBy);
    }
  },  
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript get the last array element 
Javascript :: javascript pipe async functions 
Javascript :: Search by text score in mongodb 
Javascript :: hamburger menu js 
Javascript :: datatable ajax reload 
Javascript :: call node js function from javascript 
Javascript :: sails js 
Javascript :: empty array 
Javascript :: vue prop using variable 
Javascript :: .has js 
Javascript :: how to remove first element of array in javascript 
Javascript :: arrow function in javascript 
Javascript :: date time react component 
Javascript :: jq cheat sheet 
Javascript :: download in react 
Javascript :: run react native with debugger breakpoint 
Javascript :: react class names 
Javascript :: last element from list javascript 
Javascript :: foreach function into arrow with addeventlistener 
Javascript :: Everything Be True 
Javascript :: filter based on input typing react 
Javascript :: can we send image in json in angular 
Javascript :: jest visit a page 
Javascript :: angular generer guard 
Javascript :: stop monitorevents 
Javascript :: how to get selected value from between form tags in Angular 
Javascript :: proet javascript web 
Javascript :: nsenter 
Javascript :: progrmatically change audio src 
Javascript :: react native listview date separator site:stackoverflow.com 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =