0

我在我的 Nuxt 项目中使用 Vue.Draggable。我在一些页面和组件中使用了它,一切都很好。但在我的一个页面上,它给了我一个愚蠢的问题!加载页面时,它将所选项目发送到第一个!之后一切正常!即使我选择和取消选择而不移动,一切正常!我试图检查选择的数组是否再次正确移动它。当所选项目是第一个 if 数组时它起作用,但如果我选择第一个而不是第一个,第二次尝试它也移动另一个项目!所以我有点卡住了。

这是代码:顺便说一句,有些变量和方法你找不到它们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
      }
    },
}
4

1 回答 1

0

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

如果我的数组的第一个索引是选定的项目,我得到了选定的项目,@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);
于 2020-08-13T13:11:40.990 回答