当前位置:   article > 正文

Vue+element UI 可编辑表格_vue可编辑表格组件

vue可编辑表格组件

前端开发过程中,我们都会尽量避免多层弹窗的嵌套,最多两层弹窗嵌套。这么做的原因是为了方便管理代码和防止多层嵌套弹窗造成bug。但是有时候,内层的弹窗可能展示了一个表格,并且需要操作表格中的数据,这个时候我们只能自己DIY一个表格,取代弹窗处理的方法。

1,实现效果和逻辑

效果:表格展示数据,并且表格可以编辑数据(增、删、改)。

逻辑:实现可编辑表格的主要思想:使用table创立一个表格,正常展示数据,需要操作某一行数据时,根据行数据的ID作为标志,将该行HTML换成编辑框或者其他form组件。

2,效果图

 2,代码

HTML:

  1. <!--
  2. *
  3. *checkedList:table表格行数据列表
  4. *customCurrentId:编辑某行时,编辑行ID
  5. *item.id !== customCurrentId:控制该行显示HTML内容,不同时显示文本,相同是显示编辑状态
  6. *@click="addOtherForm":新增行数据
  7. *
  8. -->
  9. <table border="1" cellspacing="0" cellpadding="0" class="sinorock-table">
  10. <tr class="bg-style">
  11. <td class="table-label" style="width: 8%;">序号</td>
  12. <td class="table-label" style="width: 20%;">节点名称</td>
  13. <td class="table-label" style="width: 24%;">验收人</td>
  14. <td class="table-label" style="width: 20%;">人员信息</td>
  15. <td class="table-label" style="width: 18%;">方式</td>
  16. <td class="table-label" style="width: 10%;">操作</td>
  17. </tr>
  18. <tr v-for="(item,index) in checkedList" :key="item.id + index" class="bg-style">
  19. <template v-if="item.id !== customCurrentId">
  20. <td style="width: 8%;">{{ index+1 }}</td>
  21. <td style="width: 20%;">{{ item.name }}</td>
  22. <td style="width: 24%;">
  23. <el-radio v-model="item.type" :label="0" disabled>全部人员</el-radio>
  24. <el-radio v-model="item.type" :label="1" disabled>指定人员</el-radio>
  25. </td>
  26. <td style="width: 20%;" v-if="item.type === 1">{{ item.accepterName }}</td>
  27. <td style="width: 20%;" v-if="item.type === 0">全部</td>
  28. <td style="width: 18%;">
  29. <el-radio v-model="item.way" :label="0" disabled>会签</el-radio>
  30. <el-radio v-model="item.way" :label="1" disabled>或签</el-radio>
  31. </td>
  32. <td style="width: 10%;">
  33. <div class="explain-btn">
  34. <el-button type="text" icon="el-icon-edit" style="color:#39A1FF;" @click="customEditHandle(item)"></el-button>
  35. <el-button type="text" icon="el-icon-delete" style="color:#FD7474;" @click="customDeleteHandle(item)"></el-button>
  36. </div>
  37. </td>
  38. </template>
  39. <template v-else>
  40. <td style="width: 8%;">{{ index+1 }}</td>
  41. <td style="width: 20%;">
  42. <el-input type="text" size="mini" placeholder="请输入字段名" v-model="customCurrentObj.name" maxlength="10" clearable></el-input>
  43. </td>
  44. <td style="width: 24%;">
  45. <el-radio v-model="customCurrentObj.type" :label="0" @change="typeChangeHandle">全部人员</el-radio>
  46. <el-radio v-model="customCurrentObj.type" :label="1">指定人员</el-radio>
  47. </td>
  48. <td style="width: 20%;" v-if="customCurrentObj.type === 1">
  49. <el-input
  50. placeholder="请选择"
  51. size="mini"
  52. v-model="customCurrentObj.accepterName"
  53. @focus="selectShow()"
  54. @clear="clearHandle()"
  55. clearable>
  56. </el-input>
  57. </td>
  58. <td style="width: 20%;" v-if="customCurrentObj.type === 0">全部</td>
  59. <td style="width: 18%;">
  60. <el-radio v-model="customCurrentObj.way" :label="0" :disabled="customCurrentObj.type === 0">会签</el-radio>
  61. <el-radio v-model="customCurrentObj.way" :label="1" :disabled="customCurrentObj.type === 0">或签</el-radio>
  62. </td>
  63. <td style="width: 10%;">
  64. <el-button type="text" icon="el-icon-circle-check" style="color:#39A1FF;" @click="customCheckOK(item)"></el-button>
  65. <el-button type="text" icon="el-icon-circle-close" @click="customCheckcancel(item)"></el-button>
  66. </td>
  67. </template>
  68. </tr>
  69. <tr class="bg-style">
  70. <td style="width: 8%;border: 0;"></td>
  71. <td style="width: 20%;border: 0;"></td>
  72. <td style="width: 24%;border: 0;text-align: right;">
  73. <div class="add-check-custom" @click="addOtherForm">
  74. <i class="el-icon-circle-plus-outline" style="color: #39A1FF;"></i>
  75. <span>添加验收节点</span>
  76. </div>
  77. </td>
  78. <td style="width: 20%;border: 0;"></td>
  79. <td style="width: 18%;border: 0;"></td>
  80. <td style="width: 10%;border: 0;"></td>
  81. </tr>
  82. </table>

