Syn*_*ied 9 html-table vue.js vuejs2 vuetify.js
我一直在努力使用 VueJs 和 Vuetify 数据表创建嵌套数据表。
这是我想要实现的目标:
我想要一个主数据表,带有过滤器、搜索和其他所有内容(我认为这部分是正确的,使用计算属性并在数据表中重用我过滤的数据),以及嵌套在数据表下的其他一些数据表主要的。
为此,我使用了 Vuetify 数据表的“扩展”功能,并在此扩展范围下创建了子表。
虽然我已经能够实现嵌套部分,至少从视觉上来说,我似乎无法解决一个问题:我想在父行(即父数据的一行)上添加一个复选框表)并能够选择其展开槽中的所有内容。它当然“有效”,因为它有效地选择了绑定到该行的数据,但它不会将扩展范围下的数据标记为已选择。
而发生的情况是选择了行,而不是扩展范围下的子元素。
总结:我想要一个复选框,它的作用类似于子数据表标题上的“全选”复选框。请记住,我需要有 3 个嵌套数据表(这里只有两个),其中父元素始终选择所有子元素,并且可以随意取消选择或重新选择子元素。
我做了一支笔让你们明白我在做什么,也许你们可以帮忙?( https://codepen.io/Synkied/pen/qGLNBv?editors=1010 )
这是代码:
<div id="app">
  <v-app id="inspire">
    <v-data-table
      v-model="selected"
      :headers="headers"
      :items="treats"
      :pagination.sync="pagination"
      expand="true"
      select-all
      item-key="category"
      class="elevation-1"
      hide-actions
>
      
      <template v-slot:items="props">
        <tr @click="props.expanded = !props.expanded">
          <td>
            <!-- This one should select every items under the expand slot -->
            <v-checkbox
                @click.stop="props.selected = !props.selected"
                :input-value="props.selected"
                primary
                hide-details
             ></v-checkbox>
          </td>
          <td class="text-xs-right">{{ props.item.category }}</td>
        </tr>
      </template>
      <template v-slot:expand="props">
        <v-data-table
                      v-model="selected"
                      :headers="headers"
                      :items="props.item.food"
                      :pagination.sync="pagination"
                      expand="true"
                      select-all
                      item-key="category"
                      class="elevation-1"
                      hide-actions
                      >
          <!-- I should be able to hide headers and have the parent row checkbox act like the select all headers' checkbox -->
              <template v-slot:items="props">
                <tr @click="props.expanded = !props.expanded">
                  <td>
                    <v-checkbox
                      :input-value="props.selected"
                      primary
                      hide-details
                    ></v-checkbox>
                  </td>
                  <td>{{ props.item.name }}</td>
                  <td class="text-xs-right">{{ props.item.calories }}</td>
                  <td class="text-xs-right">{{ props.item.fat }}</td>
                  <td class="text-xs-right">{{ props.item.carbs }}</td>
                  <td class="text-xs-right">{{ props.item.protein }}</td>
                  <td class="text-xs-right">{{ props.item.iron }}</td>
                </tr>
              </template>
          </v-data-table>
      </template>
    </v-data-table>
    {{ selected }}
  </v-app>
</div>
new Vue({
  el: '#app',
  data: () => ({
    pagination: {
      sortBy: 'name'
    },
    selected: [],
    headers: [
      {
        text: 'Dessert (100g serving)',
        align: 'left',
        value: 'name'
      },
      { text: 'Calories', value: 'calories' },
      { text: 'Fat (g)', value: 'fat' },
      { text: 'Carbs (g)', value: 'carbs' },
      { text: 'Protein (g)', value: 'protein' },
      { text: 'Iron (%)', value: 'iron' }
    ],
    treats: [
      {
        category: 'Desserts',
        food: [
          {
            name: 'Frozen Yogurt',
            calories: 159,
            fat: 6.0,
            carbs: 24,
            protein: 4.0,
            iron: '1%'
          },
          {
            name: 'Ice cream sandwich',
            calories: 237,
            fat: 9.0,
            carbs: 37,
            protein: 4.3,
            iron: '1%'
          },
          {
            name: 'Cupcake',
            calories: 305,
            fat: 3.7,
            carbs: 67,
            protein: 4.3,
            iron: '8%'
          }
        ]
      },
      {
        category: 'Entries',
        food: [
          {
            name: 'Melon',
            calories: 159,
            fat: 6.0,
            carbs: 24,
            protein: 4.0,
            iron: '1%'
          },
          {
            name: 'Hummus',
            calories: 237,
            fat: 9.0,
            carbs: 37,
            protein: 4.3,
            iron: '1%'
          }
        ]
      }
    ]
  })
})
非常感谢,很棒的社区!
小智 0
<div id="app">
  <v-app id="inspire">
    <v-data-table
      :headers="dessertHeaders"
      :items="desserts"
      :single-expand="singleExpand"
      :expanded.sync="expanded"
      item-key="name"
      show-expand
      class="elevation-1"
    >
      <template v-slot:top>
        <v-toolbar flat>
          <v-toolbar-title>Expandable Table</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-switch
            v-model="singleExpand"
            label="Single expand"
            class="mt-2"
          ></v-switch>
        </v-toolbar>
      </template>
      <template v-slot:item="{ item, expand, isExpanded }">
        <tr>
          <td
            class="d-block d-sm-table-cell"
            v-for="field in Object.keys(item)"
          >
            {{item[field]}}
          </td>
          <td>
            <v-btn icon @click="expand(!isExpanded)">
              <v-icon
                >{{ isExpanded ? 'mdi-chevron-up' : 'mdi-chevron-down'
                }}</v-icon
              >
            </v-btn>
          </td>
        </tr>
      </template>
      <template v-slot:expanded-item="{ headers, item }">
        <tr
          v-for="(item,index) in desserts"
          :key="index"
          :colspan="headers.length"
        >
          <td v-for="(i,index) in Object.values(item)">{{i}}</td>
        </tr>
      </template>
    </v-data-table>
  </v-app>
</div>
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      expanded: [],
      singleExpand: false,
      dessertHeaders: [
        {
          text: 'Dessert (100g serving)',
          align: 'start',
          sortable: false,
          value: 'name',
        },
        { text: 'Calories', value: 'calories' },
        { text: 'Fat (g)', value: 'fat' },
        { text: 'Carbs (g)', value: 'carbs' },
        { text: 'Protein (g)', value: 'protein' },
        { text: 'Iron (%)', value: 'iron' },
        { text: '', value: 'data-table-expand' },
      ],
      desserts: [
        {
          name: 'Frozen Yogurt',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%',
        },
        {
          name: 'Ice cream sandwich',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: '1%',
        },
        {
          name: 'Eclair',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: '7%',
        },
        {
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: '8%',
        },
        {
          name: 'Gingerbread',
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: '16%',
        },
        {
          name: 'Jelly bean',
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: '0%',
        },
        {
          name: 'Lollipop',
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: '2%',
        },
        {
          name: 'Honeycomb',
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: '45%',
        },
        {
          name: 'Donut',
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: '22%',
        },
        {
          name: 'KitKat',
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: '6%',
        },
      ],
    }
  },
})