当前位置:   article > 正文

el-table 实现可编辑表格_el-table-column可编辑

el-table-column可编辑

效果如图

代码

  1. <template>
  2. <div>
  3. <el-table
  4. v-loading="listLoading"
  5. :data="list"
  6. row-key="id"
  7. :has-n-o="false"
  8. :cell-class-name="tableCellClassName"
  9. @sort-change="sortChange"
  10. @row-dblclick="dbclick"
  11. >
  12. <el-table-column fixed prop="processid" label="编号" align="left" width="150">
  13. <template slot-scope="scope">
  14. <el-input
  15. v-if="scope.row.index + ',' + scope.column.index == currentCell"
  16. :ref="scope.row.index + ',' + scope.column.index"
  17. v-model="scope.row.processid"
  18. @blur="hideInput(scope.row)"
  19. />
  20. <span v-else>{{ scope.row.processid }}</span>
  21. </template>
  22. </el-table-column>
  23. <el-table-column prop="processtype_name" label="类别" align="left" width="100">
  24. <template slot-scope="scope">
  25. <el-select
  26. v-if="scope.row.index + ',' + scope.column.index == currentCell"
  27. v-model="scope.row.processtype"
  28. >
  29. <el-option
  30. v-for="(item, index) in processtypeOptions"
  31. :key="index"
  32. :label="item.fullName"
  33. :value="item.enCode"
  34. />
  35. </el-select>
  36. <span v-else>{{ scope.row.processtype_name }}</span>
  37. </template>
  38. </el-table-column>
  39. </el-table>
  40. </div>
  41. </template>
  42. <script>
  43. export default {
  44. data() {
  45. return {
  46. list: [], // 数据
  47. // 用一个字符串来保存当前双击的是哪一个单元格
  48. currentCell: null,
  49. }
  50. },
  51. methods: {
  52. // 给单元格绑定横向和竖向的index,这样就能确定是哪一个单元格
  53. tableCellClassName({ row, column, rowIndex, columnIndex }) {
  54. row.index = rowIndex
  55. column.index = columnIndex
  56. },
  57. // 获得当前双击的单元格的横竖index,然后拼接成一个唯一字符串用于判断,并赋给currentCell
  58. // 拼接后类似这样:"1,0","1,1",
  59. dbclick(row, column) {
  60. this.currentCell = row.index + ',' + column.index
  61. // input 自动获取焦点
  62. if (column.property === 'processid_name' || column.property === 'processmoudel_name') {
  63. // 双击后自动获得焦点
  64. this.$nextTick(() => {
  65. this.$refs[row.index + ',' + column.index].focus()
  66. })
  67. }
  68. },
  69. // 关闭编辑状态
  70. hideInput(row) {
  71. this.currentCell = null
  72. }
  73. }
  74. }
  75. </script>

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

闽ICP备14008679号