sortable.js 页面排序混乱问题解决

sortable.js编写一个可拖拽组件 

 最近用sortable.js写了一个多选、重复选、可拖拽的下拉框,但onend回调里将数据改变之后,页面会根据最新的数据重新进行渲染,就会出现拖拽之后页面不改变或者排序混乱,解决办法如下

附代码

<div class="titInputDiv">
                <div :class="callError?'titInput callError':'titInput'" @click.stop="titInputX()">
                  <div class="titInputTabId" style="display:flex;flex-wrap: wrap;" id="selectTit">
                  <div class="titInputTab" v-for="(item,index5) in model.titleComponents" :key="index5" @click="delTitName(index5)"><div>{{item}}</div><a-icon type="close" /></div>
                  </div>
                </div>
                <div class="selectTitInputTab" v-if="titInput">
                    <div class="selectCon1" style="padding-left:6px">固定词组</div>
                    <div v-for="(item,index) in guding" :key="index" class="selectCon" style="padding-left:20px" @click="addTitName(item.fixedPhrase)">{{item.fixedPhrase}}</div>
                    <div v-for="(item,index1) in titleAll" :key="index1+'_'" class="selectCon" style="padding-left:6px" @click="addTitName(item.fixedPhrase)">{{item.fixedPhrase}}</div>
                </div>
              </div>
Sortable.create(selectTit, {
          sort:true,
          animation: 150,
            onEnd: function ({ newIndex, oldIndex }) {
              console.log(newIndex,oldIndex)
              let newD = document.getElementsByClassName('titInputTab')[newIndex]
              let oldD = document.getElementsByClassName('titInputTab')[oldIndex]
              if(newIndex>oldIndex) {
                document.getElementsByClassName('titInputTabId')[0].insertBefore(newD,oldD)
                }
              else {
              let oldD1 = document.getElementsByClassName('titInputTab')[oldIndex+1]
                document.getElementsByClassName('titInputTabId')[0].insertBefore(newD,oldD1)
              }
              const currRow = that.model.titleComponents.splice(oldIndex, 1)[0]
              that.model.titleComponents.splice(newIndex, 0, currRow)
              console.log(that.model.titleComponents)
            },
        });
// 多选下拉框样式
.titInputDiv {
  position: relative;
  width: 100%;
  height: auto;
  min-height: 32px;
}
.titInput {
  color: rgba(0, 0, 0, 0.65);
  background-color: #fff;
  background-image: none;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  position: relative;
  display: inline-block;
  width: 100%;
  height: auto;
  min-height: 32px;
  padding: 4px 6px 0 6px;
  line-height: 32px;
  transition: all 0.3s;
  display: flex;
  flex-wrap:wrap;
}
.titInput:hover {
  border-color: #c4ba8f;
  border-right-width: 1px !important;
}
.titInputTab {
  width:auto;
  height: 24px;
  line-height: 24px;
  background-color: #fafafa;
  color: rgba(0, 0, 0, 0.65);
  border: 1px solid #e8e8e8;
  border-radius: 2px;
  padding: 0 8px;
  margin-right: 4px;
  margin-bottom: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.titInputTab div {
  padding-right: 4px;
}
.titInputTab i {
  font-size: 11px;
}
.selectTitInputTab {
  position: absolute;
  z-index: 111;
  top: 35px;
  left: 0;
  width: 100%;
  height: auto;
  padding: 2px 6px;
  max-height: 300px;
  overflow-y: scroll;
  background: #fff;
  color: rgba(0, 0, 0, 0.65);
  box-shadow: 0 2px 8px 0 #ccc;
  border-radius: 4px;
}
.selectTitInputTabDiv {
}
.selectCon:hover {
  background-color: #f7f6e9;
  border-radius: 8px;
}
.selectCon1 {
  color: rgba(0, 0, 0, 0.45);;
}
.selectCon .selectCon1 {
  height: 36px;
  line-height: 36px;
}
.callError {
  border: 1px solid red;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 sortable.js 来实现拖动排序,并将排序结果发送到后台,您需要遵循以下步骤: 1. 引入 sortable.js 库 在页面中引入 sortable.js 库,可以从官方网站下载或使用 CDN。 2. 创建可排序的列表 使用 HTML 和 CSS 创建可排序的列表。在每个列表项中添加一个唯一的标识符,以便在排序后将其发送到后台。 3. 初始化 sortable.jsJavaScript 中初始化 sortable.js。使用 onEnd 回调函数来捕获排序事件,并获取排序结果。 4. 将结果发送到后台 在 onEnd 回调函数中,将排序结果发送到后台。您可以使用 AJAX 或其他方法来完成此操作。 以下是示例代码: HTML: ``` <ul id="sortable"> <li data-id="1">Item 1</li> <li data-id="2">Item 2</li> <li data-id="3">Item 3</li> <li data-id="4">Item 4</li> </ul> <button id="save">Save</button> ``` JavaScript: ``` // 初始化 sortable.js var sortable = Sortable.create(document.getElementById('sortable'), { onEnd: function (evt) { // 获取排序结果 var items = sortable.toArray(); // 将结果发送到后台 $.ajax({ url: '/save', type: 'post', data: { items: items }, success: function (response) { console.log(response); } }); } }); // 保存按钮点击事件 $('#save').click(function () { // 获取排序结果 var items = sortable.toArray(); // 将结果发送到后台 $.ajax({ url: '/save', type: 'post', data: { items: items }, success: function (response) { console.log(response); } }); }); ``` 在上面的示例代码中,我们使用 jQuery 的 AJAX 方法将排序结果作为 POST 请求发送到 /save 路径。您需要根据自己的需求更改此代码以适应您的应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值