当前位置:   article > 正文

vue el-table字段点击出现el-input输入框,失焦保存_el-table @input

el-table @input

一、效果展示

  • 当没有数据初始化展示如下:

  • 有数据展示数据,点击出现输入框, 失焦保存修改

二、代码实现

  1. <!-- @cell-click="cellClick" 当前单击的单元格 -->
  2. <el-table
  3. ref="table"
  4. size="mini"
  5. height="100%"
  6. row-key="id"
  7. :data="tableData"
  8. :row-class-name="getRowClass"
  9. @cell-click="cellClick"
  10. >
  11. <el-table-column width="100" label="排序" show-overflow-tooltip>
  12. <template slot-scope="scope">
  13. <span
  14. v-if="
  15. scope.row.index === dbClickIndex &&
  16. dbClickLabel === '排序'
  17. "
  18. >
  19. <el-input
  20. ref="sortNumRef"
  21. v-model="sortNum"
  22. placeholder="请输入"
  23. @blur="inputBlur(scope.row)"
  24. :pattern="numberPattern"
  25. />
  26. </span>
  27. <div v-else>
  28. <div class="flex_between cursor_pointer">
  29. <span
  30. :style="{ color: !scope.row.sortNum ? '#bbb' : '' }"
  31. >{{ scope.row.sortNum || '请输入' }}</span
  32. >
  33. <i class="el-icon-edit" style="color: #1989fe"></i>
  34. </div>
  35. </div>
  36. </template>
  37. </el-table-column>
  38. </el-table>
  39. data() {
  40. return {
  41. sortNum: null,
  42. dbClickIndex: null, // 点击的单元格
  43. dbClickLabel: '', // 当前点击的列名
  44. numberPattern: '[1-9]\\d*', // 正则表达式,限制只能输入正整数
  45. }
  46. }
  47. methods:{
  48. // 把每一行的索引放进row
  49. getRowClass({ row, rowIndex }) {
  50. row.index = rowIndex
  51. },
  52. // row 当前行 column 当前列
  53. cellClick(row, column, cell, event) {
  54. if (column.label === '排序') {
  55. this.dbClickIndex = row.index
  56. this.dbClickLabel = column.label
  57. this.sortNum = row.sortNum
  58. //聚焦input
  59. this.$nextTick(() => {
  60. this.$refs.sortNumRef.focus()
  61. })
  62. }
  63. },
  64. // 失去焦点初始化
  65. inputBlur(row, event, column) {
  66. this.editThemeIndex(row)
  67. this.dbClickIndex = null
  68. this.dbClickLabel = ''
  69. this.sortNum = null
  70. },
  71. // 编辑主题指标列表-排序字段
  72. editThemeIndex(row) {
  73. if (this.sortNum === '' || this.sortNum === row.sortNum) return
  74. const params = {
  75. sortNum: Number(this.sortNum) || '',
  76. id: row.id
  77. }
  78. //接口请求
  79. xxxApi(params).then((res) => {
  80. if (res.code === 200) {
  81. this.$message.success('修改成功')
  82. this.refreshClick()
  83. }
  84. })
  85. },
  86. // 刷新
  87. refreshClick(val) {
  88. this.pages.pageIndex = 1
  89. this.initTable()
  90. }
  91. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/96509
推荐阅读
相关标签
  

闽ICP备14008679号