【问题标题】:How can I reorder the items in a Vuetify data table by dragging the rows?如何通过拖动行重新排序 Vuetify 数据表中的项目?
【发布时间】:2021-01-24 02:33:36
【问题描述】:

我正在为客户开发一个 Vuetify 网络应用程序,她希望能够通过拖放行来调整数据表中显示的元素的顺序,但 Vuetify 文档没有解释如何去做;我该怎么做?

【问题讨论】:

    标签: vue.js vuetify.js


    【解决方案1】:

    这是我使用的 CodePen:https://codepen.io/NathanWailes/pen/rNLajYO

    它使用:

    • Vue 2.x
    • Vuetify 2.3.13
    • Sortable 1.10.2(如果您还没有,则需要安装/导入它)

    它基于this GitHub issue 中的答案。

    代码如下:

    <div id="app">
      <v-app>
        <v-data-table
          :headers="headers"
          :items="desserts"
          v-sortable-data-table
          @sorted="saveOrder"
          item-key="name"
        ></v-data-table>
      </v-app>
    </div>
    
    new Vue({
      el: '#app',
      vuetify: new Vuetify(),
      data () {
        return {
          headers: [
            {
              text: 'Dessert',
              align: 'start',
              sortable: false,
              value: 'name',
            },
          ],
          desserts: [
            {
              name: 'Frozen Yogurt',
            },
            {
              name: 'Ice cream sandwich',
            },
            {
              name: 'Eclair',
            },
            {
              name: 'Cupcake',
            },
            {
              name: 'Gingerbread',
            },
          ],
        }
      },
      methods: {
        saveOrder (event) {
          const movedItem = this.desserts.splice(event.oldIndex, 1)[0];
          this.desserts.splice(event.newIndex, 0, movedItem);
        }
      },
      directives: {
        sortableDataTable: {
          bind (el, binding, vnode) {
            const options = {
              animation: 150,
              onUpdate: function (event) {
                vnode.child.$emit('sorted', event)
              }
            }
            Sortable.create(el.getElementsByTagName('tbody')[0], options)
          }
        }
      },
    })
    

    【讨论】:

    • 谢谢你让我免于使用原生 JS 事件 :) 我没有时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多