当前位置:   article > 正文

element table 表格实现上移、下移_el-table上移下移

el-table上移下移

业务场景:有时候需要前端实现上移和下移功能

代码如下:

根据交互形式,我这里是把第一条数据的上移按钮置灰不可点击::disabled="scope.$index == 0";最后一条数据下移按钮置灰不可点击::disabled="(scope.$index + 1) == tableData.length"。

  1. <template>
  2. <el-table
  3. :data="tableData">
  4. <el-table-column type="index" label="序号" width="60"></el-table-column>
  5. <el-table-column label="名称" prop="name"></el-table-column>
  6. <el-table-column label="操作">
  7. <template slot-scope="scope">
  8. <el-button type="text" style="padding:0" :disabled="scope.$index == 0" @click="moveUpward(scope.row, scope.$index)">上移</el-button>
  9. <el-button type="text" style="padding:0" :disabled="(scope.$index + 1) == tableData.length" @click="moveDown(scope.row, scope.$index)">下移</el-button>
  10. </template>
  11. </el-table-column>
  12. </el-table>
  13. </template>
  14. <script>
  15. export default {
  16. data() {
  17. tableData: [
  18. {id: 1, name: '小明001'},
  19. {id: 2, name: '小红001'},
  20. {id: 3, name: '小强001'},
  21. {id: 4, name: '小刚001'},
  22. {id: 5, name: '小白001'},
  23. {id: 6, name: '小花001'}
  24. ]
  25. },
  26. methods: {
  27. moveUpward(row, index) {
  28. if (index > 0) {
  29. let upData = this.tableData[index - 1];
  30. this.tableData.splice(index - 1, 1);
  31. this.tableData.splice(index, 0, upData);
  32. } else {
  33. this.$message({
  34. message: '已经是第一条,上移失败',
  35. type: 'warning'
  36. });
  37. }
  38. },
  39. moveDown(row, index) {
  40. if ((index + 1) == this.tableData.length) {
  41. this.$message({
  42. message: '已经是最后一条,下移失败',
  43. type: 'warning'
  44. });
  45. } else {
  46. let downData = this.tableData[index + 1];
  47. this.tableData.splice(index + 1, 1);
  48. this.tableData.splice(index, 0, downData);
  49. }
  50. }
  51. }
  52. }
  53. </script>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/224000
推荐阅读
相关标签