關於上移下移,一行代碼完成數據交換

      onMoveUp (index) {
          if (index !== 0) {
          this.swapArray(this.dataList, index, index - 1)
        } else {
          this.$message({message: '已經處於置頂,無法上移', type: 'warning'})
        }
      },
      onMoveDown (index) {
        let len = this.dataList.length
        if (index + 1 !== len) {
          this.swapArray(this.dataList, index, index + 1)
        } else {
          this.$message({message: '已經處於置底,無法下移', type: 'warning'})
        }
      },
      swapArray (arr, indexOne, indexTwo) {
        arr[indexOne] = arr.splice(indexTwo, 1, arr[indexOne])[0]
        return arr
      }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章