基于el-table实现行内增删改功能

教程下载地址: 网赚博客https://www.piaodoo.com/创业项目排行榜前十名https://www.piaodoo.com/


实现效果:

在这里插入图片描述

核心代码:

<el-table :data="items"
                  style="width: 100%;margin-top: 16px"
                  border
                  :key="randomKey">
          <el-table-column label="计划名称"
                           property="name">
            <template slot-scope="{row}">
              <template v-if="row.edit">
                <el-input v-model="row.name"></el-input>
              </template>
              <span v-else>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="计划完成时间"
                           property="executionDate" width="175">
            <template slot-scope="scope">
              <el-date-picker v-if="scope.row.edit" style="width: 153px;" type="date"
                              v-model="scope.row.timeEnd">
              </el-date-picker>
              <span v-else>{{ parseTime(scope.row.timeEnd) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="协同人"
                           property="leaderList">
            <template slot-scope="scope">
              <template v-if="scope.row.edit">
                <el-select
                  v-model="scope.row.leaderList"
                  clearable filterable multiple>
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.nickname"
                    :value="item.userId">
                  </el-option>
                </el-select>
              </template>
              <span v-else>{{ scope.row.leaderName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="执行人" width="80">
            <template>
              <span>{{ $store.getters.name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100">
            <template slot-scope="{row,column,$index}">
              <el-button
                v-if="row.edit"
                type="success" icon="el-icon-check" circle
                size="small"
                @click="confirmEdit(row)"
              >
              </el-button>
              <el-button
                v-if="row.edit"
                icon="el-icon-close" circle
                size="small"
                @click="cancelEdit(row)"
              >
              </el-button>
              <el-button
                type="primary" icon="el-icon-edit" circle
                v-if="!row.edit"
                size="small"
                @click="row.edit=!row.edit"
              >
              </el-button>
              <el-button
                type="danger" icon="el-icon-delete" circle
                size="small" @click="handleDelete($index)"
                v-if="!row.edit"
              >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="display: flex;margin-top: 28px;justify-content: center;">
        <el-button @click="addSon" size="small" icon="el-icon-plus">添加子计划</el-button>
      </div>

Method:

cancelEdit(row) {
      row.name = row.oldName
      row.leaderList = row.oldLeaderList
      row.timeEnd = row.oldTimeEnd
      row.leaderName = row.oldLeaderName
      row.edit = false
      this.$message({
        message: '已恢复原值',
        type: 'warning'
      })
    },
    confirmEdit(row) {
      row.edit = false
      row.oldName = row.name
      row.oldLeaderList = row.leaderList
      row.oldTimeEnd = row.timeEnd
      let arr = []
      row.leaderList.forEach(i => {
        let userName = this.userList.find(({userId}) => userId === i).nickname
        arr.push(userName)
      })
      row.leaderName = arr.join()
      row.oldLeaderName = row.leaderName
      this.$message({
        message: '修改成功',
        type: 'success'
      })
    },
    handleDelete(index) {
      this.items.splice(index, 1)
    },
    addSon() {
      this.items.push({
        name: null,
        timeEnd: null,
        leaderList: [],
        leaderName: null,
        edit: true
      })
    },

到此这篇关于基于el-table实现行内增删改的文章就介绍到这了,更多相关el-table行内增删改内容请搜索网赚博客https://www.piaodoo.com/以前的文章或继续浏览下面的相关文章希望大家以后多多支持网赚博客https://www.piaodoo.com/!

                        友情连接:  

茂名一技http://www.szsyby.net/


茂名一技http://www.enechn.com/


美文集http://www.tpyjn.cn/


手游排行前十名http://www.bjkhrx.com/


蔚来汽车http://www.weilaiqiche.cn/

### Element UI 的 `el-table` 组件实现行内编辑 为了实现在 `el-table` 中的行内编辑功能,可以通过监听特定事件并控制每一的状态来切换输入模式和查看模式。下面是一个完整的示例说明如何通过点击编辑按钮触发行内编辑。 #### HTML 结构 ```html <template> <div id="app"> <el-table :data="tableData" style="width: 100%"> <!-- 定义表格列 --> <el-table-column label="日期" width="180"> <template slot-scope="scope"> <span v-if="!scope.row.edit">{{ scope.row.date }}</span> <el-input v-else v-model="scope.row.date"></el-input> </template> </el-table-column> <el-table-column label="姓名" width="180"> <template slot-scope="scope"> <span v-if="!scope.row.edit">{{ scope.row.name }}</span> <el-input v-else v-model="scope.row.name"></el-input> </template> </el-table-column> <el-table-column label="地址"> <template slot-scope="scope"> <span v-if="!scope.row.edit">{{ scope.row.address }}</span> <el-input v-else v-model="scope.row.address"></el-input> </template> </el-table-column> <el-table-column fixed="right" label="操作" width="120"> <template slot-scope="scope"> <el-button @click.prevent="editRow(scope.$index)" type="text">编辑</el-button> <el-button @click.prevent="saveRow(scope.$index)" type="text">保存</el-button> </template> </el-table-column> </el-table> </div> </template> ``` #### JavaScript 部分 ```javascript <script> export default { data() { return { tableData: [ { date: '2016-05-03', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', edit: false }, { date: '2016-05-02', name: 'John', address: 'New York No. 1 Lake Park', edit: false } ] }; }, methods: { // 编辑当前 editRow(index) { this.tableData[index].edit = true; }, // 保存修改后的数据 saveRow(index) { this.tableData[index].edit = false; console.log('Updated Data:', this.tableData); // 此处可加入API调用来提交更改到服务器 } } }; </script> ``` 此代码片段展示了怎样利用 Vue.jsElement UI 来创建一个支持行内编辑的表格[^1]。当用户点击“编辑”按钮时,对应进入编辑状态;而点击“保存”则会结束编辑并将新值应用回原始对象上。此外,在实际项目中可能还需要考虑验证逻辑以及向后台发送更新请求等功能扩展[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值