vuejs element table 表格添加行,修改,单独删除行,批量删除行操作


Posted in Javascript onJuly 18, 2020

1.表格动态添加,也可删除

<template>
 <div class="TestWord">
   <el-button @click="addLine">添加行数</el-button>
   <el-button @click="save">保存</el-button>
    <el-table
     :data="tableData"
     style="width: 100%">
     <el-table-column prop="bookname" label="书名">
       <template slot-scope="scope">
       <el-input v-model="scope.row.bookname" placeholder="书名"></el-input>
       </template>
     </el-table-column>
     <el-table-column prop="bookvolume" label="册数">
       <template slot-scope="scope">
       <el-input v-model="scope.row.bookvolume" placeholder="册数"></el-input>
       </template>
     </el-table-column>
     <el-table-column prop="bookbuyer" label="购买者">
       <template slot-scope="scope">
 
       <el-input v-model="scope.row.bookbuyer" placeholder="购买者"></el-input>
       </template>
     </el-table-column>
     <el-table-column prop="bookborrower" label="借阅者">
       <template slot-scope="scope">
       <el-input v-model="scope.row.bookborrower" placeholder="借阅者"></el-input>
       </template>
     </el-table-column>
     <el-table-column prop="bookbuytime" label="购买日期">
       <template slot-scope="scope">
        <el-date-picker
         v-model="scope.row.bookbuytime"
         type="date"
         format="yyyy-MM-dd"
         value-format="yyyy-MM-dd"
         placeholder="购买日期">
        </el-date-picker>
       </template>
     </el-table-column>
     <el-table-column prop="bookbuytime" label="购买日期">
       <template slot-scope="scope">
        <el-button
         size="mini"
         type="danger"
         v-if="!scope.row.editing"
         icon="el-icon-delete"
         @click="handleDelete(scope.$index, scope.row)">删除
        </el-button>
       </template>
     </el-table-column>
  </el-table>
 </div>
</template>

vuejs 代码

export default {
  name:'TestWorld',
  data() {
    return {
      tableData:[{
        bookname: '',
        bookbuytime: '',
        bookbuyer: '',
        bookborrower: '',
        bookvolume:''
      }]
    }
  },
  methods:{
    addLine(){ //添加行数
      var newValue = {
         bookname: '',
         bookbuytime: '',
         bookbuyer: '',
         bookborrower: '',
         bookvolume:''
       };
      //添加新的行数
      this.tableData.push(newValue);
    },
    handleDelete(index){ //删除行数
      this.tableData.splice(index, 1)
    },
    save(){
     //这部分应该是保存提交你添加的内容
     console.log(JSON.stringify(this.tableData))
    }
  }
 
}

运行图片

vuejs element table 表格添加行,修改,单独删除行,批量删除行操作

2.编辑表格 (即使input已经修改过,当点击取消时,内容不会变)

<template>
  <div class="TestWorld">
   <el-button @click="savemodify">保存</el-button>
    <el-table
     :data="modifyData"
     style="width: 100%">
     <el-table-column prop="bookname" label="书名">
      <template slot-scope="scope">
 	     	<template v-if="scope.row.editing">
 	      	<el-input class="edit-input" v-model="scope.row.bookname" placeholder="书名"></el-input>
 	     	</template>
 	     	<span v-else>{{ scope.row.bookname }}</span>
 	    </template>
     </el-table-column>
     <el-table-column prop="bookvolume" label="册数">
       <template slot-scope="scope">
        <template v-if="scope.row.editing">
         <el-input class="edit-input" v-model="scope.row.bookvolume" placeholder="册数"></el-input>
        </template>
        	<span v-else>{{ scope.row.bookvolume}}</span>
       </template>
     </el-table-column>
     <el-table-column prop="bookbuyer" label="购买者">
       <template slot-scope="scope">
        <template v-if="scope.row.editing">
         <el-input class="edit-input" v-model="scope.row.bookbuyer" placeholder="购买者"></el-input>
        </template>
        <span v-else>{{scope.row.bookbuyer}}</span>
       </template>
     </el-table-column>
     <el-table-column prop="bookborrower" label="借阅者">
       <template slot-scope="scope">
        <template v-if="scope.row.editing">
         <el-input class="edit-input" v-model="scope.row.bookborrower" placeholder="借阅者"></el-input>
        </template>
        <span v-else>{{scope.row.bookborrower}}</span>
       </template>
     </el-table-column>
     <el-table-column prop="bookbuytime" label="购买日期">
       <template slot-scope="scope">
        <template v-if="scope.row.editing">
         <el-date-picker
          v-model="scope.row.bookbuytime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="购买日期">
         </el-date-picker>
        </template>
       <span v-else>{{scope.row.bookbuytime}}</span>
       </template>
     </el-table-column>
     <el-table-column prop="editing" label="操作">
       <template slot-scope="scope">
        <el-button
         type="danger"
         v-if="!scope.row.editing"
         icon="el-icon-delete"
         v-model="scope.$index"
         @click="handleEdit(scope.$index, scope.row)">编辑
        </el-button>
        <el-button
         v-else
         type="danger"
         icon="el-icon-delete"
         v-model="scope.$index"
         @click="handleCancle(scope.$index, scope.row)">取消
        </el-button>
       </template>
     </el-table-column>
    </el-table>
  </div>
