vue使用sortablejs实现对的行拖拽时,需刷新,才展示结果
问题描述
表格行数据可以进行拖拽,后台数据返回正确,但是页面无法正确展示
找到问题了,that.articleList.splice(newIndex, 0, currRow)这句的问题
代码
html部分
<el-table id="crTable" :data="articleList" v-loading="listLoading" element-loading-text="Loading" highlight-current-row row-key="indexId">
<el-table-column prop="rank" label="排序" width="65" v-if="!disable">
<template slot-scope="scope">
<span class="rank-number" v-show=" !isSort" @click="isSort = true">{{scope.row.rank}}</span>
<el-input v-model="scope.row.rank" placeholder="请输入内容" size="mini" @keyup.enter.native="handleSorTable()" v-show="isSort"></el-input>
</template>
</el-table-column>
<el-table-column label="标题">
<template slot-scope="scope">
<router-link :to="'/backend/article/modify?id='+scope.row.id">{{scope.row.title}}</router-link>
</template>
</el-table-column>
<el-table-column prop="is_recommended" label="推荐" width="200%">
<template slot-scope="scope">
<span v-if="scope.row.is_recommended === 1">是</span>
<span v-else>否</span>
</template>
</el-table-column>
<el-table-column prop="visible" label="状态" width="200%">
<template slot-scope="scope">
<span v-if="scope.row.visible === 1">公开</span>
<span v-if="scope.row.visible === 0">隐藏</span>
<span v-if="scope.row.visible === 2">草稿</span>
</template>
</el-table-column>
<el-table-column label="操作" width="160" align="center">
<template slot-scope="scope">
<router-link :to="'/backend/article/modify?id='+scope.row.id">
<el-button type="primary" size="mini">编辑</el-button>
</router-link>
<el-dropdown>
<el-button type="info" size="mini" plain>
更多<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<router-link :to="'/backend/preview?id='+scope.row.id" target="_blank">
<el-dropdown-item>预览</el-dropdown-item>
</router-link>
<el-dropdown-item v-if="scope.row.visible === 0||scope.row.visible == 2" @click.native="changeVisible(scope.row.id,1)">公开</el-dropdown-item>
<el-dropdown-item v-if="scope.row.visible === 1" @click.native="changeVisible(scope.row.id,0)">隐藏</el-dropdown-item>
<el-dropdown-item @click.native="deleteArticle(scope.row.id)">删除</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</template>
</el-table-column>
</el-table>
js部分
// 获取文章列表
fetchArticleList() {
this.disable = !this.changeCategory
this.isSort = false
this.pageSize = this.disable ? 10 : 1000
const params = {
lang: this.language,
visible: this.visibleValue,
cat_id: this.changeCategory,
page_size: this.pageSize,
offset: this.offset,
title: this.title
}
getArticleList(params).then(response => {
this.articleList = response.data.items
this.totalCount = response.data.total_count
this.sorTable()
this.rowDrop()
})
}
// 实现行拖拽
rowDrop() {
const tbody = document.getElementById('crTable').querySelector('.el-table__body-wrapper tbody')
const that = this
Sortable.create(tbody, {
onEnd({ newIndex, oldIndex }) {
const currRow = that.articleList.splice(oldIndex, 1)[0]
that.articleList.splice(newIndex, 0, currRow)
}
})
}
期望
that.articleList.splice(newIndex, 0, currRow)这句代码有问题,拖拽后,console.log的结果正确,但是没有在表格中显示
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是:
有时你可能需要为已有对象赋值多个新 property,比如使用
Object.assign()
或_.extend()
。但是,这样添加到对象上的新 property 不会触发更新。在这种情况下,你应该用原对象与要混合进去的对象的 property 一起创建一个新的对象。官网链接:
https://cn.vuejs.org/v2/guide...