动态增删表格

文章描述了如何使用Vue.js实现一个可以动态添加表格行的组件,包括添加新行、删除行时序号的自动调整以及数据验证的需求。同时提及了如何处理多表单通过同一接口提交时的数据整合问题。
摘要由CSDN通过智能技术生成

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

<el-button type="text" class="primary"
         @click="addMember()">+添加</el-button>
<el-table
        :data="memberList"
        style="width: 100%"
        :header-cell-style="{
          background: '#fafafa',
          color: '#aaa',
          fontSize: '15px',
      }"
      >
        <el-table-column prop="index" label="序号" width="60">
          <template slot-scope="scope">
            <span>{{ getMemberIndex(scope.$index) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="facilityName" label="设施名称">
          <template slot-scope="scope">
            <el-select v-model="scope.row.facilityName" placeholder="请选择名称" clearable>
              <el-option v-for="dict in dict.type.data_facilities_name" :label="dict.label" :value="dict.value"  />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="facilitySpecificType" label="具体类型">
          <template slot-scope="scope">
            <el-select v-model="scope.row.facilitySpecificType" placeholder="请选择类型" clearable>
              <el-option v-for="dict in dict.type.data_facilities_type" :label="dict.label" :value="dict.value"/>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="facilityLocation" label="设施位置">
          <template slot-scope="scope">
            <el-select v-model="scope.row.facilityLocation" placeholder="请选择位置" clearable>
              <el-option v-for="dict in dict.type.data_equipment_location" :label="dict.label" :value="dict.value"/>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="facilityTp" label="规格">
          <template slot-scope="scope">
            <el-input v-model="scope.row.facilityTp"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="accountabilityUnit" label="责任单位">
          <template slot-scope="scope">
            <el-input v-model="scope.row.accountabilityUnit"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index,'memberList')"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

// 添加杆件数据按钮
    addMember() {
      var member = {
        index: this.dataId++,
        facilityNo: '',
        facilityName: '',
        facilitySpecificType: '',
        facilityLocation: '',
        facilityTp: '',
        accountabilityUnit: '',
      };
      this.memberList.push(member);
    },

效果:

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


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

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

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

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

 问题二:

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

/** 提交按钮 */
    async submitForms() {
      // 构建数据结构
      const facilitiesData = {
        pointId: this.pointForm.pointId,
        pointName: this.pointForm.pointName,
        memberList: this.memberList.map(member => ({
          facilityName: member.facilityName,
          facilitySpecificType: member.facilitySpecificType,
          facilityLocation: member.facilityLocation,
          facilityTp: member.facilityTp,
          accountabilityUnit: member.accountabilityUnit,
        })),
      }
      // 发送请求添加设施
      await addFacilities(facilitiesData).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.goBack();
      })
    },

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 可以使用jQuery和HTML表格来实现动态增删改查功能。以下是一个简单的示例: HTML代码: ``` <table id="myTable"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>John</td> <td>25</td> <td> <button class="edit-btn">Edit</button> <button class="delete-btn">Delete</button> </td> </tr> <tr> <td>2</td> <td>Jane</td> <td>30</td> <td> <button class="edit-btn">Edit</button> <button class="delete-btn">Delete</button> </td> </tr> </tbody> </table> <button id="add-btn">Add Row</button> ``` jQuery代码: ``` // Add Row $('#add-btn').click(function() { var newRow = '<tr>' + '<td></td>' + '<td><input type="text"></td>' + '<td><input type="text"></td>' + '<td>' + '<button class="save-btn">Save</button>' + '<button class="cancel-btn">Cancel</button>' + '</td>' + '</tr>'; $('#myTable tbody').append(newRow); }); // Edit Row $(document).on('click', '.edit-btn', function() { var row = $(this).closest('tr'); var id = row.find('td:eq(0)').text(); var name = row.find('td:eq(1)').text(); var age = row.find('td:eq(2)').text(); var editRow = '<tr>' + '<td>' + id + '</td>' + '<td><input type="text" value="' + name + '"></td>' + '<td><input type="text" value="' + age + '"></td>' + '<td>' + '<button class="update-btn">Update</button>' + '<button class="cancel-btn">Cancel</button>' + '</td>' + '</tr>'; row.replaceWith(editRow); }); // Update Row $(document).on('click', '.update-btn', function() { var row = $(this).closest('tr'); var id = row.find('td:eq(0)').text(); var name = row.find('td:eq(1) input').val(); var age = row.find('td:eq(2) input').val(); var updateRow = '<tr>' + '<td>' + id + '</td>' + '<td>' + name + '</td>' + '<td>' + age + '</td>' + '<td>' + '<button class="edit-btn">Edit</button>' + '<button class="delete-btn">Delete</button>' + '</td>' + '</tr>'; row.replaceWith(updateRow); }); // Cancel Edit $(document).on('click', '.cancel-btn', function() { var row = $(this).closest('tr'); var id = row.find('td:eq(0)').text(); var name = row.find('td:eq(1) input').val(); var age = row.find('td:eq(2) input').val(); var cancelRow = '<tr>' + '<td>' + id + '</td>' + '<td>' + name + '</td>' + '<td>' + age + '</td>' + '<td>' + '<button class="edit-btn">Edit</button>' + '<button class="delete-btn">Delete</button>' + '</td>' + '</tr>'; row.replaceWith(cancelRow); }); // Delete Row $(document).on('click', '.delete-btn', function() { $(this).closest('tr').remove(); }); ``` 该代码包括四个操作:增加行、编辑行、更新行和删除行。当用户单击“Add Row”按钮时,将添加一行空白行。当用户单击“Edit”按钮时,将行转换为可编辑行。用户可以更新行或取消编辑。当用户单击“Delete”按钮时,将删除行。 ### 回答2: jQuery是一个非常流行的JavaScript库,它可以简化和优化开发过程,包括对表格动态增删改查操作。 动态增加表格行: 使用jQuery可以很方便地在表格动态添加行。可以使用append()方法将新的行元素插入到表格最后一行,也可以使用prepend()方法将新的行元素插入到表格的第一行。 动态删除表格行: 通过指定删除按钮的点击事件,可以使用jQuery的remove()方法删除对应的表格行。可以通过获取到按钮所在行的父元素,然后调用remove()方法来删除整行。 动态修改表格内容: 使用jQuery可以很容易地找到需要修改的表格元素,并使用text()或html()方法来修改表格内容。可以通过获取到特定元素或者行的选择器,然后使用text()方法替换相应的内容。 动态查询表格行: 可以使用jQuery的filter()方法来过滤表格行。通过定义一个条件,可以使用filter()方法筛选符合条件的行,并将其展示出来,或者隐藏掉其他不符合条件的行。 总之,使用jQuery可以轻松实现表格动态增删改查操作。通过选择器可以找到特定的表格元素,然后使用各种方法来对其进行操作。这样可以大大简化开发过程,并提高开发效率。 ### 回答3: 使用jQuery可以轻松地实现表格动态增加删除、修改和查询功能。 1. 增加行:使用`append()`方法可以直接在表格末尾添加一行,使用`prepend()`方法可以在表格开头添加一行。 2. 删除行:使用`remove()`方法可以删除指定行,例如点击删除按钮时,可以使用`$(this).closest('tr').remove();`将当前行删除。 3. 修改行:通过遍历表格的每一行,使用`find()`方法找到要修改的单元格,并使用`text()`或`html()`方法修改其内容,或使用`val()`方法修改表单元素的值。 4. 查询行:通过遍历表格的每一行,使用`find()`方法找到要查询的单元格,并使用`text()`或`html()`方法获取其内容,然后进行比较判断。 以上就是简单的jQuery实现表格动态增删改查的方法。可以结合事件监听和条件判断等方法实现更复杂的功能,例如添加表单验证、筛选条件等。使用jQuery可以快速、简洁地实现表格动态操作,提升用户体验。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值