uniapp heckbox-group实现多选

本文详细描述了一个使用Vue.js开发的应用,涉及HTML和JS代码,展示了如何实现可滚动视图和多选功能,包括checkbox-group组件,以及处理全选和选择特定经销商的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录


混了业务逻辑,谨慎观看

html 代码

<view>
	<!--可滚动视图区域。用于区域滚动 -->
	<scroll-view :style="{ height: clientHeight + 'px' }" :scroll-top="scrollTop" scroll-y="true"
		@scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll">
		<!--多项选择器-->
		<checkbox-group @change="checkboxChange">
			<!-- 多内容pendingList 卡片 -->
			<view v-for="(item, index) in pendingList" :key="index">
				<uni-card :title="item.deliveryCode" :extra="item.dbilldate.substring(0,10)">
					<view class="checkbox_collapse">
						<!--多选项目 checkbox 和 view同级-->
						<!-- value绑定的值;checked当前是否选中,可用来设置默认选中 -->
						<checkbox :value="item.checkboxValue" :checked="item.checked" color="#1d99ff" />
						<view style="margin-left: 10px;width: 100%;">
							<view v-for="(array,index3) in item.detail" :key="index3">
								<u-row>
									<u-col span="1" v-if="item.detail.length > 1">
										{{index3+1}}
									</u-col>
									<u-col span="11">
										<view class="uni-body order_detail_product_name">
											{{array.materialName}}
											{{array.castUnit}}
										</view>
										<view class="uni-body order_detail_product_name">
											<text style="color: #5d5d5d;">订单:</text>
											{{array.saleCode}}
										</view>
										<view class="uni-body order_detail_product_name bottom-border">
											<text style="color: #5d5d5d;">单项金额:</text>
											¥{{array.norigtaxmny}}
										</view>
									</u-col>
								</u-row>
							</view>
							<view class="order_detail">
								<view style="color: black;">¥{{item.totalPrice}}</view>
							</view>
						</view>
					</view>
				</uni-card>
			</view>
		</checkbox-group>
	</scroll-view>
	<view class="foot_view">
		<u-row>
			<u-col>
				<u-row>
					<u-col span="5" class="foot_view_checkbox">
						<!--全选选择器-->
						<checkbox-group placement="column" @change="groupChange">
							<checkbox :value="allRadio.name" :checked="allRadio.checked" color="#1d99ff"
								style="margin-left: 25px;">全选
							</checkbox>
						</checkbox-group>
					</u-col>
					<u-col span="9" justify="end">
						<view class="foot_view_but_box">
							<button type="primary" class="foot_view_but" @click="submitTicket">提交申请</button>
						</view>
					</u-col>
				</u-row>
			</u-col>
		</u-row>
	</view>
</view>

JS 代码