</template>

vuejs 代码

export default {
  name:'TestWorld',
  data() {
    return {
       modifyData:[],
       prevValue:{}
    }
  },
  mounted(){
    this.getData()
  },
  methods:{
    getData(){
      this.$ajax({
        method: 'get',
        url:'../static/json/1.1.1.json', //<---本地地址
        //url: '/api/drummer/8bd17859',
      }).then((response)=>{
        console.log(JSON.stringify(response.data))
 
        let _data = response.data;
        let datalength = _data.length;
        for(let i = 0;i < datalength; i++){
          this.$set(_data[i], 'editing', false)
        }
        //赋值
        this.modifyData = _data;
          
       }).catch(function(err){
         console.log(err)
       })
    },
    handleEdit(index,row){
     row.editing = true;
     console.log(index)
     this.prevValue = JSON.parse(JSON.stringify(row));
    },
    handleCancle(index,row){
     row.editing = false;
     let prevContent = this.prevValue.bookname;
     this.$set(row,"bookname",prevContent);
    },
    savemodify(){
     console.log(JSON.stringify(this.modifyData))
    }
  }
 
}

本地的1.1.1.JSON数据

[{"bookname":"普通高等教育物联网工程专业规划用书:物联网技术概论","bookbuytime": "2016-05-04","bookbuyer": "李晓月","bookborrower": "王小虎","bookvolume":"1"},{"bookname":"区块链革命:比特币底层技术如何改变货币、商业和世界","bookbuytime": "2016-05-04","bookbuyer": "李晓月","bookborrower": "李小虎","bookvolume":"1"},{"bookname":"大家一起学配色:数学色彩设计全能书","bookbuytime": "2017-12-04","bookbuyer": "张晓月","bookborrower": "王而虎","bookvolume":"1"}]

如果不用get本地数据,vuejs如下

export default {
  name:'TestWorld',
  data() {
    return {
       modifyData:[
          {
            bookname: '普通高等教育物联网工程专业规划用书:物联网技术概论',
            bookbuytime: '2016-05-04',
            bookbuyer: '李晓月',
            bookborrower: '王小虎',
            bookvolume: '1',
            editing: false
          },
          {
            bookname: '区块链革命:比特币底层技术如何改变货币、商业和世界',
            bookbuytime: '2016-05-04',
            bookbuyer: '李晓月',
            bookborrower: '李小虎',
            bookvolume: '1',
            editing: false
          },
          {
            bookname: '大家一起学配色:数学色彩设计全能书',
            bookbuytime: '2017-12-04',
            bookbuyer: '张晓月',
            bookborrower: '王而虎',
            bookvolume: '1',
            editing: false
          }
        ],
       prevValue:{}
    }
  },
  methods:{
    handleEdit(index,row){ //编辑
     row.editing = true;
     console.log(index)
     this.prevValue = JSON.parse(JSON.stringify(row));
    },
    handleCancle(index,row){ //取消
     row.editing = false;
     let prevContent = this.prevValue.bookname;
     this.$set(row,"bookname",prevContent);
    },
    savemodify(){
     console.log(JSON.stringify(this.modifyData))
    }
  }
 
}

运行图

vuejs element table 表格添加行,修改,单独删除行,批量删除行操作

3.批量删除行数

<template>
  <div class="TestWorld">
    <el-table ref="multipleTable" :data="tableData3" tooltip-effect="dark"   style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="日期" width="120">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="address" label="地址" show-overflow-tooltip>
      </el-table-column>
   </el-table>
   <div style="margin-top: 20px">
     <el-button @click="batchDelete">批量删除</el-button>
     <el-button @click="toggleSelection()">取消选择</el-button>
   </div>
  </div>
</template>

vuejs 代码

export default {
  name:'TestWorld',
  data() {
    return {
        tableData3: [
          {
           date: '2016-05-03',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
          }, 
          {
           date: '2016-05-02',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
          },
          {
           date: '2016-05-04',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
          },
          {
           date: '2016-05-01',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
          },
          {
           date: '2016-05-08',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
          },{
           date: '2016-05-06',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
          },{
           date: '2016-05-07',
           name: '王小虎',
           address: '上海市普陀区金沙江路 1518 弄'
          }],
          multipleSelection: []
    }
  },
  methods:{
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row);
      });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
     },
     batchDelete(){
       let multData = this.multipleSelection;
       let tableData =this.tableData3;
       let multDataLen = multData.length;
       let tableDataLen = tableData.length;
       for(let i = 0; i < multDataLen ;i++){ 
         for(let y=0;y < tableDataLen;y++){
           if(JSON.stringify(tableData[y]) == JSON.stringify(multData[i])){ //判断是否相等,相等就删除
            this.tableData3.splice(y,1)
            console.log("aa")
           }
         }
       }
     },
     handleSelectionChange(val) {
      this.multipleSelection = val;
     }
  }
 
}

