当前位置:   article > 正文

vue elmentui 可编辑table 实现

vue elmentui 可编辑table 实现

废话不多说上图:

1.可编辑input

2.可编辑下来框

3.点击chechbox

4.可编辑radio

其实后面两种可以直接显示值 需要修改直接改就行

保持风格统一所以就做了点击之后出现修改功能

上代码,不要哔哔 哈哈 粗暴   真得是曲不离口 拳不离手, 几天不练习都不会写了

声明:部分代码有借鉴

  1. <template>
  2. <div class="divCaculateResultDisplay">
  3. <div class="divParamInput2">
  4. <label class="labConfidenceParamTitle">结果:</label>
  5. </div>
  6. <div class="divNotAllowCaculateResult">
  7. <el-table
  8. border
  9. height="385"
  10. :data="tableData"
  11. class="tb-edit"
  12. style="width: 100%"
  13. @cell-click="getCell"
  14. :cell-class-name="tableCellClassName"
  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. <el-input v-model="scope.row.powerFactor" @blur="handleBlur(scope.row)" v-if=' scope.row.index == tabRowIndex && scope.column.index == tabColumnIndex' v-focus></el-input>
  21. <div v-else>
  22. <span >{{scope.row.powerFactor}}</span>
  23. </div>
  24. </template>
  25. </el-table-column>
  26. <el-table-column prop="selecttest" label="测试选择下拉" width="" align="center" >
  27. <template slot-scope="scope">
  28. <el-select v-focus v-model="scope.row.selecttest" v-if=' scope.row.index == tabRowIndex && scope.column.index == tabColumnIndex' @change="handleBlur(scope.row)" >
  29. <el-option
  30. v-for="item in FloorsOptions"
  31. :key="item"
  32. :label="item"
  33. :value="item"
  34. ></el-option>
  35. </el-select>
  36. <div v-else>
  37. <span >{{scope.row.selecttest}}</span>
  38. </div>
  39. </template>
  40. </el-table-column>
  41. <el-table-column prop="checkboxtest" label="测试check" width="" align="center" >
  42. <template slot-scope="scope">
  43. <el-checkbox v-model="scope.row.checkboxtest" v-if=' scope.row.index == tabRowIndex && scope.column.index == tabColumnIndex' @change="handleBlur(scope.row)" label="复选框测试"></el-checkbox>
  44. <div v-else>
  45. <span >{{scope.row.checkboxtest}}</span>
  46. </div>
  47. </template>
  48. </el-table-column>
  49. <el-table-column prop="radiotest" label="测试radio" width="" align="center" >
  50. <template slot-scope="scope">
  51. <el-radio-group v-if=' scope.row.index == tabRowIndex && scope.column.index == tabColumnIndex' v-focus @change="handleBlur(scope.row)" v-model="scope.row.radiotest" >
  52. <el-radio label="男" ></el-radio>
  53. <el-radio label="女" ></el-radio>
  54. </el-radio-group>
  55. <div v-else>
  56. <span >{{scope.row.radiotest}}</span>
  57. </div>
  58. </template>
  59. </el-table-column>
  60. <!-- 此处省略 -->
  61. <el-table-column label="操作">
  62. <template slot-scope="scope">
  63. <el-button @click="handleRowEdit(scope.$index, scope.row)">编辑</el-button>
  64. <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  65. </template>
  66. </el-table-column>
  67. </el-table>
  68. <el-pagination :current-page="page" :page-size="10" @current-change="CurrentChange" style="
  69. margin-top: 10px;
  70. display: flex;
  71. justify-content: center;
  72. align-items: center;
  73. " background layout="prev, pager, next" :total="total">
  74. </el-pagination>
  75. </div>
  76. </div>
  77. </template>
  78. <script>
  79. export default {
  80. directives: {
  81. //注册一个局部的自定义指令 v-focus,输入框自动聚焦
  82. focus: {
  83. inserted: function (el) {
  84. el.querySelector('input').focus()
  85. }
  86. }
  87. },
  88. data() {
  89. return {
  90. page: 1,
  91. tabRowIndex: null, //行角标
  92. tabColumnIndex: '', //列角标
  93. limit: 10,
  94. total:0,
  95. FloorsOptions:["test1","test2","test3","test4","test5"],
  96. tableData: [{
  97. createTime:"2024-01-01",
  98. powerFactor:"555",
  99. selecttest:"test1",
  100. checkboxtest:"false",
  101. radiotest:"",
  102. setingFlag:false
  103. },
  104. {
  105. createTime:"2024-02-01",
  106. powerFactor:"66",
  107. selecttest:"test2",
  108. checkboxtest:"false",
  109. radiotest:"",
  110. setingFlag:false
  111. },
  112. {
  113. createTime:"2024-03-01",
  114. powerFactor:"66",
  115. selecttest:"test3",
  116. checkboxtest:"false",
  117. radiotest:"",
  118. setingFlag:false
  119. },
  120. {
  121. createTime:"2024-04-01",
  122. powerFactor:"66",
  123. selecttest:"test4",
  124. checkboxtest:"false",
  125. radiotest:"",
  126. setingFlag:false
  127. },
  128. ],
  129. }
  130. },
  131. methods: {
  132. getCell(row, column, cell, event) {
  133. console.log("行的内容:",row);
  134. this.tabRowIndex = row.index
  135. this.tabColumnIndex = column.index
  136. },
  137. tableCellClassName({row, column, rowIndex, columnIndex}){//注意这里是解构
  138. //利用单元格的 className 的回调方法,给行列索引赋值
  139. row.index=rowIndex;
  140. column.index=columnIndex;
  141. },
  142. handleBlur(row) {
  143. row.setingFlag = false;
  144. const param={
  145. id:row.id,
  146. powerFactor:row.powerFactor,
  147. currentA:row.currentA,
  148. currentB:row.currentB,
  149. currentC:row.currentC,
  150. voltage:row.voltage,
  151. voltageA:row.voltageA,
  152. voltageB:row.voltageB,
  153. voltageC:row.voltageC,
  154. activePower:row.activePower,
  155. selecttest:row.selecttest,
  156. reactivePower:row.reactivePower,
  157. apparentPower:row.apparentPower,
  158. calculatedPowerFactor:row.calculatedPowerFactor,
  159. energyConsumption:row.energyConsumption
  160. }
  161. this.tabRowIndex = null;
  162. this.tabColumnIndex = null;
  163. console.log("handleBlur========================");
  164. console.log(param);
  165. },
  166. handleRowEdit(index ,row){
  167. this.$set(row, "setingFlag", true)
  168. },
  169. handleDelete( index, row){
  170. console.log(index )
  171. console.log( row)
  172. },
  173. CurrentChange(){
  174. console.log("CurrentChange")
  175. },
  176. }
  177. }
  178. </script>

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

闽ICP备14008679号