Vuetify Datatable:隐藏下拉菜单,以便在 mobileBreakpoint 下方进行分组排序

Vuetify Datatable: Hide dropdown menu for sorting below mobileBreakpoint with grouping

考虑以下基于https://vuetifyjs.com/en/components/data-tables/#grouping

的代码

// Disable annoying tips:
Vue.config.devtools = false
Vue.config.productionTip = false

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      search: '',
      headers: [
        {
          text: 'Dessert (100g serving)',
          align: 'start',
          value: 'name',
          groupable: false,
        },
        { text: 'Category', value: 'category', align: 'right' },
        { text: 'Dairy', value: 'dairy', align: 'right' },
        { text: 'Calories', value: 'dairy', align: 'right' },
        { text: 'Fat (g)', value: 'calories' },
        { text: 'Carbs (g)', value: 'carbs' },
        { text: 'Protein (g)', value: 'protein' },
        { text: 'Iron (%)', value: 'iron' },
      ],
      desserts: [
        {
          name: 'Frozen Yogurt',
          category: 'Ice cream',
          dairy: 'Yes',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%',
        },
        {
          name: 'Eclair',
          category: 'Cookie',
          dairy: 'Yes',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: '7%', 
        },
        {
          name: 'Cupcake',
          category: 'Pastry',
          dairy: 'Yes',
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: '8%',
        },
        {
          name: 'Gingerbread',
          category: 'Cookie',
          dairy: 'No',
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: '16%',
        },
        {
          name: 'Jelly bean',
          category: 'Candy',
          dairy: 'No',
          name: 'Jelly bean',
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: '0%',
        },
        {
          name: 'Lollipop',
          category: 'Candy',
          dairy: 'No',
          name: 'Lollipop',
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: '2%',
        },
        {
          name: 'Honeycomb',
          category: 'Toffee',
          dairy: 'No',
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: '45%',
        },
        {
          name: 'Donut',
          category: 'Pastry',
          dairy: 'Yes',
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: '22%',
        },
        {
          name: 'KitKat',
          category: 'Candy',
          dairy: 'Yes',
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: '6%',
        },
        {
          name: 'Ice cream sandwich',
          category: 'Ice cream',
          dairy: 'Yes',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: '1%',
        },
      ],
    }
  },
})
<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@4.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.4.8/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">

<div id="app">
  <v-app id="inspire">
    <v-card>
    <v-card-title>
      <v-text-field
        v-model="search"
        append-icon="mdi-magnify"
        label="Search"
        single-line
        hide-details
        ></v-text-field>
    </v-card-title>
    <v-data-table
      :headers="headers"
      :items="desserts"
      item-key="name"
      :search="search"
      sort-by="name"
      group-by="category"
      class="elevation-1"
      show-group-by
      mobile-breakpoint=100000
      :header-props="{'disable-sort': true}"
    ></v-data-table>
      </v-card>
  </v-app>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.4.8/dist/vuetify.js"></script>

单击 Run code snippet 并单击 Full page 以更好地查看。

如果您更喜欢 codepen,请单击 here

我已经执行了 <v-data-table> component of vuetify by setting mobile-breakpoint to 100000. The default is 600 which corresponds to the breakpoint xs 的 mobile/responsive 版本。

我想隐藏用于移动视图排序的下拉菜单,但为了分组功能和 Desktop/Tablet 查看器保持排序处于活动状态。

我已添加

:disable-sort="$vuetify.breakpoint.xsOnly"
// or here simply since we consider only the mobile version:
disable-sort

但是,这会破坏分组。

其他尝试是

  1. :hide-default-header="$vuetify.breakpoint.xsOnly" 这删除了我不想要的条目的描述。

  2. :header-props="{'disable-sort': true}" 什么都不做

Vuetify 版本:2.4.8 和 Vue 2.6.12。

更新 说明列 headers 应该保留,但只应删除响应视图中用于排序的下拉列表:

你已经做了很好的尝试,但这里的行为有点不同

手机断点范围在table内,不会改变this.$vuetify.breakpoint中的设备类型,所以当你检查this.$vuetify.breakpoint.xsOnly 只有当屏幕宽度小于 600 而不是 10000 时它才会 return true 即使你想确认相同,你可以把 this.$vuetify.breakpoint.name 放在 UI 的 span 标签内,你会得到 xs 仅当屏幕宽度小于600

If you want to hide the sort_by if the device width is less than 10000, it is possible to manage by css

请在下面找到代码

<div id="app">
  <v-app id="inspire">
    <v-card>
    <v-card-title>
      <v-text-field
        v-model="search"
        append-icon="mdi-magnify"
        label="Search"
        single-line
        hide-details
        ></v-text-field>
    </v-card-title>
    <v-data-table
      :headers="headers"
      :items="desserts"
      item-key="name"
      :search="search"
      sort-by="name"
      group-by="category"
      class="elevation-1"
      show-group-by
    ></v-data-table>
      </v-card>
  </v-app>
</div>

@media only screen and (max-width: 600px) {
  .v-data-table-header.v-data-table-header-mobile {
   display:none; 
  }
}

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      search: '',
      headers: [
        {
          text: 'Dessert (100g serving)',
          align: 'start',
          value: 'name',
          groupable: false,
        },
        { text: 'Category', value: 'category', align: 'right' },
        { text: 'Dairy', value: 'dairy', align: 'right' },
        { text: 'Calories', value: 'dairy', align: 'right' },
        { text: 'Fat (g)', value: 'calories' },
        { text: 'Carbs (g)', value: 'carbs' },
        { text: 'Protein (g)', value: 'protein' },
        { text: 'Iron (%)', value: 'iron' },
      ],
      desserts: [
        {
          name: 'Frozen Yogurt',
          category: 'Ice cream',
          dairy: 'Yes',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%',
        },
        {
          name: 'Eclair',
          category: 'Cookie',
          dairy: 'Yes',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: '7%', 
        },
        {
          name: 'Cupcake',
          category: 'Pastry',
          dairy: 'Yes',
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: '8%',
        },
        {
          name: 'Gingerbread',
          category: 'Cookie',
          dairy: 'No',
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: '16%',
        },
        {
          name: 'Jelly bean',
          category: 'Candy',
          dairy: 'No',
          name: 'Jelly bean',
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: '0%',
        },
        {
          name: 'Lollipop',
          category: 'Candy',
          dairy: 'No',
          name: 'Lollipop',
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: '2%',
        },
        {
          name: 'Honeycomb',
          category: 'Toffee',
          dairy: 'No',
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: '45%',
        },
        {
          name: 'Donut',
          category: 'Pastry',
          dairy: 'Yes',
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: '22%',
        },
        {
          name: 'KitKat',
          category: 'Candy',
          dairy: 'Yes',
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: '6%',
        },
        {
          name: 'Ice cream sandwich',
          category: 'Ice cream',
          dairy: 'Yes',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: '1%',
        },
      ],
    }
  },
})

Please find the working codepen here: https://codepen.io/chansv/pen/WNRxWwz?editors=1010