有关验证的代码,看上面,持续更新~

以上这篇vuejs element table 表格添加行,修改,单独删除行,批量删除行操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
删除select中所有option选项jquery代码
Aug 12 Javascript
返回上一页并自动刷新的JavaScript代码
Feb 19 Javascript
javascript实现表单验证
Jan 29 Javascript
jquery实现无刷新验证码的简单实例
May 19 Javascript
jQuery中JSONP的两种实现方式详解
Sep 26 Javascript
bootstrap制作jsp页面(根据值让table显示选中)
Jan 05 Javascript
详解Webpack+Babel+React开发环境的搭建的方法步骤
Jan 09 Javascript
微信小程序实现的涂鸦功能示例【附源码下载】
Jan 12 Javascript
使用Vue组件实现一个简单弹窗效果
Apr 23 Javascript
JS实现仿微信支付弹窗功能
Jun 25 Javascript
Vue打包后访问静态资源路径问题
Nov 08 Javascript
浅谈vue 组件中的setInterval方法和window的不同
Jul 30 Javascript
vue element table中自定义一些input的验证操作
Jul 18 #Javascript
vue cli4.0项目引入typescript的方法
Jul 17 #Javascript
js实现省级联动(数据结构优化)
Jul 17 #Javascript
Vue如何基于vue-i18n实现多国语言兼容
Jul 17 #Javascript
jquery实现有过渡效果的tab切换
Jul 17 #jQuery
使用Vue-cli 中为单独页面设置背景图片铺满全屏
Jul 17 #Javascript
vue 点击其他区域关闭自定义div操作
Jul 17 #Javascript
You might like
php格式化日期和时间格式化示例分享
2014/02/24 PHP
php解析url并得到url中的参数及获取url参数的四种方式
2015/10/26 PHP
Yii中Model(模型)的创建及使用方法
2015/12/28 PHP
完整显示当前日期和时间的JS代码
2007/09/17 Javascript
input 高级限制级用法
2009/03/26 Javascript
让低版本浏览器支持input的placeholder属性(js方法)
2013/04/03 Javascript
jquery实现隐藏与显示动画效果/输入框字符动态递减/导航按钮切换
2013/07/01 Javascript
input获取焦点时底部菜单被顶上来问题的解决办法
2017/01/24 Javascript
手把手搭建安装基于windows的Vue.js运行环境
2017/06/12 Javascript
jQuery实现键盘回车搜索功能
2017/07/25 jQuery
VUE安装使用教程详解
2019/06/03 Javascript
浅谈vue限制文本框输入数字的正确姿势
2019/09/02 Javascript
webpack 如何同时输出压缩和未压缩的文件的实现步骤
2020/06/05 Javascript
[41:17]完美世界DOTA2联赛PWL S3 access vs CPG 第二场 12.13
2020/12/17 DOTA
Python 命令行参数sys.argv
2008/09/06 Python
Python生成器(Generator)详解
2015/04/13 Python
python轻松查到删除自己的微信好友
2016/01/10 Python
python制作企业邮箱的爆破脚本
2016/10/05 Python
基于Python的文件类型和字符串详解
2017/12/21 Python
python正则表达式及使用正则表达式的例子
2018/01/22 Python
python 通过logging写入日志到文件和控制台的实例
2018/04/28 Python
解决win64 Python下安装PIL出错问题(图解)
2018/09/03 Python
Python 实现王者荣耀中的敏感词过滤示例
2019/01/21 Python
pytorch 固定部分参数训练的方法
2019/08/17 Python
关于pycharm中pip版本10.0无法使用的解决办法
2019/10/10 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2020/04/13 Python
css3动画事件—webkitAnimationEnd与计时器time事件
2013/01/31 HTML / CSS
阿迪达斯西班牙官方网站:adidas西班牙
2016/07/21 全球购物
信息技术课后反思
2014/04/27 职场文书
村党支部书记个人对照材料汇报
2014/10/26 职场文书
异地恋情人节寄语
2015/02/28 职场文书
2015年药店工作总结
2015/04/20 职场文书
2015年公务员个人工作总结
2015/04/24 职场文书
《弟子规》读后感:知廉耻、明是非、懂荣辱、辨善恶
2019/12/03 职场文书
导游词之介休绵山
2019/12/31 职场文书
nginx实现发布静态资源的方法
2021/03/31 Servers