动态过滤数据并在 Vue.js 中显示它们

huangapple go评论64阅读模式
英文:

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: &quot;63e1330539560eaef2612f84&quot;​​​​​​
event_date: &quot;2023-02-16T14:00:00.000Z&quot;​​​
event_link: &quot;/testLink&quot;​​​
event_name: &quot;TestName&quot;
price: 12

What I tried :

&lt;div v-for=&quot;date in datesList&quot; :key=&quot;date.id&quot;&gt;
    &lt;div&gt;{{ date }}&lt;/div&gt;
    &lt;div class=&quot;dateSeparator&quot;&gt;&lt;/div&gt;
    &lt;div v-for=&quot;event in events.data&quot; v-bind:key=&quot;event._id&quot;&gt;
        &lt;div v-if=&quot;event.event_date == date&quot;&gt;
            ...data X, data Y...

As for the script :


var datesList = []

events.data.forEach(event =&gt; {
  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) =&gt; {
  let filteredEvents = []
  events.data.forEach(event =&gt; {
    if (event.event_date == date) {
      filteredEvents.push(event)
    }
  })
  return filteredEvents
}

Changed the v-for :

&lt;div v-for=&quot;event in filterByDate(date)&quot; v-bind:key=&quot;event._id&quot;&gt;

答案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: &#39;#app&#39;,
  data: {
    events: {
      data: [{
        _id: &quot;63e1330539560eaef2612f84&quot;,
        event_date: &quot;2023-02-16&quot;,
        event_link: &quot;/testLink&quot;,
        event_name: &quot;TestName 1&quot;,
        price: 12
      }, {
        _id: &quot;63e1330539560eaef2612f85&quot;,
        event_date: &quot;2023-02-17&quot;,
        event_link: &quot;/testLink&quot;,
        event_name: &quot;TestName 2&quot;,
        price: 12
      }, {
        _id: &quot;63e1330539560eaef2612f86&quot;,
        event_date: &quot;2023-02-16&quot;,
        event_link: &quot;/testLink&quot;,
        event_name: &quot;TestName 3&quot;,
        price: 12
      }, {
        _id: &quot;63e1330539560eaef2612f87&quot;,
        event_date: &quot;2023-02-17&quot;,
        event_link: &quot;/testLink&quot;,
        event_name: &quot;TestName 4&quot;,
        price: 12
      }]
    },
    datesList: []
  },
  mounted() {
    this.events.data.forEach(event =&gt; {
      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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;app&quot;&gt;
  &lt;div v-for=&quot;(date, index) in datesList&quot; :key=&quot;index&quot; class=&quot;date-container&quot;&gt;
    &lt;div&gt;{{ date }}&lt;/div&gt;
    &lt;hr&gt;
    &lt;div v-for=&quot;event in events.data&quot; v-bind:key=&quot;event._id&quot;&gt;
      &lt;div v-if=&quot;event.event_date === date&quot;&gt;
        {{ event.event_name }}
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 06:46:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定