gpt4 book ai didi

javascript - 如何使用vue js过滤日期范围?

转载 作者:行者123 更新时间:2023-12-04 00:51:26 25 4
gpt4 key购买 nike

我正在学习 vue.js,并且正在尝试使用范围日期的过滤器功能。场景是:先按类型过滤,再按日期范围过滤(有开始日期和结束日期)。选择结束日期后将显示结果。我已经创建了第一个过滤器,它是 type,它可以工作。我已经搜索了各种日期范围的方法,但仍然找不到合适的方法。如何使用 computedmethods 过滤日期范围?如果有人知道,请你一步步告诉我好吗?

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: function () {

let filterType = this.selectedType
if (!filterType) return this.items; // when filterType not selected

let startDate = this.startDate && new Date(this.startDate);
let endDate = this.endDate && new Date(this.endDate);

return this.items.filter(item => {
return item.type == filterType;
}).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; // when neither startDate nor endDate selected
})

}
}
})
.list-item {
margin-top: 50px;
}

#app {
position: relative;
padding-bottom: 200px;
}

span {
margin: 0 15px;
cursor: pointer;
}

.filter-box {
margin-top: 15px;
}

.card {
box-shadow: 0px 10px 16px rgba(0, 0, 0, 0.16);
width: 400px;
padding: 20px 30px;
margin-bottom: 30px;
}

button {
background-color: #1cf478;
border: none;
padding: 10px 25px;
font-weight: bold;
border-radius: 15px;
}

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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.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: {{ item.date }}</p>
<p>Country: {{ item.country }}</p>
</div>
</div>
</div>

最佳答案

修复方法如下:

  1. 创建 startDateendDate 作为 Date。但是,请注意日期选择器使用 ISO 字符串格式 (yyyy-mm-dd),即 UTC。数据包含特定于区域设置的日期 (mm/dd/yyyy),因此 Date 会使用本地时区解析字符串。我们需要规范化日期,以便比较具有相同时区偏移量的日期(首选本地):

    methods: {
    localizeDate(date) {
    if (!date || !date.includes('-')) return date
    const [yyyy, mm, dd] = date.split('-')
    return new Date(`${mm}/${dd}/${yyyy}`)
    }
    }
    let startDate = this.localizeDate(this.startDate);
    let endDate = this.localizeDate(this.endDate);
  2. if-else 中的日期比较明确检查是否为 null,但它们的值不会是 null 因为您正在实例化 Date 对象。通过步骤 1 中的更新,日期值为 Datefalse,因此您可以更新比较以检查是否存在虚假值而不是 null:

    if (startDate && endDate) {/*...*/}
    if (startDate && !endDate) {/*...*/}
    if (!startDate && endDate) {/*...*/}
  3. selectedType 永远不会是 null,但您可以检查它是否为假以确定类型是否未指定。在这种情况下,您可以只返回原始数组以避免不必要地调用 Array.prototype.filter:

    const itemsByType = filterType ? this.items.filter(item => item.type === filterType) : this.items
  4. 由于您想将日期显示为MMM dd, yyyy,您可以使用Intl.DateTimeFormat“长”日期样式在组件方法中,并从模​​板中调用它:

    methods: {
    formatDate(date) {
    return new Intl.DateTimeFormat('en-US', { dateStyle: 'long' }).format(new Date(date))
    }
    }
    <p>Date: {{ formatDate(item.date) }}</p>

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) {
// Date picker uses ISO format (yyyy-mm-dd), which is UTC. The data
// contains locale specific date strings (mm/dd/yyyy), which `Date`
// parses with local time-zone offset instead of UTC. Normalize the
// ISO date so we're comparing local times.
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>

关于javascript - 如何使用vue js过滤日期范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66193339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com