当前位置: 首页 > 编程笔记 >

Vue.js实现可编辑的表格

宗政鸿志
2023-03-14
本文向大家介绍Vue.js实现可编辑的表格,包括了Vue.js实现可编辑的表格的使用技巧和注意事项,需要的朋友参考一下

本文实例为大家分享了Vue.js实现可编辑的表格的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
<head>
 <title></title>
 <link rel="stylesheet" type="text/css" href="bootstrap.min.css" >
 <style type="text/css">
   table tr td{
   text-align: center;
   }
  .btn-info{
   margin-left: 5px;
  }

  .add,.addBox{
   margin: 10px 0px 10px 10px;
  }
  .submit{
   margin-left: 172px;
  }
  /*全删*/
  .delAll{
   margin-left: 10px;
  }
  /*新增*/
  fieldset{
   margin-left: 10px;
  }
 </style>
</head>
<body>

<div id="app">
<button class="btn btn-primary btn-sm addBox" @click="addBox">添加</button>
<button class="btn btn-sm btn-danger delAll" @click="delAll">批量删除</button>
 <table class="table table-bordered" >
 <thead>
 <tr>
  <td><input type="checkbox" @click="allSelect" v-model="checked"></td>
  <td>学号</td>
  <td>姓名</td>
  <td>年纪</td>
  <td>操作</td>

 </tr>
 </thead>
 <tbody>
  <tr v-for="person,index in people">
  <td><input type="checkbox" v-model="selected" v-bind:value="person.sid"></td>
  <td @click="edit(index)" contenteditable="true">{{person.sid}}</td>
  <td @click="edit(index)" contenteditable="true">{{person.sname}}</td>
  <td @click="edit(index)" contenteditable="true">{{person.sage}}</td>
  <td><button @click="del(index)" class="btn btn-danger btn-sm">删除</button><button @click="update(index)" class="btn btn-info btn-sm">编辑</button></td>
  </tr>
 </tbody>

 </table>

<fieldset v-show="seen" >
 <legend>新增用户</legend>
 <div class="">
 <p>
  <label>学号:</label>
  <input type="text" v-model="newPeople.sid">
 </p>
 <p>
  <label>姓名:</label>
  <input type="text" v-model="newPeople.sname">
 </p>
 <p>
  <label>年龄:</label>
  <input type="number" v-model="newPeople.sage">
 </p>
 <p>
  <button class="btn btn-success btn-sm submit" @click="add">提交</button>
 </p>
</div>


</fieldset> 
<!-- 编辑界面 -->
<fieldset v-show="editSeen">
 <legend>编辑用户</legend>


<div class="">
 <p>
  <label>学号:</label>
  <input type="text" v-model="editPeople.sid" value="{{sid}}">
 </p>
 <p>
  <label>姓名:</label>
  <input type="text" v-model="editPeople.sname" value="{{sname}}">
 </p>
 <p>
  <label>年龄:</label>
  <input type="number" v-model="editPeople.sage" value="{{sage}}">
 </p>
 <p>
  <button class="btn btn-success btn-sm submit" @click="editSubmit">提交</button>
 </p>
</div>
</fieldset> 
</div>


 <script type="text/javascript" src="vue.min.js"></script>
 <script type="text/javascript">
 var data ={
    people:[ 
     {'sid':'1043','sname':'张三','sage':18}, 
     {'sid':'2434','sname':'赵六','sage':43}, 
     {'sid':'3424','sname':'李四','sage':42}, 
     {'sid':'1231','sname':'王五','sage':35} 
    ],
    newPeople:{
     'sid':'','sname':'','sage':''
    },
    seen:false,
    editSeen:false,
    checked:false,
    selected:[],
    editPeople:{
     'sid':'','sname':'','sage':''
    }

   } ;
  var app = new Vue({
  'el':'#app',
   data:data,

   methods:{
   // 添加
   addBox:function(){
    this.seen = this.seen == false ? true : false;
   },
   //删除
   del:function(index){
    console.log(11);
    this.people.splice(index,1);
   },
   //提交
   add:function(){
     //插入到people中
     this.people.push(this.newPeople);

     this.newPeople = {};
     this.seen = false
   },
   //全选
   allSelect:function(){
    if(this.selected.length != this.people.length){
     this.selected = [];
     for(var i = 0; i<this.people.length;i++){
      this.selected.push(this.people[i].sid);
      console.log(this.people[i].sid);

     }

    }else{
     this.selected = [];
    }

   },
   //批量删除
   delAll:function(){
    for(var j = 0; j< this.selected.length;j++){
     for(var i = 0; i< this.people.length; i++){ 
      if(this.selected[j] == this.people[i].sid){
       this.people.splice(i,1);
      }
     }
    }

   },
    //编辑
    update:function(index){
    this.editSeen = true;
     this.editPeople = this.people[index];



    },
    //编辑后提交
    editSubmit:function(){
    this.editSeen = false;

    }

   },
   watch:{
   "selected":function(){
    if(this.selected.length == this.people.length){
     this.checked = true;
    }else{
     this.checked = false;
    }
   }
   }
  })
 </script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • vue-vuetify怎么实现表格单元格可编辑,并且能获取到输入的值呢?

  • 本文向大家介绍JQuery实现可直接编辑的表格,包括了JQuery实现可直接编辑的表格的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JQuery实现可直接编辑的表格。分享给大家供大家参考。具体分析如下: 功能: 创建一个表格,用户单击某个单元格后,可以直接修改单元格文本。 在编辑状态下,用户可按回车键确认修改,按ESC键撤销修改。 效果如下图: 思路: 当用户点击某个单元格后,立即向该单

  • 本文向大家介绍jQuery实现的可编辑表格完整实例,包括了jQuery实现的可编辑表格完整实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了jQuery实现的可编辑表格。分享给大家供大家参考,具体如下: 更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery表格(table)操作技巧汇总》、《jQuery常用插件及用法总结》、《jquery中Ajax用法总结》、《jQue

  • 本文向大家介绍利用jQuery实现可以编辑的表格,包括了利用jQuery实现可以编辑的表格的使用技巧和注意事项,需要的朋友参考一下 今天学习了利用jQuery实现可以编辑的表格这个例子。这个例子需求是这样的:在前台的表格中单击单元格便可修改其中的内容,回车键保存修改的内容,esc撤销保存的内容。原理:单击客户端表格单元格时,在单元格中添加一个文本框,并将单元格中原来的内容赋值给文本框,再进一步去修

  • 本文向大家介绍jQuery实现可编辑的表格实例讲解(2),包括了jQuery实现可编辑的表格实例讲解(2)的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了jQuery实现可编辑表格的具体代码,供大家参考,具体内容如下 我们最终要达到的效果如下: 当单击学号列的时候,可以进行编辑: 当单击ESC的时候,操作取消,当单击回车的时候,修改生效(没有与后台交互) 页面代码如下(asp.net

  • 本文向大家介绍BootStrap和jQuery相结合实现可编辑表格,包括了BootStrap和jQuery相结合实现可编辑表格的使用技巧和注意事项,需要的朋友参考一下 editTable.js 提供编辑表格当前行、添加一行、删除当前行的操作,其中可以设置参数,如: operatePos 用于设置放置操作的列,从0开始,-1表示以最后一列作为放置操作的列;(这里的操作包括 编辑当前行、在当前行下添加