Vue實現(xiàn)table列表項上下移動的示例代碼
結合Element組件,scope中有三個參數(shù)(row,cow,$index)分別表示行內容、列內容、以及此行索引值,
table上綁定數(shù)組 :data=“newsList”。
上移和下調兩個按鈕,并綁定上點擊函數(shù),將此行的索引值(scope.$index)作為參數(shù):
<template> <el-table :data="newsList"> <el-table-column type="index" label="序號" width="50"></el-table-column> <el-table-column prop="title" label="文章標題" min-width="300" ></el-table-column> <el-table-column prop="descript" label="文章描述" min-width="300" ></el-table-column> <el-table-column label="操作(素材排序)" > <template slot-scope="scope"> <el-button size="mini" type='text' @click.stop="sortUp(scope.$index, scope.row)">向上↑ </el-button> <el-button size="mini" type='text' @click.stop="sortDown(scope.$index, scope.row)">向下↓</el-button> </template> </el-table-column> </el-table> </template>
上移下移函數(shù),此處的坑,是vue視圖更新?。。?/p>
直接使用下面這種方式是錯誤的,雖然tableList的值變了,但是不會觸發(fā)視圖的更新:
upFieldOrder (index) { let temp = this.tableList[index-1]; this.tableList[index-1] = this.tableList[index] this.tableList[index] = temp },
正確方法:
// 上移按鈕 sortUp (index, row) { if (index === 0) { this.$message({ message: '已經是列表中第一個素材!', type: 'warning' }) } else { let temp = this.newsList[index - 1] this.$set(this.newsList, index - 1, this.newsList[index]) this.$set(this.newsList, index, temp) } },
同理,下移函數(shù),
// 下移按鈕 sortDown (index, row) { if (index === (this.newsList.length - 1)) { this.$message({ message: '已經是列表中最后一個素材!', type: 'warning' }) } else { let i = this.newsList[index + 1] this.$set(this.newsList, index + 1, this.newsList[index]) this.$set(this.newsList, index, i) } }
最后貼出效果圖:
到此這篇關于Vue實現(xiàn)table列表項上下移動的示例代碼的文章就介紹到這了,更多相關Vue table列表項上下移動內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue3?使用defineAsyncComponent與component標簽實現(xiàn)動態(tài)渲染組件思路詳解
這篇文章主要介紹了vue3?使用defineAsyncComponent與component標簽實現(xiàn)動態(tài)渲染組件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03vue使用百度地圖報錯BMap?is?not?defined問題及解決
這篇文章主要介紹了vue使用百度地圖報錯BMap?is?not?defined問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10