export default {
	name: "pending",
	data() {
		return {
			pendingList: [],
			scrollTop: 0,
			allRadio: { 
				name: '全选',
				checked: false
			},
			selectPending: [], //选中的经销商数据
			selectPendingCode: [],//选中的出库单编号和订单编号数据
			dealerRadio: false, //选择开票客户和发票类型对话框
			dealerRadioList: [], //开票客户
			selectDealerRadio: '', //对话框中选择的经销商
			totalAmount: 0,//总金额
			showDealerRadio: false, // 开票客户对话框
		}
	},
	methods: {
		checkboxChange(e) {
			this.totalAmount = 0 //总金额
			this.selectPending = [] //选中的经销商数据
			this.selectPendingCode = []//选中的出库单编号和订单编号数据
			let dealerList = new Set() //这步业务需要,过滤重复经销商
			//  e.detail.value是checkbox选中的值:"XC2022121000000002-SO302022121000000001"
			let selectData = e.detail.value
			
			//pendingList循环的数据
			for (var i = 0, lenI = this.pendingList.length; i < lenI; ++i) {
				const item = this.pendingList[i]
				//判断selectData和pendingList.checkboxValue是否相等,相等则把checked设为true
				if (selectData.includes(item.checkboxValue)) {
					this.$set(this.pendingList[i], 'checked', true)
				} else {
					this.$set(this.pendingList[i], 'checked', false)
				}
			}
			//  商品是否全部勾选,判断全选与否状态
			var offCarArr = []
			//this.pendingList.whether 属性值是true,则把itempush到offCarArr
			this.pendingList.forEach(item => item.whether == true ? offCarArr.push(item) : '')
			//offCarArr的checked都是true,则allChecks = true,否则反之
			//every 若有一个不满足条件,则返回false,后面的元素都不会再执行。不会对空数组进行检测,不会改变原始数组
			let allChecks = offCarArr.every(item => item.checked == true)
			//如果allChecks等于true就反allRadio改为选中状态
			allChecks ? this.$set(this.allRadio, 'checked', true) : this.$set(this.allRadio, 'checked', false)
	
			//往dealerList添加经销商数据
			for (let ii in selectData) {
				this.selectPendingCode.push(selectData[ii])
				const temp = selectData[ii].split('-')
				for (let i in this.pendingList) {
					if (temp[0] === this.pendingList[i].deliveryCode) {
						this.totalAmount += this.pendingList[i].totalPrice
						for (let j in this.pendingList[i].detail) {
							let item = this.pendingList[i].detail[j]
							const str = item.invoiceCustCode + '-' + item.invoiceCustName
							dealerList.add(str)
						}
					}
				}
			}
			this.selectPending = [...dealerList]
		},
		groupChange(n) {
			//全选按钮是否选中
			if (n.detail.value.length == 0) { 
				//未选中
				this.pendingList.map(item => this.$set(item, 'checked', false))
				this.$set(this.allRadio, 'checked', false)
				//清空业务数据
				this.totalAmount = 0
				this.selectPending = []
				this.selectPendingCode = []
			} else {
				// 选中
				for (var i in this.pendingList) {
					this.$set(this.pendingList[i], 'checked', true)
				}
				this.$set(this.allRadio, 'checked', true)
				//清空业务数据
				this.totalAmount = 0
				this.selectPending = []
				this.selectPendingCode = []
				let dealerList = new Set()
				//往dealerList添加经销商数据
				for (let i in this.pendingList) {
					if (this.pendingList[i].checked) {
						this.selectPendingCode.push(this.pendingList[i].checkboxValue)
						this.totalAmount += this.pendingList[i].totalPrice
						for (let j in this.pendingList[i].detail) {
							let item = this.pendingList[i].detail[j]
							const str = item.invoiceCustCode + '-' + item.invoiceCustName
							dealerList.add(str)
						}
					}
				}
				this.selectPending = [...dealerList]
			}
		},
	}
}
<think>嗯,用户问的是如何在UniApp中对checkbox-group组件进行动态赋值。首先,我需要回忆一下UniApp的文档,特别是关于checkbox-group的部分。记得checkbox-group通常需要绑定一个数组来管理中的值,动态赋值的话应该涉及到数据绑定和列表渲染。 然后,用户提到的动态赋值可能包括两种情况:初始化时从服务器获取数据,或者在运行时更新项。比如,用户可能从后端API获取项列表,然后渲染到页面上。这时候需要使用v-for指令来循环生成checkbox组件,同时用v-model绑定中的数组。 接下来,我需要检查是否有相关的示例代码。根据用户提供的引用,特别是引用[2]和[3],ElementUI中的例子使用了el-checkbox-group,用v-model绑定数组,v-for循环生成项。不过UniApp的语法可能稍有不同,比如使用checked属性或者value,但基本思路应该类似。 需要注意的是,UniAppcheckbox-group的change事件返回的是中的值数组,这和ElementUI的el-checkbox-group类似。用户可能需要监听这个事件来处理中的值变化。同时,动态赋值的重点在于如何将获取的数据(可能是异步的)赋值给项列表,并确保checkbox-group的v-model正确反映当前中的项。 还要考虑用户提到的引用[4],里面提到checkbox-group的checked属性可能无效,尤其是异步设置时可能会有问题。解决方案可能是确保在数据获取后再进行渲染,或者使用条件渲染v-if来避免状态不一致的问题。 另外,动态赋值时,每个checkbox的value或label需要正确对应,特别是在使用对象数组时,可能需要指定具体的键名。例如,每个项可能有id和name,这时候需要绑定正确的属性到label上。 总结步骤应该是:1. 在data中定义项列表和中数组;2. 使用v-for循环生成checkbox;3. 使用v-model绑定中数组;4. 在生命周期钩子中异步获取数据并更新项列表;5. 处理change事件更新中值。同时,要注意异步数据加载时的渲染时机,可能需要使用条件渲染或加载状态来避免问题。 最后,要提供一个示例代码,展示如何动态从服务器获取数据并赋值给checkbox-group,同时处理中状态。可能需要模拟异步请求,比如使用setTimeout,然后在回调中设置数据。还要提醒用户关于对象数据的处理,可能需要value-key属性,如果项是对象的话。</think>### 如何在UniApp实现`checkbox-group`动态赋值 在UniApp中,`checkbox-group`组件的动态赋值需要结合**数据绑定**和**列表渲染**实现,具体步骤如下: --- #### 1. 基础实现 **模板部分**:通过`v-for`动态生成项,`value`属性绑定唯一标识,`v-model`绑定中值数组。 ```html <checkbox-group @change="handleChange"> <label v-for="item in options" :key="item.id"> <checkbox :value="item.value" :checked="item.checked" /> {{ item.label }} </label> </checkbox-group> ``` **脚本部分**:定义数据源`options`和中值数组`selectedValues`。 ```javascript export default { data() { return { options: [ { id: 1, value: 'apple', label: '苹果', checked: false }, { id: 2, value: 'banana', label: '香蕉', checked: false } ], selectedValues: [] } }, methods: { handleChange(e) { this.selectedValues = e.detail.value; // 获取中值数组 } } } ``` --- #### 2. 动态赋值场景 **从接口异步加载数据**: ```javascript export default { data() { return { options: [], // 初始为空数组 selectedValues: [] } }, mounted() { // 模拟异步请求 setTimeout(() => { this.options = [ { id: 1, value: 'A', label: '项A' }, { id: 2, value: 'B', label: '项B' } ]; }, 1000); } } ``` **回显已数据**(如编辑场景): ```javascript // 假设接口返回已值['A'] setTimeout(() => { this.options = [ { id: 1, value: 'A', label: '项A' }, { id: 2, value: 'B', label: '项B' } ]; this.selectedValues = ['A']; // 自动勾对应项 }, 1000); ``` --- #### 3. 对象类型数据处理 若项值为对象,需设置`value-key`属性: ```html <checkbox-group @change="handleChange" :value-key="'id'"> <label v-for="item in objectOptions" :key="item.id"> <checkbox :value="item" /> {{ item.name }} </label> </checkbox-group> ``` 数据示例: ```javascript objectOptions: [ { id: 1, name: '北京' }, { id: 2, name: '上海' } ] ``` --- #### 4. 注意事项 1. **异步渲染问题**:若项数据需要异步加载,建议添加`v-if="options.length"`避免空数据渲染错误。 2. **性能优化**:当项数量过大时,可考虑分页加载或虚拟滚动[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值