new Vue({
el: '#app',
data: {
selectedType: '',
startDate:null,
endDate:null,
items: [
{
name: 'Nolan',
type: 'mercedes',
year: '2020',
country: 'england',
date: '08/01/2020'
},
{
name: 'Edgar',
type: 'bmw',
year: '2020',
country:'belgium',
date: '08/11/2020'
},
{
name: 'John',
type: 'bmw',
year: '2019',
country: 'england',
date: '08/21/2020'
},
{
name: 'Axel',
type: 'mercedes',
year: '2020',
country: 'england',
date: '08/01/2020'
}
]
},
computed: {
filterItem() {
let filterType = this.selectedType;
let startDate = this.localizeDate(this.startDate);
let endDate = this.localizeDate(this.endDate);
const itemsByType = filterType ? this.items.filter(item => item.type === filterType) : this.items
return itemsByType
.filter(item => {
const itemDate = new Date(item.date)
if (startDate && endDate) {
return startDate <= itemDate && itemDate <= endDate;
}
if (startDate && !endDate) {
return startDate <= itemDate;
}
if (!startDate && endDate) {
return itemDate <= endDate;
}
return true;
})
}
},
methods: {
localizeDate(date) {
// Normalize ISO format dates from date picker with locale specific format provided in the data
// For comparison with local timezone
if (!date || !date.includes('-')) return date
const [yyyy, mm, dd] = date.split('-')
return new Date(`${mm}/${dd}/${yyyy}`)
},
formatDate(date) {
return new Intl.DateTimeFormat('en-US', { dateStyle: 'long' }).format(new Date(date))
}
}
})
.list-item {
margin-top: 50px;
}
.card {
box-shadow: 0px 10px 16px rgba(0, 0, 0, 0.16);
width: 400px;
padding: 20px 30px;
margin-bottom: 30px;
}
select,input {
border: none;
padding: 10px 15px;
background-color: #c1c1c1;
border-radius: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<label for="">Type</label>
<select v-model="selectedType">
<option value="" disabled selected hidden>Type</option>
<option value="mercedes">Mercedes</option>
<option value="bmw">BMW</option>
</select>
<label for="">From</label>
<input type="date" v-model="startDate">
<label for="">To</label>
<input type="date" v-model="endDate">
<div class="list-item" v-for="item in filterItem">
<div class="card">
<p>Name: {{ item.name }}</p>
<p>Car: {{ item.type }}</p>
<p>Date: {{ formatDate(item.date) }}</p>
<p>Country: {{ item.country }}</p>
</div>
</div>
</div>