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 相关文章推荐
分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码
Sep 21 Javascript
Jquery带搜索框的下拉菜单
May 06 Javascript
JavaScript执行顺序详细介绍
Dec 04 Javascript
jquery操作checkbox实现全选和取消全选
May 02 Javascript
JQuery教学之性能优化
May 14 Javascript
深入分析jquery解析json数据
Dec 09 Javascript
js图片跟随鼠标移动代码
Nov 26 Javascript
js和jquery实现监听键盘事件示例代码
Jun 24 Javascript
javaScript字符串工具类StringUtils详解
Dec 08 Javascript
JavaScript设计模式之职责链模式应用示例
Aug 07 Javascript
JavaScript canvas绘制圆弧与圆形
Feb 18 Javascript
react结合bootstrap实现评论功能
May 30 Javascript
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
Re:从零开始的异世界生活 第2季 开播啦
2020/07/24 日漫
ie6 动态缩略图不显示的原因
2009/06/21 PHP
表格展示无限级分类(PHP版)
2012/08/21 PHP
PHP微信PC二维码登陆的实现思路
2017/07/13 PHP
php5与php7的区别点总结
2019/10/11 PHP
JS定义回车事件(实现代码)
2013/07/08 Javascript
jQuery在iframe中无法弹出对话框的解决方法
2014/01/12 Javascript
chrome下img加载对height()的影响示例探讨
2014/05/26 Javascript
在NodeJS中启用ECMAScript 6小结(windos以及Linux)
2014/07/15 NodeJs
jquery获得同源iframe内body下标签的值的方法
2014/09/25 Javascript
JavaScript在网页中画圆的函数arc使用方法
2015/11/13 Javascript
canvas雪花效果核心代码分享
2017/02/19 Javascript
jquery实现限制textarea输入字数的方法
2017/09/06 jQuery
详解Angularjs 自定义指令中的数据绑定
2018/07/19 Javascript
10分钟彻底搞懂Http的强制缓存和协商缓存(小结)
2018/08/30 Javascript
详解vue-cli 2.0配置文件(小结)
2019/01/14 Javascript
详解vue移动端项目代码拆分记录
2019/03/15 Javascript
Node.js使用MongoDB的ObjectId作为查询条件的方法
2019/09/10 Javascript
Paypal支付不完全指北
2020/06/04 Javascript
wxPython窗口中文乱码解决方法
2014/10/11 Python
web.py 十分钟创建简易博客实现代码
2016/04/22 Python
在python中实现对list求和及求积
2018/11/14 Python
Python进程间通信 multiProcessing Queue队列实现详解
2019/09/23 Python
HTML5 FormData 方法介绍以及实现文件上传示例
2017/09/12 HTML / CSS
标签和贴纸印刷:Lightning Labels
2018/03/22 全球购物
递归计算如下递归函数的值(斐波拉契)
2012/02/04 面试题
建筑施工实习自我鉴定
2013/09/19 职场文书
信息专业大学生自我评价分享
2014/01/17 职场文书
企业总经理岗位职责
2014/02/13 职场文书
体育教学随笔感言
2014/02/24 职场文书
贯彻学习两会心得体会范文
2014/03/17 职场文书
清明节演讲稿
2014/05/27 职场文书
关于有小孩的离婚协议书
2014/10/26 职场文书
2015年房地产销售工作总结
2015/04/20 职场文书
大学校园招聘会感想
2015/08/10 职场文书
如何撰写创业策划书
2019/06/27 职场文书