最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue如何實(shí)現(xiàn)數(shù)據(jù)的上移和下移

 更新時(shí)間:2024年06月07日 17:05:38   作者:小林犟  
這篇文章主要介紹了Vue如何實(shí)現(xiàn)數(shù)據(jù)的上移和下移問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Vue實(shí)現(xiàn)數(shù)據(jù)的上移和下移

場(chǎng)景

點(diǎn)擊上移下移按鈕進(jìn)行列表移動(dòng),第一行不能上移,最后一行不能下移

在這里插入圖片描述

解決方案

<el-button @click="moveUp(index)">上移</el-button>
<el-button @click="moveDown(index)">下移</el-button>

data() {
    return {
        list: [
            { id: 1, name: '張三' },
            { id: 2, name: '李四' },
            { id: 3, name: '王五' }
        ]
    }
}

// 上移
moveUp (index) {
    const arr = this.list
    arr.splice(index - 1, 1, ...arr.splice(index, 1, arr[index - 1]))
},
// 下移
moveDown (index) {
    const arr = this.list
    arr.splice(index, 1, ...arr.splice(index + 1, 1, arr[index]))
},

禁用上下移邏輯

  • 禁用上移:
:disabled="index === 0"
  • 禁用下移:
:disabled="index === list.length - 1"

Vue表單批量上移 下移

效果圖

在這里插入圖片描述

    // 上移
    handDmoveUp () {
      //選中行數(shù)據(jù)
      let arrChecked = this.$refs.ref_ri_table.getCheckboxRecords();
      //表格數(shù)據(jù)
      let arr = this.tableData;
        //正序遍歷,保證移動(dòng)完成的數(shù)據(jù)在下一次循環(huán)時(shí)位置不會(huì)再變動(dòng)
        a: for (let index1 = 0; index1 < arrChecked.length; index1++) {
          b: for (let index2 = 0; index2 < arr.length; index2++) {
            //選中數(shù)據(jù)定位到其在總數(shù)據(jù)中的位置時(shí)開始上移
            if (arrChecked[index1] === arr[index2]) {
              //選中數(shù)據(jù)與總數(shù)據(jù)索引相同時(shí),說明已經(jīng)上移到最上層,結(jié)束這層
              //循環(huán)
              if (index1 === index2) {
                break b;
              }
              //上移一位到達(dá)上一條數(shù)據(jù)的上方
              arr.splice(index2 - 1, 0, arr[index2]);
              //刪除原數(shù)據(jù)
              arr.splice(index2 + 1, 1);
              //上移完成結(jié)束內(nèi)存循環(huán),開始移動(dòng)下一條選中數(shù)據(jù)
              break b;
            }
          }
        }
 },


  //下移
    handMoveDown () {
      let arrChecked = this.$refs.ref_ri_table.getCheckboxRecords();
      let arr = this.tableData;
      
        a: for (let index1 = arrChecked.length - 1; index1 >= 0; index1--) {
          b: for (let index2 = arr.length - 1; index2 >= 0; index2--) {
            if (arrChecked[index1] === arr[index2]) {
              //選中數(shù)據(jù)索引+表格數(shù)組長(zhǎng)度-選中數(shù)組長(zhǎng)度=選中數(shù)據(jù)索引,代表以及下移到最底部,結(jié)束下移
              if (index1 + arr.length - arrChecked.length === index2) {
                break b;
              }
              arr.splice(index2 + 2, 0, arr[index2]);
              arr.splice(index2, 1);
              break b;
            }
          }
        }
        },

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

阜阳市| 保山市| 富锦市| 玉山县| 瑞丽市| 阜新| 策勒县| 兴海县| 阿城市| 天长市| 阜南县| 车险| 鲜城| 荔波县| 黄平县| 揭东县| 建湖县| 盐山县| 萨迦县| 桑植县| 辽宁省| 清水县| 濮阳县| 迁西县| 石首市| 二手房| 枣庄市| 鹰潭市| 泾阳县| 厦门市| 绥阳县| 萍乡市| 北海市| 嘉义市| 额尔古纳市| 临湘市| 呼图壁县| 固镇县| 兴仁县| 湘西| 东阿县|