vue中表格新增,修改,保存,删除,上移,下移功能实现

代码如下:

<div class="fragment">
      <div class="titleInfo">
        <span>角色关联</span>
        <el-button type="primary" @click="add"  v-if="!isLook">新增节点</el-button>
      </div>
      <el-table
        :data="relNodeList"
        stripe
        border
        highlight-current-row>
        <el-table-column type="index" label="序号" min-width="50" align="center"></el-table-column>
        <el-table-column prop="nodeName" label="节点名称" min-width="80">
           <template slot-scope="scope">
            <span v-if="scope.row.edit"><el-input v-model="scope.row.nodeName" placeholder="请输入节点名称"></el-input></span>
            <span v-else>{{scope.row.nodeName}}</span>
           </template>
        </el-table-column>
        <el-table-column prop="userId" label="关联账号" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.edit"><el-input v-model="scope.row.userId" placeholder="请输入账号信息"></el-input></span>
            <span v-else>{{scope.row.userId}}</span>
           </template>
        </el-table-column>
        <el-table-column prop="userName" label="账号姓名" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.edit"><el-input v-model="scope.row.userName" placeholder="请输入账号姓名"></el-input></span>
            <span v-else>{{scope.row.userName}}</span>
           </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.edit"><el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input></span>
            <span v-else>{{scope.row.remark}}</span>
           </template>
        </el-table-column>
        <el-table-column label="操作" min-width="100px" fixed="right" v-if="!isLook">
          <template slot-scope="scope">
            <el-button type="text" class="tb-btn" size="small" @click="handleEdit(scope)" v-show="!scope.row.edit">修改</el-button>
            <el-button type="text" class="tb-btn" size="small" @click="handleSaveRow(scope)" v-show="scope.row.edit">保存</el-button>
            <el-button type="text" size="small" class="tb-btn" @click="handleDelRow(scope)"><span class="danger-txt">删除</span></el-button>
            <el-button type="text" class="tb-btn" size="small" @click="moveUp(scope.$index)" :disabled="scope.$index === 0">上移</el-button>
            <el-button type="text" class="tb-btn" size="small" @click="moveDown(scope.$index)" :disabled="scope.$index === relNodeList.length - 1">下移</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
add(){
     let length=this.relNodeList.length
     if(length>0&&this.relNodeList[length-1].edit){
       this.$message.info('请先保存上一条')
       return false
     }
      var list = {
                nodeId:'',
                nodeName:'',
                userId: '',
                userName:'',
                remark:'',
                edit:true
         		 };
      this.relNodeList.push(list)
    },
    //修改
    handleEdit(scope){
      scope.row.edit=true;
    },
    //保存
    handleSaveRow(scope){
      if(scope.row.nodeName==''){
        this.$message.error('请输入结点名称')
        return false
      }
      if(scope.row.userId==''){
        this.$message.error('请输入关联账号')
        return false
      }
      if(scope.row.userName==''){
        this.$message.error('请输入关联账号姓名')
        return false
      }
      scope.row.edit=false;
    },
    ///删除
    handleDelRow(scope){
      this.$confirm('确定删除吗', '提示').then(() => {
        this.relNodeList.splice(scope.$index,1);
      })
    },
     moveUp(index){
      if (index > 0) {
        let upDate = this.relNodeList[index - 1];
        this.relNodeList.splice(index - 1, 1);
        this.relNodeList.splice(index, 0, upDate);
      } else {
        this.$message.error('已经是第一条,不可上移');
      }
    },
    moveDown(index){
      if (index + 1 === this.relNodeList.length) {
        this.$message.error('已经是最后一条,不可下移');
      } else {
        let downDate = this.relNodeList[index + 1];
        this.relNodeList.splice(index + 1, 1);
        this.relNodeList.splice(index, 0, downDate);
      }
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~犇犇~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值