简体   繁体   English

Vuetify 数据表:如何自动设置每页的行数(取决于组件填充的垂直空间)?

[英]Vuetify Data table: how to have an automatic number of rows per page (depending on the vertical space filled by the component)?

I am using Vuetify and its amazing datatable but I can't find a way for the datable to adjust automatically the number of rows per page (that is, when the pagination is enabled) depending on the vertical space filled by the component?我正在使用 Vuetify 及其惊人的数据表,但我无法找到一种方法让数据表根据组件填充的垂直空间自动调整每页的行数(即启用分页时)?

So that when resizing the browser, the datatable would automatically change the number of rows per page.这样在调整浏览器大小时,数据表会自动更改每页的行数。

Is there a way or a workaround to achieve that?有没有办法或解决方法来实现这一目标?

Yes it is possible to set the number of rows per page automatically when we resize the page是的,当我们调整页面大小时,可以自动设置每页的行数

Here is the working code: https://codepen.io/chansv/full/PooOpJz这是工作代码: https://codepen.io/chansv/full/PooOpJz

Open developers console and try resizing the window size, Its just a prototype or an approach to make it work, you can add your own logic to how to behave on resize打开开发人员控制台并尝试调整 window 大小,它只是一个原型或使其工作的方法,您可以添加自己的逻辑来调整大小

https://codepen.io/chansv/pen/PooOpJz https://codepen.io/chansv/pen/PooOpJz

<div id="app">
  <v-app id="inspire">
    <v-data-table
      id="dataTable"
      :headers="headers"
      :items="desserts"
      :items-per-page="itemsPerPage"
      :footer-props="footerProps"
      class="elevation-1"
    ></v-data-table>
  </v-app>
</div>



new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      itemsPerPage: 0,
      optionsLength: 6,
      footerProps: {'items-per-page-options': [5, 10,15, 30, 50, 100]},
      headers: [
        {
          text: 'Dessert (100g serving)',
          align: 'left',
          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' },
      ],
      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%',
        },
      ],
    }
  },
  methods: {
    updateTable() {
      var tableHeight = document.getElementById('dataTable').offsetHeight;
      this.itemsPerPage = parseInt(tableHeight/40);
      if (this.desserts.length < this.itemsPerPage) this.itemsPerPage = this.desserts.length;
      if (!this.footerProps['items-per-page-options'].includes(this.itemsPerPage)) {
        if (this.footerProps['items-per-page-options'].length == this.optionsLength) {
          this.footerProps['items-per-page-options'].unshift(this.itemsPerPage);
        } else {
          this.footerProps['items-per-page-options'].shift();
          this.footerProps['items-per-page-options'].unshift(this.itemsPerPage);
        }
      }
    },
  },
  created() {
    this.updateTable();

    var self = this;
    window.onresize = function(event) {
      self.updateTable();
    };
  },
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在 v-data-table (vuetify) 中更改每页行数而不会将表重置到第 1 页 - Is there a way to change Rows Per Page in a v-data-table (vuetify) without the table resetting to page 1 Vuetify 数据表 - 隐藏每页项目时,上一页/下一页显示在左侧 - Vuetify data table - previous/next page is showing on the left side when items-per-page is hidden Vuetify:如何从数据表组件中删除此特定道具内容? - Vuetify: How to remove this specific prop content from the data table component? 如何合并Vuetify数据表和Vuetify列表? - How to combine a Vuetify data table and Vuetify list? 对于大量对象,Vuetify 数据表很慢 - Vuetify data table slow for large number of objects 如何每页多次显示/隐藏表行? - How to do show/hide of table rows multiple times per page? 如何使用新数据和新列更新 vuetify 数据表组件 - How do I update vuetify data table component with new data and new columns 如何在 Vuetify 中扩展 v-data-table 组件中的插槽? - How can I expand slots in v-data-table component in Vuetify? 如何访问插槽 object 属性以传递给 Vuetify 中给定数据表行的组件? - How to access slot object property to pass into component for a given data table row in Vuetify? 如何在Vuetify(版本&gt; = 2.0)中单击时突出显示v-data-table中的行? - How to highlight rows in v-data-table on click in Vuetify (version >= 2.0)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM