欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue.js实现的全选与全不选功能示例【基于elementui】

程序员文章站 2022-05-05 15:51:18
本文实例讲述了vue.js实现的全选与全不选功能。分享给大家供大家参考,具体如下: elementui是有checkbox组件,不过问题在于checkbox组件内只能嵌套...

本文实例讲述了vue.js实现的全选与全不选功能。分享给大家供大家参考,具体如下:

elementui是有checkbox组件,不过问题在于checkbox组件内只能嵌套简单的字符串,如果要嵌入标签怎么办?

首先渲染页面:

<el-checkbox v-model="checkall" @change="handlecheckallchange">全选</el-checkbox>
<tbody v-for="item in orderdata">
 <tr>
  <td class="order-num" colspan="7">
   <el-checkbox v-model="item.checkmodel" @change="handlecheckitemchange" style="vertical-align:top;margin-top:20px;"></el-checkbox>
   <div class="num">
    <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >订单号:{{item.ordernumber}}</a>
    <p>商户单号:{{item.shopnumber}}</p>
   </div>
  </td>
  <td class="order-action" colspan="2">
   <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >查看详情</a>-<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >备注</a>-<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >加星</a>
  </td>
 </tr>
 <tr>
  <td>
   <div class="pic">
    <img :src=item.orderpic alt="">
   </div>
   <div class="info">
    <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{item.name}}</a>
    <p>{{item.size}}</p>
    <p>商品来源:{{item.from}}</p>
   </div>
  </td>
  <td>{{item.number}}</td>
  <td>{{item.price}}</td>
  <td>-</td>
  <td>{{item.company}}</td>
  <td>
   <p>{{item.address}}</p>
   <p>({{item.phone}})</p>
  </td>
  <td>{{item.date}}<br />{{item.time}}</td>
  <td>{{item.state}}</td>
  <td>{{item.pay}}<br /><span v-if="item.postcost">(运费:{{item.postcost}})</span></td>
 </tr>
</tbody>

初始化data数据:

checkall:false,
checkedallshops:[],
checkitemdata:[],
orderdata:[
 {
  checkmodel:false,
  ordernumber:'2017081618322542542',
  shopnumber:'2017081618322542542',
  orderpic:'../../../../static/images/realtimeprofile01.png',
  name:'【商城】贴轻松穴位艾灸贴',
  size:'5贴*盒',
  from:'本店商品',
  number:'10',
  price:'200.00',
  company:'蒂花之秀',
  address:'童话镇',
  phone:'12345678910',
  date:'2018-1-12',
  time:'09:30:00',
  state:'订单关闭',
  pay:'400.00',
  postcost:'10'
 },{
  checkmodel:false,
  ordernumber:'2017081618322542542',
  shopnumber:'2017081618322542542',
  orderpic:'../../../../static/images/realtimeprofile01.png',
  name:'【商城】贴轻松穴位艾灸贴',
  size:'5贴*盒',
  from:'本店商品',
  number:'10',
  price:'200.00',
  company:'蒂花之秀',
  address:'童话镇',
  phone:'12345678910',
  date:'2018-1-12',
  time:'09:30:00',
  state:'订单关闭',
  pay:'400.00',
  postcost:'10.00'
 }
]

相关方法:

handlecheckallchange(val){
 this.orderdata.map((item,i)=>{
  item.checkmodel = val;
 })
},
handlecheckitemchange(val){
 for(let i = 0,l = this.orderdata.length;i < l;i ++){
  if(this.orderdata[i].checkmodel !== val){
   this.checkall = false;
   return;
  }
 }
 this.checkall = val;
}

css代码就不贴出来了,不好看,哈哈

感兴趣的朋友还可以使用本站如下在线工具测试运行效果:

在线html/css/javascript前端代码调试运行工具:
http://tools.jb51.net/code/webcoderun

在线html/css/javascript代码运行工具:
http://tools.jb51.net/code/htmljsrun

希望本文所述对大家vue.js程序设计有所帮助。