每个v-for循环有多个元素

时间:2017-08-28 20:35:59

标签: javascript vue.js v-for

在我的Vue应用程序中,我正在遍历一系列学校。每所学校都有一个名字,一系列教师计数(每个年级一个),以及一系列学生计数(每个年级一个)。

以下代码有效,但仅限于我正在手动编码<td>

&#13;
&#13;
new Vue({
  el: '#app',
  data: {
    schools: [
      { name: 'Lincoln',   teachers: [3, 4, 1], students: [55, 42, 39] },
      { name: 'Oak Grove', teachers: [1, 2, 1], students: [31, 36, 23] },
      { name: 'Fairview',  teachers: [1, 3, 2], students: [30, 26, 39] },
    ],
  },
});
&#13;
thead th,
tbody td { text-align: center; }
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>

<table id="app" class="table">
  <thead>
    <tr>
      <th rowspan="2"></th>
      <th colspan="2">Grade K</th>
      <th colspan="2">Grade 1</th>
      <th colspan="2">Grade 2</th>
    </tr>
    <tr>
      <th>Teachers</th>
      <th>Students</th>
      <th>Teachers</th>
      <th>Students</th>
      <th>Teachers</th>
      <th>Students</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="school in schools">
      <th>{{ school.name }}</th>
      
      <td>{{ school.teachers[0] }}</td>
      <td>{{ school.students[0] }}</td>
      
      <td>{{ school.teachers[1] }}</td>
      <td>{{ school.students[1] }}</td>
      
      <td>{{ school.teachers[2] }}</td>
      <td>{{ school.students[2] }}</td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

注意重复的行:

<td>{{ school.teachers[x] }}</td>
<td>{{ school.students[x] }}</td>

这个简化的例子并没有太大的问题。但在我的真实项目中,还有更多列和子列。 有没有办法让重复循环显示<td>

我尝试了另一个v-for循环,但由于它位于<tr>内,因此只允许<td><th>

1 个答案:

答案 0 :(得分:5)

假设教师和学生阵列始终的长度相同,则可以迭代template标签。

<tr v-for="school in schools">
  <th>{{ school.name }}</th>
  <template v-for="(cnt, idx) in school.teachers.length">
    <td>{{ school.teachers[idx] }}</td>
    <td>{{ school.students[idx] }}</td>
  </template>
</tr>

实施例。

&#13;
&#13;
new Vue({
  el: '#app',
  data: {
    schools: [
      { name: 'Lincoln',   teachers: [2, 2, 2], students: [40, 40, 40] },
      { name: 'Oak Grove', teachers: [2, 2, 2], students: [40, 40, 40] },
      { name: 'Fairview',  teachers: [2, 2, 2], students: [40, 40, 40] },
    ],
  },
});
&#13;
thead th,
tbody td { text-align: center; }
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>

<table id="app" class="table">
  <thead>
    <tr>
      <th rowspan="2"></th>
      <th colspan="2">Grade K</th>
      <th colspan="2">Grade 1</th>
      <th colspan="2">Grade 2</th>
    </tr>
    <tr>
      <th>Teachers</th>
      <th>Students</th>
      <th>Teachers</th>
      <th>Students</th>
      <th>Teachers</th>
      <th>Students</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="school in schools">
      <th>{{ school.name }}</th>
      <template v-for="(cnt, idx) in school.teachers.length">
        <td>{{ school.teachers[idx] }}</td>
        <td>{{ school.students[idx] }}</td>
      </template>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

另一种方法是将重复的元素抽象为一个组件并使用special is attribute<td is="details" v-for="..."></td>)。