前端js實現表格數據的上移下移

 

vue項目需求中需要前端實現表格行數據的上移下移,效果如下

實現思路:主要使用前端對數組的操作方法splice()

代碼:

    tableData: [{
        name: '1',
        id: '1'
      }, {
        name: '2',
        id: '2'
      }, {
        name: '3',
        id: '3'
      }, {
        name: '4',
        id: '4'
      }, {
        name: '5',
        id: '5'
    }]


    <el-table-column width="180">
        <template slot-scope="scope">
            <el-button
             title="上移"
             @click="moveup(scope.$index)"
             icon="icon-arrow_up">
             </el-button>
             <el-button
             title="下移"
             @click="movedown(scope.$index)"
             icon="icon-arrow_down">
            </el-button>
        </template>
     </el-table-column>

 moveup (val) {
      let x = val; let y = val + 1
      this.tableData.splice(x - 1, 1, ...this.tableData.splice(y - 1, 1, this.tableData[x - 1]))
 },
 movedown (val) {
      let x = val + 1; let y = val + 2
      this.tableData.splice(x - 1, 1, ...this.tableData.splice(y - 1, 1, this.tableData[x - 1]))
 },

 

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