You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vuetify DataTable中默认仅展示指定条件的数据(无需搜索)

解决Vuetify DataTable默认仅展示指定条目问题

要实现默认只显示desserts数据集中name为"Cupcake"的条目,无需使用搜索功能,你可以通过计算属性过滤数据源的方式实现,具体修改如下:

修改后的JavaScript代码

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      headers: [
        {
          text: '甜点(100克份)',
          align: 'left',
          value: 'name',
        },
        { text: '卡路里', value: 'calories' },
        { text: '脂肪(克)', value: 'fat' },
        { text: '碳水化合物(克)', value: 'carbs' },
        { text: '蛋白质(克)', value: 'protein' },
        { text: '铁(%)', value: 'iron' },
      ],
      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: 'Cupcake',
          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: 'Cupcake',
          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%',
        }
      ]
    }
  },
  computed: {
    filteredDesserts () {
      // 筛选出name为"Cupcake"的条目
      return this.desserts.filter(item => item.name === 'Cupcake')
    }
  }
})

修改后的HTML代码

<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

<div id="app">
  <v-app id="inspire">
    <v-card>
      <v-card-title>仅展示Cupcake数据</v-card-title>
      <v-data-table
        :headers="headers"
        :items="filteredDesserts"
      ></v-data-table>
    </v-card>
  </v-app>
</div>

关键修改说明

  • 移除了原代码中的search变量和搜索输入框组件,完全去掉搜索功能
  • 添加filteredDesserts计算属性,利用数组filter方法筛选出name等于"Cupcake"的条目
  • 将v-data-table的:items绑定从原数据源desserts改为过滤后的filteredDesserts,实现默认仅展示指定条目

这种方式保留了原始数据源的完整性,后续如果需要扩展其他过滤逻辑,只需修改计算属性即可,灵活性更高。

内容的提问来源于stack exchange,提问作者plasticheart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 21:57:11