当前位置:   article > 正文

vue2中使用el-table实现直接编辑表格,鼠标点击空白处实现保存并传递给后端的简单方法_el-table 中实现表格可编辑

el-table 中实现表格可编辑
一、实现表格编辑

1.首先我们需要用到highlight-current-row去初始定位高亮指定行,意思就是选中某个数据行时,高亮该行。

需求实现如下:点击某个单元格实现整行数据高亮,并且可以修改,修改完成后鼠标点击空白处,实现保存修改后的整行数据并传递给后端。

2.用vue的axios发起post请求:

3. 在el-input@blur的意思是当元素失去焦点时所触发的事件,当不点击单元格时显示值,点击单元格时显示编辑框,用v-ifv-else来判断

  1. <div class="divCaculateResultDisplay">
  2. <div class="divParamInput2">
  3. <label class="labConfidenceParamTitle">结果:</label>
  4. </div>
  5. <div class="divNotAllowCaculateResult">
  6. <div class="divResultTitle">
  7. <p class="pValueResult">导入数据</p>
  8. </div>
  9. <el-table
  10. border
  11. height="385"
  12. :data="tableData"
  13. class="tb-edit"
  14. style="width: 100%"
  15. highlight-current-row>
  16. <el-table-column fixed align="center" prop="createTime" label="创建时间" width="150">
  17. </el-table-column>
  18. <el-table-column prop="powerFactor" label="功率因素" width="" align="center">
  19. <template slot-scope="scope">
  20. <span v-if="!scope.row.setingFlag" @click="handleEdit(scope.row)">{{scope.row.powerFactor}}</span>
  21. <div v-else>
  22. <el-input v-model="scope.row.powerFactor" @blur="handleBlur(scope.row)"></el-input>
  23. </div>
  24. </template>
  25. </el-table-column>
  26. <!-- 此处省略 -->
  27. <el-table-column label="操作">
  28. <template slot-scope="scope">
  29. <!--<el-button @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
  30. <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  31. </template>
  32. </el-table-column>
  33. </el-table>
  34. <el-pagination :current-page="page" :page-size="10" @current-change="CurrentChange" style="
  35. margin-top: 10px;
  36. display: flex;
  37. justify-content: center;
  38. align-items: center;
  39. " background layout="prev, pager, next" :total="total">
  40. </el-pagination>
  41. </div>
  42. </div>

4.引入接口

5.返回数组

6.方法使用

  1. handleBlur(row) {
  2. row.setingFlag = false;
  3. const param={
  4. id:row.id,
  5. powerFactor:row.powerFactor,
  6. currentA:row.currentA,
  7. currentB:row.currentB,
  8. currentC:row.currentC,
  9. voltage:row.voltage,
  10. voltageA:row.voltageA,
  11. voltageB:row.voltageB,
  12. voltageC:row.voltageC,
  13. activePower:row.activePower,
  14. reactivePower:row.reactivePower,
  15. apparentPower:row.apparentPower,
  16. calculatedPowerFactor:row.calculatedPowerFactor,
  17. energyConsumption:row.energyConsumption
  18. }
  19. modify(param).then(res=>{
  20. if(res.code == 200){
  21. this.$message.success('修改数据成功!')
  22. }else{
  23. this.$message.error('获取数据失败!')
  24. }
  25. })
  26. },
  27. handleEdit(row) {
  28. this.$set(row, 'setingFlag', true)
  29. },

7.完整代码如下:

  1. <template>
  2. <div class="divCaculateResultDisplay">
  3. <div class="divParamInput2">
  4. <label class="labConfidenceParamTitle">结果:</label>
  5. </div>
  6. <div class="divNotAllowCaculateResult">
  7. <div class="divResultTitle">
  8. <p class="pValueResult">导入数据</p>
  9. </div>
  10. <el-table
  11. border
  12. height="385"
  13. :data="tableData"
  14. class="tb-edit"
  15. style="width: 100%"
  16. highlight-current-row>
  17. <el-table-column fixed align="center" prop="createTime" label="创建时间" width="150">
  18. </el-table-column>
  19. <el-table-column prop="powerFactor" label="功率因素" width="" align="center">
  20. <template slot-scope="scope">
  21. <span v-if="!scope.row.setingFlag" @click="handleEdit(scope.row)">{{scope.row.powerFactor}}</span>
  22. <div v-else>
  23. <el-input v-model="scope.row.powerFactor" @blur="handleBlur(scope.row)"></el-input>
  24. </div>
  25. </template>
  26. </el-table-column>
  27. <!-- 此处省略 -->
  28. <el-table-column label="操作">
  29. <template slot-scope="scope">
  30. <!--<el-button @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
  31. <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  32. </template>
  33. </el-table-column>
  34. </el-table>
  35. <el-pagination :current-page="page" :page-size="10" @current-change="CurrentChange" style="
  36. margin-top: 10px;
  37. display: flex;
  38. justify-content: center;
  39. align-items: center;
  40. " background layout="prev, pager, next" :total="total">
  41. </el-pagination>
  42. </div>
  43. </div>
  44. </template>
  45. <script>
  46. import {modify} from "@/api/request"
  47. export default {
  48. data() {
  49. return {
  50. page: 1,
  51. limit: 10,
  52. total:0,
  53. tableData: [],
  54. }
  55. }
  56. methods: {
  57. handleBlur(row) {
  58. row.setingFlag = false;
  59. const param={
  60. id:row.id,
  61. powerFactor:row.powerFactor,
  62. currentA:row.currentA,
  63. currentB:row.currentB,
  64. currentC:row.currentC,
  65. voltage:row.voltage,
  66. voltageA:row.voltageA,
  67. voltageB:row.voltageB,
  68. voltageC:row.voltageC,
  69. activePower:row.activePower,
  70. reactivePower:row.reactivePower,
  71. apparentPower:row.apparentPower,
  72. calculatedPowerFactor:row.calculatedPowerFactor,
  73. energyConsumption:row.energyConsumption
  74. }
  75. modify(param).then(res=>{
  76. if(res.code == 200){
  77. this.$message.success('修改数据成功!')
  78. }else{
  79. this.$message.error('获取数据失败!')
  80. }
  81. })
  82. },
  83. handleEdit(row) {
  84. this.$set(row, 'setingFlag', true)
  85. },
  86. }
  87. }
  88. </script>

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

闽ICP备14008679号