Sortable 的 VueDraggable 无法正常工作并将所选项目发送到 init 上的第一个

VueDraggable of Sortable is not working properly and send the choosed item to the first on init

我在我的 Nuxt 项目中使用 Vue.Draggable。我在一些页面和组件中使用了它,一切都很好。但是在我的一个页面上它给了我一个愚蠢的问题!加载页面时,它会将所选项目发送到第一个!!之后一切正常!即使我选择和取消选择而不移动,一切都很好! 我试图检查所选择的是否到达阵列的第一个,再次正确移动它。 it worked when the chosen item is the first if array but if i choose others than first, on 2nd try it move another item too!! 所以我有点卡住了。

代码如下: 顺便说一句,有些变量和方法你找不到它们,比如 cmsListItems。它们已全局添加到我的项目中

模板:

<draggable v-model="myItemsArray" @end="onEnd" @choose="onChoose" @unchoose="onUnChoose" v-bind="getOptions()">
    <transition-group type="transition" name="sortable_transition">
      <categorylistcard v-for="(category, index) in cmsListItems" 
      listtype="cat"
      :key="category.id" 
      :cat="category" 
      :index="index" 
      :showforsub="showForSub"
      :parentarr="parentArr"
      @addnewsub="addNewCat()"
      :sub="true"
      :sublvl="true"
      :sortable="true"
      :btntitle="lang.addsubcat"
      />
    </transition-group>
  </draggable>

脚本:

export default {
    data(){
      return{
        oldIndex: '',
        newIndex: '',
        dragOptions: {
            ghostClass: "sortable_ghost",
            chosenClass: "sortable_chosen",
            handle: ".sortable_handle",
            disabled: false
        },
        isMoved: false,
        myItemsArray: [],
        originalItemsArray: [],
        choosedItem: null
      }
    },
    methods:{
          // ***** Reorder By Sorting *****\

          this.isMoved = true
          this.oldIndex = event.oldIndex
          this.newIndex = event.newIndex
          console.log(this.myItemsArray[this.oldIndex])
          console.log(this.myItemsArray[this.newIndex])
          if(this.oldIndex !== this.newIndex){

            this.dragOptions.disabled = true
            let response = await this.axiosGet(`category/reorder/${this.originalItemsArray[this.oldIndex].id}/${this.originalItemsArray[this.newIndex].id}`)
            if(this.resOk(response.status)){
                this.noty(ALERT_TYPE[1], 'ok')
                this.originalItemsArray = this.myItemsArray
                this.isMoved = false
                this.dragOptions.disabled = false
                this.addClassToMovedItems(this.oldIndex)
                this.addClassToMovedItems(this.newIndex)
                this.setCmsListItems(this.myItemsArray)
                // this part is my defected solution
                setTimeout(() => {
                  if(this.myItemsArray[this.oldIndex].id === this.choosedItem.id || this.myItemsArray[0].id === this.choosedItem.id){
                    let arrayOfItems = [...this.originalItemsArray]
                    arrayOfItems.shift()
                    arrayOfItems.splice(this.newIndex,0,this.choosedItem)
                    this.setCmsListItems(arrayOfItems)
                    this.myItemsArray = [...this.cmsListItems]
                  }
                }, 50);
                // --------------------
            }else{
                this.isMoved = false
                this.myItemsArray = this.originalItemsArray
                this.dragOptions.disabled = false
                this.addClassToMovedItems(this.oldIndex)
                this.addClassToMovedItems(this.newIndex)
            }

          }else{
            this.isMoved = false
            this.myItemsArray = this.originalItemsArray
            this.dragOptions.disabled = false
            this.addClassToMovedItems(this.oldIndex)
            this.addClassToMovedItems(this.newIndex)
          }
          
      },
      addClassToMovedItems(index){
          if((index == this.oldIndex || index == this.newIndex) && this.isMoved == true){
              return 'sortable_moved'
          }else{
              return ''
          }
      }
    },
    async fetch(){
      this.btnLoading = true
      let response = await this.axiosGet(`categories/admin/0/1`)
      if(this.resOk(response.status)){
        if(this.notEmpty(response.data)){
          this.setCmsListItems(response.data.items)
          this.myItemsArray = [...this.cmsListItems]
          this.originalItemsArray = [...this.cmsListItems]
        }
        this.btnLoading = false
      }
    },
}

我有点破解它,所以我不推荐它!!

我用 @choose 得到了选择的项目,在我的 @end 中检查了我的数组的第一个索引是否是选择的项目,unshift 数组并将选择的添加到 newIndexsplice() 如下所示:

setTimeout(() => {
  if(this.myItemsArray[this.oldIndex].id === this.choosedItem.id || this.myItemsArray[0].id === this.choosedItem.id){
    let arrayOfItems = [...this.originalItemsArray]
    arrayOfItems.shift()
    arrayOfItems.splice(this.newIndex,0,this.choosedItem)
    this.setCmsListItems(arrayOfItems)
    this.myItemsArray = [...this.cmsListItems]
    this.choosedItem = null
  }
}, 1);