vue2.0在table中实现全选和反选的示例代码


Posted in Javascript onNovember 04, 2017

其实在去年小颖已经写过一篇:Vue.js实现checkbox的全选和反选

小颖今天在跟着慕课网学习vue的过程中,顺便试试如何在table中实现全选和反选,页面的css样式是直接参考慕课网的样式写的,js是小颖自己写哒,欢迎大家吐槽和点赞,嘻嘻

demo的  git 地址:ShoppingCart

页面效果:

vue2.0在table中实现全选和反选的示例代码

具体怎么实现的呢?

使用localstorage来存储页面信息 中已经有写项目是怎么创建的所以小颖在这里就不重复了,其实只是在上篇文章的基础上稍微做了改动:

vue2.0在table中实现全选和反选的示例代码

App.vue文件

<template>
 <div id="app">
  <router-view/>
 </div>
</template>
<script>
export default {
 name: 'app'
}

</script>
<style>
#app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 color: #2c3e50;
}

li,
dl,
dt,
dd,
h1,
h2,
h3,
h4,
h5,
h6,
hgroup,
p,
blockquote,
figure,
form,
fieldset,
input,
legend,
pre,
abbr,
button {
 margin: 0;
 padding: 0;
}

ul,
ol {
 list-style: none;
 margin: 0;
 padding: 0;
}

*,
*::before,
*::after {
 box-sizing: border-box;
}

div,
p,
dl,
dt,
dd {
 margin: 0;
 padding: 0;
}

a {
 color: inherit;
 text-decoration: none;
}

.checkout-title {
 position: relative;
 margin-bottom: 41px;
 text-align: center;
}

.checkout-title::before {
 position: absolute;
 top: 50%;
 left: 0;
 content: "";
 width: 100%;
 height: 1px;
 background: #ccc;
 z-index: 0;
}

.checkout-title span {
 position: relative;
 padding: 0 1em;
 background-color: #fff;
 font-family: "moderat", sans-serif;
 font-weight: bold;
 font-size: 20px;
 color: #605F5F;
 z-index: 1;
}
</style>

home.vue文件

<template>
 <div class="container">
  <div class="checkout-title">
   <span>购物车</span>
  </div>
  <table class="product_table">
   <tbody>
    <template v-for="(list,index) in table_list">
     <tr>
      <td width="7%" min-width="94px" v-if="index===0">
       <input type="checkbox" v-model='checked' v-on:click='checkedAll'></td>
      <td width="7%" v-if="index!==0">
       <input type="checkbox" v-model='checkList' :value="list.id">
      </td>
      <td width="43%">{{list.product_inf}}</td>
      <td width="10%" v-if="index===0">{{list.product_price}}</td>
      <td width="10%" v-if="index!==0">¥{{list.product_price}}</td>
      <td width="10%">{{list.product_quantity}}</td>
      <td width="10%">{{list.total_amount}}</td>
      <td width="20%" v-if="index===0">编辑</td>
      <td width="20%" v-if="index!==0">
       <a href="#" rel="external nofollow" rel="external nofollow" class="update">修改</a>
       <a href="#" rel="external nofollow" rel="external nofollow" class="delete">删除</a>
      </td>
     </tr>
    </template>
   </tbody>
  </table>
  <div class="price_total_bottom">
   <div class="price_total_ms">
    <label>合计:{{allProductTotal}}</label>
    <router-link to="/userAddress">结账</router-link>
   </div>
  </div>
 </div>
</template>
<script>
import userAddress from './address'
export default {
 components: {
  userAddress
 },
 data() {
  return {
   table_list: [{
    'id': 0,
    'product_inf': '商品信息',
    'product_price': '商品金额',
    'product_quantity': '商品数量',
    'total_amount': '总金额'
   }, {
    'id': '1',
    'product_inf': '女士银手链',
    'product_price': 120,
    'product_quantity': 200,
    'total_amount': 24000
   }, {
    'id': '2',
    'product_inf': '女士银手镯',
    'product_price': 380,
    'product_quantity': 200,
    'total_amount': 72000
   }, {
    'id': '3',
    'product_inf': '女士银耳环',
    'product_price': 100,
    'product_quantity': 200,
    'total_amount': 20000
   }],
   checked: false,
   allProductTotal: null,
   checkList: ['1', '3']
  }
 },
 methods: {
  checkedAll: function() {
   var _this = this;
   console.log(_this.checkList);
   if (_this.checked) { //实现反选
    _this.checkList = [];
   } else { //实现全选
    _this.checkList = [];
    _this.table_list.forEach(function(item, index) {
     if (index > 0) {
      _this.checkList.push(item.id);
     }
    });
   }
  }
 },
 watch: { //深度 watcher
  'checkList': {
   handler: function(val, oldVal) {
    if (val.length === this.table_list.length - 1) {
     this.checked = true;
    } else {
     this.checked = false;
    }
   },
   deep: true
  }
 }
}

</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.container {
 padding: 69px 0 54px 0;
}

table {
 border-collapse: collapse;
 border-color: transparent;
 text-align: center;
}

.product_table,
.product_table tbody {
 width: 100%
}

.product_table tr:first-child {
 background: #ece6e6;
 color: #e66280;
 font-size: 20px;
}

