当前位置:   article > 正文

动态增删表格

动态增删表格

期望目标:实现一个能通过按钮来动态增加表格栏,每次能添加一行,每行末尾有一个删减按钮。

  1. <el-button type="text" class="primary"
  2. @click="addMember()">+添加</el-button>
  3. <el-table
  4. :data="memberList"
  5. style="width: 100%"
  6. :header-cell-style="{
  7. background: '#fafafa',
  8. color: '#aaa',
  9. fontSize: '15px',
  10. }"
  11. >
  12. <el-table-column prop="index" label="序号" width="60">
  13. <template slot-scope="scope">
  14. <span>{{ getMemberIndex(scope.$index) }}</span>
  15. </template>
  16. </el-table-column>
  17. <el-table-column prop="facilityName" label="设施名称">
  18. <template slot-scope="scope">
  19. <el-select v-model="scope.row.facilityName" placeholder="请选择名称" clearable>
  20. <el-option v-for="dict in dict.type.data_facilities_name" :label="dict.label" :value="dict.value" />
  21. </el-select>
  22. </template>
  23. </el-table-column>
  24. <el-table-column prop="facilitySpecificType" label="具体类型">
  25. <template slot-scope="scope">
  26. <el-select v-model="scope.row.facilitySpecificType" placeholder="请选择类型" clearable>
  27. <el-option v-for="dict in dict.type.data_facilities_type" :label="dict.label" :value="dict.value"/>
  28. </el-select>
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="facilityLocation" label="设施位置">
  32. <template slot-scope="scope">
  33. <el-select v-model="scope.row.facilityLocation" placeholder="请选择位置" clearable>
  34. <el-option v-for="dict in dict.type.data_equipment_location" :label="dict.label" :value="dict.value"/>
  35. </el-select>
  36. </template>
  37. </el-table-column>
  38. <el-table-column prop="facilityTp" label="规格">
  39. <template slot-scope="scope">
  40. <el-input v-model="scope.row.facilityTp"></el-input>
  41. </template>
  42. </el-table-column>
  43. <el-table-column prop="accountabilityUnit" label="责任单位">
  44. <template slot-scope="scope">
  45. <el-input v-model="scope.row.accountabilityUnit"></el-input>
  46. </template>
  47. </el-table-column>
  48. <el-table-column label="操作">
  49. <template slot-scope="scope">
  50. <el-button
  51. size="mini"
  52. type="text"
  53. icon="el-icon-delete"
  54. @click="handleDelete(scope.$index,'memberList')"
  55. >删除
  56. </el-button>
  57. </template>
  58. </el-table-column>
  59. </el-table>
  60. // 添加杆件数据按钮
  61. addMember() {
  62. var member = {
  63. index: this.dataId++,
  64. facilityNo: '',
  65. facilityName: '',
  66. facilitySpecificType: '',
  67. facilityLocation: '',
  68. facilityTp: '',
  69. accountabilityUnit: '',
  70. };
  71. this.memberList.push(member);
  72. },

效果:

遇到的问题一:在删减时,当前所有行的序号需要自动删减,重新计算变更序号

  1. // 删除行数据后序号自动连贯更新
  2. getMemberIndex(index) {
  3. return this.memberList.slice(0, index + 1).length ;
  4. },
  5. /** 删除按钮操作 */
  6. handleDelete(index, listName) {
  7. if (index !== -1 && listName =="memberList") {
  8. this.memberList.splice(index, 1)
  9. }
  10. },

给序号那一栏字段单独重设,动态变更index,<span>{{ getMemberIndex(scope.$index) }}</span>,每次删除一行,调用handleDelete函数,再用getMember重新计算一遍所有序号

如果有数据验证的需求,就需要在table外面再嵌套一层form,因为只有el-form表单猜有数据验证的功能。

      <el-form :model="memberForm" :rules="rules" ref="memberForm"></el-form>

 问题二:

多个表单同时用一个接口提交,在提交数据表单时,后端要求有特殊的数据结构,需要将获取到的多个表单数据重新按要求整合。新设立一个json字段存储

  1. /** 提交按钮 */
  2. async submitForms() {
  3. // 构建数据结构
  4. const facilitiesData = {
  5. pointId: this.pointForm.pointId,
  6. pointName: this.pointForm.pointName,
  7. memberList: this.memberList.map(member => ({
  8. facilityName: member.facilityName,
  9. facilitySpecificType: member.facilitySpecificType,
  10. facilityLocation: member.facilityLocation,
  11. facilityTp: member.facilityTp,
  12. accountabilityUnit: member.accountabilityUnit,
  13. })),
  14. }
  15. // 发送请求添加设施
  16. await addFacilities(facilitiesData).then(response => {
  17. this.$modal.msgSuccess("新增成功");
  18. this.goBack();
  19. })
  20. },

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

闽ICP备14008679号