0

I have an input where users can search for titles. Date picker which is used mengxiong10/vue2-datepicker. (Any other picker is welcome)

How can the array be filtered by only Search, only Date, or both Search and Date? (Search via Title works ).

How would the search be in array vs nested array?

HTML-

<input class="form-control" type="text" v-model="search" placeholder="Search" /></div>  
<date-picker v-model="datefilter" range></date-picker>

<div v-for="item in resultQuery">
 {{item.title}}
</div>               

Vuejs-

<script>
  import DatePicker from 'vue2-datepicker';
  import 'vue2-datepicker/index.css';

  export default {
    el: '#app',
    components: { DatePicker },
    data() {
      return {
        search: null,
        resources:[
            {title:"AAA",date: 1622192760},
            {title:"BBB",date: 1622624760},
            {title:"CCC",date: 1620810360},
            {title:"DDD",date: 1625907960},
            {title:"EEE",date: 1623315960},
        ],
        resources2:[
            {id:1,items: [{ title: 'a',date: 1622192760 }, {name: 'aa', date: 1622624760 }]},
            {id:2,items: [{ title: 'b',date: 1623315960 }, {name: 'bb', date: 1625907960 }]},
            {id:3,items: [{ title: 'c',date: 1622624760 }, {name: 'cc', date: 1622624760 }]},
            {id:4,items: [{ title: 'd',date: 1622624760 }, {name: 'dd', date: 1622624760 }]},
            {id:5,items: [{ title: 'e',date: 1622192760 }, {name: 'ee', date: 1622624760 }]},
        ],

        datefilter: null,
      };
    },
    computed: {
    resultQuery(){
      if(this.search){
      return this.resources.filter((item)=>{
        return item.title.toLowerCase().includes(this.search.toLowerCase())) }
      })
      }else{
        return this.resources;
      }
    }
  }
  };
</script>

1 Answer 1

1

You could factor out the filtering into its own functions:

const byDate = (item) => {
  const itemDate = new Date(item.date);
  return itemDate >= new Date(this.datefilter[0]) &&
         itemDate <= new Date(this.datefilter[1])
}

const byTitle = (item) =>
  item.title.toLowerCase().includes(this.search.toLowerCase())

Then pass them into this.resources.filter() when the search field isn't empty or when a date range is selected:

export default {
  computed: {
    resultQuery() {
      //...
      let results = this.resources

      if (this.search) {
        results = results.filter(byTitle)
      }

      const hasDateFilter = this.datefilter?.length >= 2 && this.datefilter[0] && this.datefilter[1]
      if (hasDateFilter) {
        results = results.filter(byDate)
      }

      return results
    }
  }
}

demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.