【问题标题】:How to get vuetify data-table selected itemsPerPage in 2.0?如何在 2.0 中获取 vuetify 数据表选择的 itemsPerPage?
【发布时间】:2020-02-26 09:17:05
【问题描述】:

我正在尝试在 Vuetify 数据表上获取每页选定的项目。似乎已经进行了一些重大更改。我按照这个例子: How to set initial 'rows per page' value in Vuetify DataTable component?

什么用途

<v-data-table
        :headers="headers"
        :items="equipment"
        class="elevation-1"
        :rows-per-page-items="[15, 30, 50, 100]"
        :pagination.sync="pagination">

data() {
return {
    pagination: {
      rowsPerPage: 30
    }, ...
  }
}

获取当前选中的rowsPerPage。这会打印如下错误: [BREAKING] 'pagination' has been removed, use 'options' instead. For more information, see the upgrade guide https://github.com/vuetifyjs/vuetify/releases/tag/v2.0.0#user-content-upgrade-guide

我浏览了升级指南,几乎没有关于页脚控制分页的方式,或者现在如何同步每页选定的行。我尝试使用options 并在此处查看表格的代码:

https://github.com/vuetifyjs/vuetify/blob/master/packages/vuetify/src/components/VDataTable/VDataTable.ts#L151

但是,目前还不清楚如何检索选定的 itemsPerPage,并且在设置 itemsPerPage 时,options 似乎不能用作反应性道具。如果有人知道当前执行相当于 pagination.sync 的方法,我们将不胜感激。

【问题讨论】:

    标签: javascript vue.js vuejs2 vuetify.js


    【解决方案1】:

    您可以使用 vuetify 2.x

    设置每页的项目和每页的行数

    这是方法,在数据表组件中使用以下属性

    :items-per-page="5" 
    

    您可以将每页的项目直接设置为数字或分配给数据反应性属性和 动态更新

    也是每页的行数,将此属性添加到数据表中

    :footer-props="footerProps"

    在脚本中

    data(){
      return {
        footerProps: {'items-per-page-options': [15, 30, 50, 100]},
      }
    }
    

    在这里找到工作的codepen:

    https://codepen.io/chansv/pen/gOOGPdR?editors=1010

    工作代码:

    <div id="app">
      <v-app id="inspire">
        <v-data-table
          :headers="headers"
          :items="desserts"
          :items-per-page="5"
          class="elevation-1"
          :footer-props="footerProps"
          @update:items-per-page="getItemPerPage"
        ></v-data-table>
      </v-app>
    </div>
    
    new Vue({
      el: '#app',
      vuetify: new Vuetify(),
      data () {
        return {
          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: {
        getItemPerPage(val) {
          console.log(val);
        },
      }
    })
    

    【讨论】:

    • 嗨@chans,感谢您的回复。我仍然很清楚,我如何获得当前选择的金额? :itemsPerPage 似乎设置了默认金额,但是如果选择的金额发生变化,我想知道它被更改为什么。
    • 是的,可以更改值,检查最新的 codepen 更改
    • @zavtra,上述答案更改是否解决了您的问题?
    • 嘿,成功了!您可以在答案中发布代码更改吗? (Codepen 不会根据需要保留 sn-ps)
    【解决方案2】:

    简单的方法: :footer-props="{ itemsPerPageOptions: [25,50,100,-1]}" 请注意,-1 用于将“全部”添加到选项

    【讨论】:

      【解决方案3】:

      对于像我一样正在寻找默认显示所有行的新选项的任何人,它现在已经更改(- 在 v2.2.11 中工作):

      老办法

      :pagination.sync="{ rowsPerPage: -1 }"
      

      新方式

      :items-per-page="-1"
      

      不确定项目和行是否可以互换使用,但这让我有一段时间感到困惑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-06
        • 1970-01-01
        • 2021-09-14
        • 2019-04-11
        • 2021-01-26
        • 2020-03-25
        • 2018-05-06
        • 1970-01-01
        相关资源
        最近更新 更多