vue 选中和全选 两个计算属性实现

还是选中和全选功能,用两个计算属性来实现,别人的代码,思维确实不一样。学习了

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		table, td, th{
		    border:1px solid #ebebeb;
		    border-collapse:collapse;
		    text-align: center;
		}
		table {
			width:500px;
		}
	</style>
	<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
	<div class="app">
		<table>
			<thead>
				<tr>
					<th style="line-height: 38px">选中数
						{{checkedCount}}<input type="checkbox" v-model="allchecked" style="zoom:200%;vertical-align: middle">
					</th>
					<th>name</th>
					<th>age</th>
				</tr>
			</thead>
			<tbody>
				<tr v-for="item in list">
					<td><input type="checkbox" v-model="item.checked" style="zoom:200%"></td>
					<td>{{item.name}}</td>
					<td>{{item.age}}</td>
				</tr>
			</tbody>
		</table>
	</div>
	<script>
		var list=[
			{
				name:'小明',
				age: 23,
				checked: false
			},
			{
				name:'小红',
				age: 2,
				checked: true
			},
			{
				name:'小蓝',
				age: 23,
				checked: true
			},
			{
				name:'小bai',
				age: 40,
				checked: true
			},
			{
				name:'王小二',
				age: 18,
				checked: false
			}
		]
		new Vue({
			el: '.app',
			data: {
				list
			},
			computed: {
              allchecked: {
                // getter
                get: function () {
                  return this.list.length == this.checkedCount
                },
                // setter
                set: function (val) {
                	//val就是点击之后,全选按钮的v-model值(状态),勾上后就是val的值就是true。未勾上就是false
                    console.log(val)
                    this.list.forEach(item => {
	                  item.checked = val
	                })
                }
              },
              checkedCount: {
                // getter
                get: function () {
                  var i = 0
                  this.list.forEach(item => {
                  	if (item.checked === true) i++                  	
                  }) 
                  return i
                }
              }
            }
		})
	</script>
</body>
</html>


Vue.js中结合Vant UI库实现全选计算总价功能以及自定义列表样式,你可以这样做: 1. **全选总价计算**: - 首先,在数据中定义一个数组用于存储商品信息,并包含一个布尔属性`isSelect`表示是否被选中,以及一个价格字段如`price`。 ```javascript data() { return { goodsList: [ { name: '商品A', price: 100, isSelect: false }, //... ], totalPrice: 0, }; } ``` - 创建一个全选按钮事件处理器,遍历`goodsList`更新所有商品的`isSelect`状态,并动态计算`totalPrice`。 ```javascript methods: { toggleAllSelection() { this.goodsList.forEach((item) => (item.isSelect = !item.isSelect)); this.updateTotalPrice(); }, updateTotalPrice() { let sum = 0; this.goodsList.forEach((item) => { if (item.isSelect) { sum += item.price; } }); this.totalPrice = sum.toFixed(2); // 保留两位小数 }, } ``` 2. **设置列表样式**: - 使用Vant中的`van-list`组件,可以配置项模板(`template`)选中样式(`active-item-class`或`active-class`): ```html <van-list :loadmore="false" :finished="true"> <van-list-item v-for="(item, index) in goodsList" :key="index" @click="toggleItem(index)"> <div class="list-item" :class="{ selected: item.isSelect }"> {{ item.name }} <span>¥{{ item.price }}</span> </div> </van-list-item> </van-list> <style scoped> .list-item { // 自定义普通项样式 } .selected { /* 选中时的样式 */ } </style> ``` - 在`.selected`类中添加你需要的全选后的显示效果,比如背景颜色变化。 **相关问题--:** 1. 如何在Vant中绑定`v-model`到列表中的`isSelect`属性? 2. 怎样阻止用户直接修改`totalPrice`数据? 3. 如何在Vue中处理单个商品的点击事件并更新对应的价格状态?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值