There may be some bugs
Here are some explanations of each part
<template>
part:
- Create a checkbox group through
<u-checkbox-group>
labels, and all checkboxes in the group will share a data model.v-model
The directive is used to bind the value of the checkbox group, that is, the name of the selected checkbox.placement
Property set to"column"
means that the checkbox list will be arranged in columns.@change
The event listener is bound tocheckboxChange
the method, which is triggered when the checkbox state changes.- Inside the checkbox group, use
v-for
a loop to loop throughcheckboxList1
each object in the array and create the corresponding<u-checkbox>
checkbox.<u-checkbox>
The element's properties set the checkbox's appearance and label, and associate the checkbox's state with the checked state in the data model.
<script>
part:
data
Two data properties are defined in :checkboxValue1
andcheckboxList1
.checkboxValue1
Used to store the name of the selected check box, initially an empty array.checkboxList1
Contains an array of objects, each object represents a check box option, including the name and whether it is disabled.methods
Two methods are defined in :
checkboxChange
Method used to handle check box state change events. In this example, it simply prints out the changed information.selectAll
Method for selecting all functionality. Based on the number of currently selected checkboxes and the total number of checkboxes, it updatescheckboxValue1
the array to select or deselect all. At the same time, it also outputs the selected status information on the console.
Without further ado, let’s get straight to the code.
<template>
<view>
<u-checkbox-group
v-model="checkboxValue1"
placement="column"
@change="checkboxChange"
>
<u-checkbox
shape="circle"
size='60rpx'
labelSize='50rpx'
:customStyle="{marginBottom: '8px'}"
v-for="(item, index) in checkboxList1"
:key="index"
:label="item.name"
:name="item.name"
>
</u-checkbox>
</u-checkbox-group>
<!-- 添加全选按钮 -->
<u-checkbox
shape="circle"
size='60rpx'
labelSize='50rpx'
:label="'全选'"
@change="selectAll"
>
</u-checkbox>
</view>
</template>
<script>
export default {
data() {
return {
checkboxValue1:[],
// 基本案列数据
checkboxList1: [{
name: '苹果',
disabled: false
},
{
name: '香蕉',
disabled: false
},
{
name: '橙子',
disabled: false
}
],
}
},
methods: {
checkboxChange(n) {
console.log('change', n);
},
selectAll() {
// 点击全选按钮时,更新所有复选框的选中状态
if (this.checkboxValue1.length === this.checkboxList1.length) {
this.checkboxValue1 = [];
console.log(this.checkboxValue1,1);
console.log(this.checkboxList1,1);
} else {
this.checkboxValue1 = this.checkboxList1.map(item => item.name);
console.log(this.checkboxValue1,1);
console.log(this.checkboxList1,2);
}
}
}
}
</script>