英文:
Dynamically filter data and display them in Vue.js
问题
我从API收到一些数据,我想要在网站上显示它们,按日期划分。
我想要实现的效果:
15 Feb Wed
对象1:数据X,数据Y
对象2:数据X,数据Y
16 Feb Thu
对象3:数据X,数据Y
对象4:数据X,数据Y
等等。
如果新对象出现在列表中,它会自动插入到正确的位置吗?
events.data是一个对象数组。每个对象都是一个事件,结构如下:
_id: "63e1330539560eaef2612f84"
event_date: "2023-02-16T14:00:00.000Z"
event_link: "/testLink"
event_name: "TestName"
price: 12
我的尝试:
<div v-for="date in datesList" :key="date.id">
<div>{{ date }}</div>
<div class="dateSeparator"></div>
<div v-for="event in events.data" v-bind:key="event._id">
<div v-if="event.event_date == date">
...数据X,数据Y...
至于脚本:
var datesList = []
events.data.forEach(event => {
if (!datesList.includes(event.event_date)) {
datesList.push(event.event_date)
}
})
问题:
为每个事件的每个日期创建了一个空的div。
问题:
如何才能实现期望的结果?
如何在日期的基础上预过滤事件,以消除v-if?
我制定的解决方案:
一个按日期筛选数据的函数。
const filterByDate = (date) => {
let filteredEvents = []
events.data.forEach(event => {
if (event.event_date == date) {
filteredEvents.push(event)
}
})
return filteredEvents
}
更改v-for:
<div v-for="event in filterByDate(date)" v-bind:key="event._id">
英文:
I receive some data from an api and I want to display them on a website, divided by date.
What I want to achieve :
15 Feb Wed
Object 1 : data X, data Y
Object 2 : data X, data Y
16 Feb Thu
Object 3 : data X, data Y
Object 4 : data X, data Y
etc.
An if a new object appears in the list, it is automatically inserted to the right place?
events.data is an array of objects. Each object is an event structured like so :
_id: "63e1330539560eaef2612f84"
event_date: "2023-02-16T14:00:00.000Z"
event_link: "/testLink"
event_name: "TestName"
price: 12
What I tried :
<div v-for="date in datesList" :key="date.id">
<div>{{ date }}</div>
<div class="dateSeparator"></div>
<div v-for="event in events.data" v-bind:key="event._id">
<div v-if="event.event_date == date">
...data X, data Y...
As for the script :
var datesList = []
events.data.forEach(event => {
if (!datesList.includes(event.event_date)) {
datesList.push(event.event_date)
}
})
Problem :
An empty div is created for each event for each date.
Question :
What would be the best way to achieve the desired result ?
How can I pre-filter the events based on their date in order to get rid of the v-if ?
Solution I made :
A function that filters my data by the date needed.
const filterByDate = (date) => {
let filteredEvents = []
events.data.forEach(event => {
if (event.event_date == date) {
filteredEvents.push(event)
}
})
return filteredEvents
}
Changed the v-for :
<div v-for="event in filterByDate(date)" v-bind:key="event._id">
答案1
得分: 1
你的代码看起来没问题。根据你分享的数据,我没有看到任何问题。我只是在下面添加了一个实时演示(以Vue 2格式展示行为),你可以根据你的需求将其更改为Vue 3。
实时演示:
<div id="app">
<div v-for="(date, index) in datesList" :key="index" class="date-container">
<div>{{ date }}</div>
<hr>
<div v-for="event in events.data" v-bind:key="event._id">
<div v-if="event.event_date === date">
{{ event.event_name }}
</div>
</div>
</div>
</div>
这部分代码没有翻译,因为它包含HTML和Vue.js代码。如果你需要其他方面的帮助,请告诉我。
英文:
Your code looks fine to me. I did not see any issue in the implementation you have as per the data you shared. I just added a live demo below (In Vue 2 format just to show the behavior), You can change it to Vue 3 as per your requirement.
Live Demo :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
new Vue({
el: '#app',
data: {
events: {
data: [{
_id: "63e1330539560eaef2612f84",
event_date: "2023-02-16",
event_link: "/testLink",
event_name: "TestName 1",
price: 12
}, {
_id: "63e1330539560eaef2612f85",
event_date: "2023-02-17",
event_link: "/testLink",
event_name: "TestName 2",
price: 12
}, {
_id: "63e1330539560eaef2612f86",
event_date: "2023-02-16",
event_link: "/testLink",
event_name: "TestName 3",
price: 12
}, {
_id: "63e1330539560eaef2612f87",
event_date: "2023-02-17",
event_link: "/testLink",
event_name: "TestName 4",
price: 12
}]
},
datesList: []
},
mounted() {
this.events.data.forEach(event => {
if (!this.datesList.includes(event.event_date)) {
this.datesList.push(event.event_date)
}
})
}
})
<!-- language: lang-css -->
.date-container {
border: 1px solid black;
margin: 10px;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div v-for="(date, index) in datesList" :key="index" class="date-container">
<div>{{ date }}</div>
<hr>
<div v-for="event in events.data" v-bind:key="event._id">
<div v-if="event.event_date === date">
{{ event.event_name }}
</div>
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论