.product_table td {
 border: 1px solid #f3e8e8;
 height: 62px;
 line-height: 62px;
}

.product_table a.update:link,
.product_table a.update:visited,
.product_table a.update:hover,
.product_table a.update:active {
 color: #1CE24A;
}

.product_table a.delete:link,
.product_table a.delete:visited,
.product_table a.delete:hover,
.product_table a.delete:active {
 color: #ffa700;
}

.price_total_bottom {
 font-size: 20px;
 padding: 20px 10px;
}

.price_total_ms {
 text-align: right;
}

.price_total_bottom .price_total_ms label {
 margin-right: 100px;
}

.price_total_bottom .price_total_ms a {
 cursor: default;
 text-align: center;
 display: inline-block;
 font-size: 20px;
 color: #fff;
 font-weight: bold;
 width: 220px;
 height: 54px;
 line-height: 54px;
 border: 0;
 background-color: #f71455;
}

</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
fancybox modal的完美解决(右上的X)
Oct 30 Javascript
用js实现控件的隐藏及style.visibility的使用
Jun 14 Javascript
JavaScript 判断用户输入的邮箱及手机格式是否正确
Dec 08 Javascript
删除条目时弹出的确认对话框
Jun 05 Javascript
Kendo Grid editing 自定义验证报错提示的解决方法
Nov 18 Javascript
vue构建动态表单的方法示例
Sep 22 Javascript
angularjs使用div模拟textarea文本框的方法
Oct 02 Javascript
JavaScript实现表单注册、表单验证、运算符功能
Oct 15 Javascript
浅谈Node框架接入ELK实践总结
Feb 22 Javascript
jquery实现弹窗(系统提示框)效果
Dec 10 jQuery
vue分页插件的使用方法
Dec 25 Javascript
vue3.0中setup使用(两种用法)
Dec 02 Vue.js
vue中使用localstorage来存储页面信息
Nov 04 #Javascript
javascript+jQuery实现360开机时间显示效果
Nov 03 #jQuery
原生JavaScrpit中异步请求Ajax实现方法
Nov 03 #Javascript
使用 Javascript 实现浏览器推送提醒功能的示例
Nov 03 #Javascript
Angular4绑定html内容出现警告的处理方法
Nov 03 #Javascript
关于Vue背景图打包之后访问路径错误问题的解决
Nov 03 #Javascript
React.Js添加与删除onScroll事件的方法详解
Nov 03 #Javascript
You might like
PHP中,文件上传
2006/12/06 PHP
PHP排序之二维数组的按照字母排序实现代码
2011/08/13 PHP
兼容各大浏览器带关闭按钮的漂浮多组图片广告代码
2014/06/05 PHP
laravel 框架结合关联查询 when()用法分析
2019/11/22 PHP
jquery控制listbox中项的移动并排序的实现代码
2010/09/28 Javascript
Javascript处理DOM元素事件实现代码
2012/05/23 Javascript
利用JQuery和Servlet实现跨域提交请求示例分享
2014/02/12 Javascript
HTML页面登录时的JS验证方法
2014/05/28 Javascript
用js替换除数字与逗号以外的所有字符的代码
2014/06/07 Javascript
Webwork 实现文件上传下载代码详解
2016/02/02 Javascript
AngularJS使用指令增强标准表单元素功能
2016/07/01 Javascript
浅谈JS中的bind方法与函数柯里化
2016/08/10 Javascript
jQuery ajax MD5实现用户注册即时验证功能
2016/10/11 Javascript
基于bootstrap风格的弹框插件
2016/12/28 Javascript
浅谈$_FILES数组为空的原因
2017/02/16 Javascript
bootstrap Table服务端处理分页(后台是.net)
2017/10/19 Javascript
分享5个顶级的JavaScript Ajax组件库
2018/09/16 Javascript
js实现的订阅发布者模式简单示例
2020/03/14 Javascript
[06:44]2014DOTA2国际邀请赛-钥匙体育馆开战 开幕式振奋人心
2014/07/19 DOTA
[01:03:47]VP vs NewBee Supermajor 胜者组 BO3 第一场 6.5
2018/06/06 DOTA
python实战之实现excel读取、统计、写入的示例讲解
2018/05/02 Python
python opencv实现切变换 不裁减图片
2018/07/26 Python
Python绘制正余弦函数图像的方法
2018/08/28 Python
Python装饰器限制函数运行时间超时则退出执行
2019/04/09 Python
Python3如何判断三角形的类型
2020/04/12 Python
Python项目跨域问题解决方案
2020/06/22 Python
奥地利顶级内衣丝袜品牌英国站:Wolford英国
2016/08/29 全球购物
澳大利亚女士时装在线:Rockmans
2018/09/26 全球购物
什么情况下你必须要把一个类定义为abstract的
2013/01/06 面试题
服装销售人员求职自我评价
2013/09/26 职场文书
文明风采获奖感言
2014/02/18 职场文书
犯错检讨书
2014/02/21 职场文书
《赠汪伦》教学反思
2014/04/12 职场文书
七一建党节慰问信
2015/02/14 职场文书
个人求职自荐信范文
2015/03/06 职场文书
2015年度对口支援工作总结
2015/07/22 职场文书