JavaScript:

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. checkedList: [], // 验收节点列表
  6. customCurrentId: '', // 验收节点编辑时节点ID
  7. customCurrentObj: {}, // 验收节点编辑时节点数据
  8. }
  9. },
  10. methods: {
  11. // 新增验收节点
  12. addOtherForm(){
  13. let num = new Date().getTime()
  14. this.checkedList.push({name:'', type:0, accepter:'', accepterName:'', accepterList: [], way:1, id: 'addNew'+num })
  15. this.customCurrentId = 'addNew'+num
  16. this.customEditHandle(this.checkedList[this.checkedList.length-1])
  17. },
  18. // 删除验收节点
  19. customDeleteHandle(item) {
  20. this.checkedList = this.checkedList.filter((ele) => {
  21. return ele.id !== item.id
  22. })
  23. // 至少保留一条验收节点
  24. if(this.checkedList && this.checkedList.length === 0) {
  25. this.$message({
  26. message: '请至少保留一条验收节点!',
  27. type: 'error',
  28. duration: 1000,
  29. onClose: () => {
  30. this.addOtherForm()
  31. }
  32. })
  33. }
  34. },
  35. // 编辑验收节点
  36. customEditHandle(data){
  37. this.customCurrentId = data.id
  38. try {
  39. this.customCurrentObj = JSON.parse(JSON.stringify(data))
  40. } catch (error) {
  41. }
  42. },
  43. // 修改确认验收节点
  44. customCheckOK(data){
  45. // 验收节点,填写完整的校验
  46. let columsEntityFlag = false
  47. let checkedPersonFlag = false
  48. if(this.customCurrentObj.type === 0) {
  49. checkedPersonFlag = false
  50. }else if(this.customCurrentObj.type === 1 && this.customCurrentObj.accepter === '') {
  51. checkedPersonFlag = true
  52. }
  53. if(!this.customCurrentObj.name || checkedPersonFlag) {
  54. columsEntityFlag = true
  55. }
  56. if(columsEntityFlag) {
  57. this.$message({
  58. message: '请将您添加的验收节点填写完整!',
  59. type: 'error',
  60. })
  61. return false
  62. }
  63. this.checkedList.forEach((item, index) => {
  64. if(item.id === this.customCurrentId) {
  65. try {
  66. this.checkedList[index] = JSON.parse(JSON.stringify(this.customCurrentObj))
  67. } catch (error) {
  68. }
  69. }
  70. })
  71. this.$message({
  72. message: '修改成功',
  73. type: 'success',
  74. duration: 1000,
  75. onClose: () => {
  76. this.customCurrentId = ''
  77. }
  78. });
  79. },
  80. // 取消修改验收节点
  81. customCheckcancel(){
  82. let columsEntityFlag = false
  83. let checkedPersonFlag = false
  84. if(this.customCurrentObj.type === 0) {
  85. checkedPersonFlag = false
  86. }else if(this.customCurrentObj.type === 1 && this.customCurrentObj.accepter === '') {
  87. checkedPersonFlag = true
  88. }
  89. if(!this.customCurrentObj.name || checkedPersonFlag) {
  90. columsEntityFlag = true
  91. }
  92. if(columsEntityFlag) {
  93. this.$message({
  94. message: '请将验收节点填写完整!',
  95. type: 'error',
  96. })
  97. return false
  98. }
  99. this.customCurrentId = ''
  100. },
  101. }
  102. }
  103. </script>

可编辑表格功能就记录到此了,有需要的小伙伴拿去用,希望对你们有所帮助。

这个方法实现可能比较low,谁有更好的方法请多多指导。

拜了个拜,迪迦。。。

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