【问题标题】:How can i use in array in the vuetify?我如何在 vuetify 的数组中使用?
【发布时间】:2020-02-10 17:31:15
【问题描述】:

以下代码的数组项未呈现在正确的列中:

<div id="app">
  <v-app>
    <v-simple-table>
      <template v-slot:default>
        <thead>
          <tr>
            <th class="text-left">Mon </th>
            <th class="text-left">Tue</th>
            <th class="text-left">Wed </th>
            <th class="text-left">Thu</th>
            <th class="text-left">Fri </th>
            <th class="text-left">Sat </th>
            <th class="text-left">Sun</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <template v-for="item in schedules">
              <td v-if="days.includes(item.day)" class="text-left">{{item.from_time}}-{{item.to_time}}</td>
              <td v-else class="text-left">-</td>
            </template>
          </tr>
        </tbody>
      </template>
    </v-simple-table>
  </v-app>
</div>

<script>
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    schedules: [
      { day: 1, from_time: '15:00', to_time: '13:00' },
      { day: 6, from_time: '16:00', to_time: '16:30' },
    ],
    days: [1,2,3,4,5,6,7]
  })
})
</script>

Codepen

schedules(第 6 天)中的第二项应放在 Sat 列,而不是 Tue 列。

注意:

  • 如果计划的索引日期 = 1,则时间放在列 Mon
  • 如果计划的索引日期 = 2,则将时间放在 星期二 列中
  • 如果日程表的索引日期 = 3,则时间放在列星期三

等等……

如何按日期索引在工作日列中呈现schedules 项目?也许解决方案是将schedules 和days 合并为一个数据对象。

【问题讨论】:

    标签: arrays vue.js vuejs2 vue-component vuetify.js


    【解决方案1】:

    您只是在模板中迭代 schedules 数组,因此只在表格正文中呈现了两个单元格。

    我更新了模板和脚本代码以获得预期的输出。

    模板代码:

    <div id="app">
      <v-app>
        <v-simple-table>
         <template v-slot:default>
           <thead>
             <tr>
               <th class="text-left">Mon </th>
               <th class="text-left">Tue</th>
               <th class="text-left">Wed </th>
               <th class="text-left">Thu</th>
               <th class="text-left">Fri </th>
               <th class="text-left">Sat </th>
               <th class="text-left">Sun</th>
             </tr>
           </thead>
           <tbody>
             <tr>
    
               <template v-for="dayEntry in days">
                      <td v-if="getScheduleByDay(dayEntry)" class="text-left">{{getScheduleByDay(dayEntry).from_time}}-{{getScheduleByDay(dayEntry).to_time}}</td>
                      <td v-else class="text-left">-</td> 
               </template>
    
            </tr>
          </tbody>
        </template>
      </v-simple-table>
      </v-app>
    </div>
    

    脚本代码:

    
      new Vue({
        el: '#app',
        vuetify: new Vuetify(),
        data: () => ({
          schedules: [
            { day: 1, from_time: '15:00', to_time: '13:00' },
            { day: 6, from_time: '16:00', to_time: '16:30' },
          ],
          days: [1,2,3,4,5,6,7]
        }),
        methods: {
          getScheduleByDay(dayEntry) {
            console.log(dayEntry)
            const relevantDay = this.schedules.filter(entry => entry.day === dayEntry);
            if(relevantDay.length > 0)
              return relevantDay[0];
            return null;
          }
        }
      })
    

    另见更新的代码笔: https://codepen.io/simonthiel2/pen/Rwwrzav

    【讨论】:

    • 谢谢。但还有其他方法吗?例如,将计划和日期组合成 1 个数据。之后显示组合数据。但合并的数据正在使用脚本
    【解决方案2】:

    在您的原始代码中:

    <template v-for="item in schedules">
      <td v-if="days.includes(item.day)" class="text-left">{{item.from_time}}-{{item.to_time}}</td>
      <td v-else class="text-left">-</td>
    </template>
    

    有几个问题:

    1. 您只迭代 schedules,这可能少于一周中的天数(如您的示例中的情况)。
    2. v-if 检查days 是否包含项目的日期编号,这很可能是真的(假设您的数据始终符合域),因此这有效地使表格按顺序呈现所有schedules 项目。

    一种解决方案是在计算属性中使用days 上的Array.prototype.map(即schedules[].day 映射到的日期ID 数组):

    export default {
      computed: {
        weekly() {
          return this.days.map(day => this.schedules.find(s => s.day == day))
        }
      }
    }
    

    ...并更新您的模板以使用该道具:

    <template v-for="item in weekly">
      <td v-if="item" class="text-left">{{item.from_time}}-{{item.to_time}}</td>
      <td v-else class="text-left">-</td>
    </template>
    

    updated codepen

    【讨论】:

      【解决方案3】:

      如果你希望将这两个数据结合起来,你可以这样做,它完全可以工作

      new Vue({
          el: '#app',
          data: () => ({
            schedules: [
              { day: 1, from_time: '15:00', to_time: '13:00' },
              { day: 6, from_time: '16:00', to_time: '16:30' },
            ],
            days: [1,2,3,4,5,6,7],
            weekday:[]
          }),
          created(){
            for(let i = 0; i < this.days.length;i++){
              var obj = null
              for(let j = 0; j < this.schedules.length; j++){
                if(this.days[i] === this.schedules[j].day){
                  obj = this.schedules[j]
                  break
                }
              }
              this.weekday.push(obj)
            }
          },
          methods: {
            
          }
        })
      <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
      <div id="app">
        <v-app>
          <v-simple-table>
           <template v-slot:default>
             <thead>
               <tr>
                 <th class="text-left">Mon </th>
                 <th class="text-left">Tue</th>
                 <th class="text-left">Wed </th>
                 <th class="text-left">Thu</th>
                 <th class="text-left">Fri </th>
                 <th class="text-left">Sat </th>
                 <th class="text-left">Sun</th>
               </tr>
             </thead>
             <tbody>
               <tr>
      
                 <template v-for="day in weekday">
                        <td v-if="day" class="text-left">{{day.from_time}}-{{day.to_time}}</td>
                        <td v-else class="text-left">-</td> 
                 </template>
      
              </tr>
            </tbody>
          </template>
        </v-simple-table>
        </v-app>
      </div>

      【讨论】:

        猜你喜欢
        • 2017-11-25
        • 2021-11-01
        • 1970-01-01
        • 2020-01-26
        • 1970-01-01
        • 2020-08-28
        • 1970-01-01
        • 2019-12-15
        • 2019-01-03
        相关资源
        最近更新 更多