当前位置:   article > 正文

vue2+element-ui新增编辑表格+删除行

vue2+element-ui新增编辑表格+删除行

实现效果:

 

代码实现 :

  1. <el-table :data="dataForm.updateData"
  2. border
  3. :header-cell-style="{'text-align':'center'}"
  4. :cell-style="{'text-align':'center'}">
  5. <el-table-column label="选项字段"
  6. align="center"
  7. prop="name">
  8. <template slot-scope="scope">
  9. <el-form-item :prop="'updateData.' + scope.$index + '.formName'"
  10. :rules="[
  11. { required: true, message: '请输入', trigger: 'blur' },
  12. { min: 1, max: 20, message: '长度在1到 20个字符', trigger: 'blur' }
  13. ]">
  14. <el-input v-model="scope.row.formName"
  15. clearable></el-input>
  16. </el-form-item>
  17. </template>
  18. </el-table-column>
  19. <el-table-column fixed="right"
  20. label="操作">
  21. <template slot-scope="scope">
  22. <el-button @click.native.prevent="addRow(scope.$index,scope.row,dataForm.updateData)"
  23. type="text"
  24. size="small">
  25. 新增
  26. </el-button>
  27. <el-button @click.native.prevent="deleteRow(scope.$index,scope.row,dataForm.updateData)"
  28. type="text"
  29. size="small"
  30. v-if="dataForm.updateData.length!=1">
  31. 移除
  32. </el-button>
  33. </template>
  34. </el-table-column>
  35. </el-table>
  36. <script>
  37. export default {
  38. data () {
  39. return {
  40. dataForm: {
  41. // 自定义字段
  42. updateData: [
  43. {
  44. // id: '',
  45. formName: ''
  46. }
  47. ]
  48. // 其他...
  49. }
  50. }
  51. },
  52. methods: {
  53. // addRow 新增 自定义字段表格行
  54. addRow (index, rows, item) {
  55. // console.log(index, rows, item)
  56. // this.dataForm.updateData.push({
  57. // // sort: this.dataForm.updateData && this.dataForm.updateData.length > 0 ? this.dataForm.updateData.length + 1 : 1,
  58. // id: null,
  59. // formName: ''
  60. // })
  61. // 数组中添加新元素
  62. item.splice(index + 1, 0, { formName: '' })
  63. },
  64. // deleteRow 删除 自定义字段表格行
  65. deleteRow (index, rows, item) {
  66. // console.log(index, '当前行索引', rows, '删除的目标行')
  67. // 从index这个位置开始删除数组后的1个元素
  68. item.splice(index, 1)
  69. // this.$confirm('删除当前行, 是否继续?', '提示', {
  70. // confirmButtonText: '确定',
  71. // cancelButtonText: '取消',
  72. // type: 'warning'
  73. // }).then(() => {
  74. // item.splice(index, 1)
  75. // // this.delArrId.push(rows.id) // 被删除的id数组集合
  76. // // rows.isDelete = 1
  77. // }).catch(() => {
  78. // this.$message({
  79. // type: 'info',
  80. // message: '已取消删除'
  81. // })
  82. // })
  83. },
  84. }
  85. }
  86. </script>

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

闽ICP备14008679号