Vue實現(xiàn)table列表項上下移動的示例代碼
結(jié)合Element組件,scope中有三個參數(shù)(row,cow,$index)分別表示行內(nèi)容、列內(nèi)容、以及此行索引值,
table上綁定數(shù)組 :data=“newsList”。
上移和下調(diào)兩個按鈕,并綁定上點擊函數(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視圖更新?。?!
直接使用下面這種方式是錯誤的,雖然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: '已經(jīng)是列表中第一個素材!',
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: '已經(jīng)是列表中最后一個素材!',
type: 'warning'
})
} else {
let i = this.newsList[index + 1]
this.$set(this.newsList, index + 1, this.newsList[index])
this.$set(this.newsList, index, i)
}
}
最后貼出效果圖:

到此這篇關(guān)于Vue實現(xiàn)table列表項上下移動的示例代碼的文章就介紹到這了,更多相關(guān)Vue table列表項上下移動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue無法訪問.env.development定義的變量值問題及解決
這篇文章主要介紹了Vue無法訪問.env.development定義的變量值問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
在vue中created、mounted等方法使用小結(jié)
這篇文章主要介紹了在vue中created、mounted等方法使用小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
解決前端調(diào)用后端接口返回200但數(shù)據(jù)返回的是html標簽
這篇文章主要給大家介紹了關(guān)于如何解決前端調(diào)用后端接口返回200但數(shù)據(jù)返回的是html標簽的相關(guān)資料,文中通過圖文將解決的過程介紹的非常詳細,對同樣遇到這個問題的朋友具有一定的參考解決價值,需要的朋友可以參考下2024-05-05
vue利用openlayers實現(xiàn)動態(tài)軌跡
這篇文章主要為大家介紹了vue利用openlayers實現(xiàn)動態(tài)軌跡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
vue異步axios獲取的數(shù)據(jù)渲染到頁面的方法
今天小編就為大家分享一篇vue異步axios獲取的數(shù)據(jù)渲染到頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

