当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue+element-ui实现表格编辑的三种实现方式

vue+element-ui实现表格编辑的三种实现方式

2018年11月05日  | 移动技术网IT编程  | 我要评论

黎华安,热血无赖出招表,刘梦婷

1、表格内部显示和编辑切换

这种方式就是利用两个标签显示隐藏来实现,我们这里用input和span,正常用span将数据显示,点击编辑时,将span隐藏,显示input进行编辑。选中当前行我们可以通过slot-scope中的index去实现,在控制显示隐藏的属性上绑定index就可以选中当前行了,如showedit[index]。

页面结构代码:

<el-table
 :data="tabledata"
 tooltip-effect="dark"
 style="width: 100%"
 header-align="center">
 <el-table-column width="50" header-align="center">
  <template slot-scope="{row,$index}">
   <span>{{$index + 1}}</span>
  </template>
 </el-table-column>
 <el-table-column label="名称" prop="name" width="300" header-align="center">
  <template slot-scope="{row,$index}">
   <input class="edit-cell" v-if="showedit[$index]" v-model="row.name">
   <span v-if="!showedit[$index]">{{row.name}}</span>
  </template>
 </el-table-column>
 <el-table-column
  fixed="right"
  label="操作"
  width="100"
  header-align="center">
  <template slot-scope="{row,$index}">
   <el-button type="text" size="small"  @click.native="handleupdate($index, row)"  v-if="showbtn[$index]">更新</el-button>
   <el-button type="text" size="small"  @click.native="handlecancel($index, row)"  v-if="showbtn[$index]">取消</el-button>

   <el-button type="text" size="small"  @click.native="handleedit($index, row)"  v-if="!showbtn[$index]">编辑</el-button>
   <el-button type="text" size="small"  @click.native="handledelete($index, row)"  v-if="!showbtn[$index]">删除</el-button>
  </template>
 </el-table-column>
</el-table>

初始化data:

data() {
 return {
  showedit: [], //显示编辑框
  showbtn: [],
  showbtnordinary: true
 }
}

实现方法:

//点击编辑
handleedit(index, row) {
 this.showedit[index] = true;
 this.showbtn[index] = true;
 this.$set(this.showedit,row,true)
 this.$set(this.showbtn,row,true)
},
//取消编辑
handelcancel(index, row) {
 this.getcontentlist();
 this.showedit[index] = false;
 this.showbtn[index] = false;
   },

//点击更新
handleupdate(formname) {

},
//点击删除
handledelete(index, row) {

},

初始化的时候最好给数组遍历赋值

for(var i = 0; i < 100; i ++) {
 this.showedit[i] = false;
 this.showbtn[i] = false;
 this.$set(vm.showedit[i], false);
 this.$set(vm.showbtn[i], false);
}

另外还可以给row自身添加一个属性,通过row.showedit来控制每一行的编辑。上面说的这些在我的开发环境实现一点问题都没有,但是测试出来了很多bug(没反应、点击第一次第二次没反应等),后来又查询了一下vue的官方文档“异步队列更新”,可能需要加一个vue.nexttick(callback)。项目比较紧换了另外一种实现方式。有兴趣的小伙伴可以看看官方文档:

2、通过弹出另外一个表格编辑

这个是网上找的一篇文章去实现的,原文链接:

另外,github上还有实现的代码,不知道是不是同一个人,为了尊重原创,地址都放在这里:https://github.com/recklesslmz/elementui

这种方式实现就简单多了,初始化table代码同上,但是可以去掉input编辑框,和showedit属性,还需要创建一个隐藏的dialog,里面放另外一张表单:

<el-dialog title="编辑"
 :visible.sync="editformvisible"
 :close-on-click-modal="false"
 class="edit-form"
 :before-close="handleclose">
 <el-form :model="editform" label-width="80px" :rules="editformrules" ref="editform">
  <el-form-item label="名称" prop="name">
   <el-input v-model="editform.name" auto-complete="off"></el-input>
  </el-form-item>
 </el-form>
 <div slot="footer" class="dialog-footer">
  <el-button @click.native="handlecancel('editform')">取消</el-button>
  <el-button type="primary" @click.native="handleupdate('editform')">更新</el-button>
 </div>
</el-dialog>

初始化data:

editformvisible: false, //默认不显示编辑弹层

方法:

//点击编辑
handleedit(index, row) {
 this.editformvisible = true;
 this.editform = object.assign({}, row); //这句是关键!!!
},

//点击关闭dialog
handleclose(done) {
 /*done();
 location.reload();*/
 this.editformvisible = false;
},

//点击取消
handlecancel(formname) {
 this.editformvisible = false;
},

//点击更新
handleupdate(forname) { 
 //更新的时候就把弹出来的表单中的数据写到要修改的表格中
 var postdata = {
  name: this.editform.name;
 }

 //这里再向后台发个post请求重新渲染表格数据
 this.editformvisible = false;
}

3.直接通过样式控制

element-ui中的表格鼠标选中行的时候,行class会自动添加一个current-row,所以通过设置这个class控制编辑和不可编辑标签的显示隐藏。具体参考:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网