sortablejs 拖拽排序部分情况动态禁用拖拽

创建 sortablejs 拖拽排序

<template>
  <div ref="sortablejsRefs" class="sortablejs-list">
    <span v-for="(item, index) in dataList" :key="item.id">
      {{ item.title }}
    </span>
  </div>
</template>

<script>
import Sortable from "sortablejs";
export default {
  name: "sortablejs",
  data() {
    return {
      dataList: [
        {
          id: 0,
          title: "11111111111111111111111111111",
        },
        {
          id: 1,
          title: "22222222222222222222222222222",
        },
        {
          id: 2,
          title: "33333333333333333333333333333",
        },
      ],
      panelEl: null,
    };
  },
  mounted() {
    const panelEl = this.$refs.sortablejsRefs;
    this.panelEl = panelEl;
    Sortable.create(panelEl, {
      animation: 100,
      sort: true,
      onEnd: (evt) => {
        const { oldIndex, newIndex } = evt;
        const oldList = this.dataList;
        oldList.splice(newIndex, 0, oldList.splice(oldIndex, 1)[0]);
        this.dataList = oldList;
        console.log(this.dataList, "dataList");
      },
    });
  },
};
</script>

<style lang="scss" scoped>
.sortablejs-list {
  width: 300px;
}
span {
  display: block;
  line-height: 20px;
  background-color: aqua;
  margin-top: 10px;
}
</style>

引入sortablejs,使用create创建拖拽,很简单,如果只是子组件加载时需要判断是否禁用,只需使用sort来设置是否禁用拖拽,那如何在初始化之后进行动态禁用拖拽?
请添加图片描述
在这里插入图片描述

动态禁用拖拽

<template>
  <div>
    <el-input v-model="inputs" placeholder="搜索"></el-input>
    <div ref="sortablejsRefs" class="sortablejs-list">
      <span
        :class="{ 'not-sort': inputs }"   // 控制类名
        v-for="(item, index) in dataList"
        :key="item.id"
      >
        {{ item.title }}
      </span>
    </div>
  </div>
</template>

<script>
import Sortable from "sortablejs";
export default {
  name: "sortablejs",
  data() {
    return {
      inputs: null,
      dataList: [
        {
          id: 0,
          title: "11111111111111111111111111111",
        },
        {
          id: 1,
          title: "22222222222222222222222222222",
        },
        {
          id: 2,
          title: "33333333333333333333333333333",
        },
      ],
      panelEl: null,
    };
  },
  mounted() {
    const panelEl = this.$refs.sortablejsRefs;
    this.panelEl = panelEl;
    Sortable.create(panelEl, {
      animation: 100,
      sort: true,
      filter: ".not-sort",  // 过滤.not-sort的元素
      onEnd: (evt) => {
        const { oldIndex, newIndex } = evt;
        console.log(oldIndex, newIndex);
        const oldList = this.dataList;
        oldList.splice(newIndex, 0, oldList.splice(oldIndex, 1)[0]);
        this.dataList = oldList;
        console.log(this.dataList, "dataList");
      },
    });
  },
};
</script>

<style lang="scss" scoped>
.sortablejs-list {
  width: 300px;
}
span {
  display: block;
  line-height: 20px;
  background-color: aqua;
  margin-top: 10px;
}
</style>

模拟搜索内容,输入框中输入内容,禁用拖拽功能。实现过程是利用 filter 来过滤掉你想禁用拖拽的部分,禁用拖拽就变成如何控制class类名。
![请添加图片描述](https://img-blog.csdnimg.cn/10fadfa961bf4b5184856a3b3f6ac8c9.gif
在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
禁止拖拽,你可以使用 SortableJS 提供的 disabled 选项。在 Vue 中,你可以使用 v-bind 指令来绑定 SortableJS 的配置项。以下是一个禁止拖拽的示例: ```html <template> <div> <div ref="list" v-bind:sortable="sortableOptions"> <div v-for="(item, index) in list" :key="item.id"> {{ item.text }} </div> </div> <button @click="toggleDrag">Toggle Drag</button> </div> </template> <script> import Sortable from 'sortablejs'; export default { data() { return { list: [ { id: 1, text: 'Item 1' }, { id: 2, text: 'Item 2' }, { id: 3, text: 'Item 3' }, ], sortableOptions: { disabled: true, }, }; }, mounted() { this.sortable = Sortable.create(this.$refs.list, { onEnd: this.onSortEnd, }); }, methods: { onSortEnd(event) { // update the list after sorting const itemEl = event.item; const newIndex = event.newIndex; const oldIndex = event.oldIndex; this.list.splice(newIndex, 0, this.list.splice(oldIndex, 1)[0]); }, toggleDrag() { // toggle drag and drop this.sortable.option('disabled', !this.sortable.option('disabled')); }, }, }; </script> ``` 在上面的示例中,我们使用了 SortableJS 的 disabled 选项来禁止拖拽。我们在组件的 data 中定义了 sortableOptions 对象,并将其绑定到 SortableJS 的配置中。然后,在 mounted 钩子函数中,我们使用 SortableJS 的 create 方法创建了一个 Sortable 实例,并将其绑定到组件的 this.sortable 变量上。最后,在 toggleDrag 方法中,我们使用 SortableJS 的 option 方法来切换 disabled 选项的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值