简体   繁体   English

如何为未过滤的元素显示文本

[英]How to display a text for non-filtered elements

I am trying to create a calendar like in the below:我正在尝试创建如下所示的日历: 在此处输入图像描述

As you see I have tickets object and according to the date of these tickets, I am displaying them.如您所见,我有票 object,根据这些票的日期,我正在展示它们。 What I need to do, as you see in the picture I want to display a text 'No available ticket' when it is empty.我需要做什么,正如您在图片中看到的那样,我想在它为空时显示文本“No available ticket”。 For example, for Monday and Tuesday (actually all days except Wednesday).例如,对于星期一和星期二(实际上是除星期三之外的所有天)。 So to take the ticket, I am filtering tickets:所以要拿票,我正在过滤票:

findTickets(tickets, date) {
            return tickets.filter((ticket) => {
                return (
                    ticket.date.getDate() === date.getDate() &&
                    ticket.date.getMonth() === date.getMonth() &&
                    ticket.date.getFullYear() === date.getFullYear() &&
                    this.getWeekNumber(ticket.date) === this.currentWeekNumber
                );
            });
        },

And basically display inside the table with v-for:基本上用 v-for 显示在表格内:

<tr class="list-content" v-for="ticket in findTickets(tickets, date)">
                <td>{{ ticket.time }}</td>
                <td>{{ ticket.date }}</td>
                <td>{{ ticket.name }}</td>
            </tr>

So do you know how can I write 'no available date' when the ticket is empty?那么你知道当票是空的时候我怎么写'no available date'吗?

You can try with map instead of filter, something like this:您可以尝试使用 map 而不是过滤器,如下所示:

return tickets.map((ticket) => {
  if (ticket.date.getDate() === date.getDate() &&
      ticket.date.getMonth() === date.getMonth() &&
      ticket.date.getFullYear() === date.getFullYear() &&
      this.getWeekNumber(ticket.date) === this.currentWeekNumber) return ticket;
  ticket = {time: '', date: 'No available ticket', name: ''}
  return ticket
});

Maybe better way is to create computed property...也许更好的方法是创建计算属性......

Instead of calling findTickets() directly in the HTML you could call findTickets() in the Javascript code and use a list directly in the v-for loop.您可以在 Javascript 代码中调用findTickets() findTickets()并直接在v-for循环中使用列表,而不是直接在 HTML 中调用 findTickets()。 You can use v-if to check if the tickets array is empty, if so, print a message.您可以使用v-if检查tickets数组是否为空,如果是,则打印一条消息。

<tr v-for="ticket in tickets" v-bind:key="ticket.name">
        <td>{{ ticket.time }}</td>
        <td>{{ ticket.date }}</td>
        <td>{{ ticket.name }}</td>
</tr>
<div v-if="tickets.length == 0">No available tickets</div>

This is the data:这是数据:

 data: function () {
    return {
      tickets: [],
    };
  },

For this example the findTickets() method should return an empty array if there's no tickets.对于这个例子,如果没有票, findTickets()方法应该返回一个空数组。

methods: {
    findTickets() {
      // Here you do your stuff.
      // Let's say you found your tickets
      var ticketsFound = true;
      if (ticketsFound) {
        this.tickets = [
          {
            time: "11:02",
            date: "23/85/2009",
            name: "Ticket 1",
          },
          {
            time: "11:02",
            date: "23/85/2009",
            name: "Ticket 2",
          },
          {
            time: "11:02",
            date: "23/85/2009",
            name: "Ticket 3",
          },
          {
            time: "11:02",
            date: "23/85/2009",
            name: "Ticket 4",
          },
        ];
      } else {
        this.tickets = [];
      }
    },
  }

And then you only need to call the findTickets() method from wherever you like.然后你只需要从任何你喜欢的地方调用findTickets()方法。 In my example I call it when the component is mounted .在我的示例中,我在安装组件时调用它。

 mounted() {
    this.findTickets();
  },

Here's a working example: https://codesandbox.io/s/patient-wave-u8uk7这是一个工作示例: https://codesandbox.io/s/patient-wave-u8uk7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM