vue 拖拽排序(前端+后端)

效果视频:https://live.csdn.net/v/299764?spm=1001.2014.3001.5501icon-default.png?t=N4P3https://live.csdn.net/v/299764?spm=1001.2014.3001.5501

vue拖拽

前端部分

vue部分代码

<li v-for="(item, index) in oneChannelList" :key="item.id" :draggable="true" @dragstart="dragstart(item)"
            @dragenter="dragenter(item,$event)" @dragend="dragend(item,$event)" @dragover="dragover($event)"
            @click="handleChangeTab(item.id, true)">
            <div class="list_img" @mouseenter="buttonsShowFun(item.id)" @mouseleave="buttonsShowFun(-1)">
              <div class="img" :style="{backgroundImage:'url('+item.img+')'}" />
              <div v-show="item.id === buttonsShow" class="buttons">
                <el-button type="danger" icon="el-icon-delete" style="margin:5px;" size="mini"
                  @click="deleteChannel(item.id)" />
                <el-button type="primary" icon="el-icon-picture-outline" style="margin:5px;" size="mini"
                  @click="openImg(item.img)" />
                <el-button type="primary" icon="el-icon-edit" style="margin:5px;" size="mini" @click="info(item.id)" />
              </div>
            </div>
            <em class="list_title">{{ item.name }}</em>
          </li>
 data() {
      return {
        oldData: [],
        newData: [],
        }
}

 methods: {
      //拖拽排序前的数组
      dragstart(value) {
        this.oldData = value
        console.log("oldData:" + this.oldData);
      },
      //拖拽排序后的数组
      dragenter(value, e) {
        this.newData = value
        e.preventDefault()
        console.log("newData:" + this.newData);
      },
      //拖拽最后操作
      dragend(value, e) {
        if (this.oldData !== this.newData) {
          const oldIndex = this.oneChannelList.indexOf(this.oldData)
          const newIndex = this.oneChannelList.indexOf(this.newData)
          const newItems = [...this.oneChannelList]
          newItems.splice(oldIndex, 1)
          newItems.splice(newIndex, 0, this.oldData)
          this.oneChannelList = [...newItems]
          const idList = []
          this.oneChannelList.forEach((value, index) => {
            idList.push(value.id)
          })
          //后台更新序号
          this.channelRankFun(idList);
        }
      },
      // 拖动事件(主要是为了拖动时鼠标光标不变为禁止)
      dragover(e) {
        e.preventDefault()
      },
     channelRankFun(ids) { //跟后台交互的方法,只传排序完成的id数组就行了
        channelRank({
          idList: ids
        }).then(
          res => {
            console.log(res)
          }
        );
      },
}

 Java部分代码

这里我直接写的sql,没有在java里写其他的代码。个人根据实际情况参考

<update id="channelRank">
     update ayj_channel   set rank = case
        <foreach collection="array" item="item" index="index" open="" close="" separator="">
            when id = #{item} then #{index}+1
        </foreach>
        end
        where id in (
        <foreach collection="array" item="item" index="index" open="" close="" separator=",">
            #{item}
        </foreach>
        )
    </update>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yzhSWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值