Checkbox 复选按钮

介绍

多选按钮用于选择。

安装

  1. import { createApp } from 'vue';
  2. import { Checkbox } from '@nutui/nutui';
  3. const app = createApp();
  4. app.use(Checkbox);

基本用法

  1. <nut-checkbox v-model="checkbox1" label="复选框"><span>复选框</span></nut-checkbox>
  2. <nut-checkbox v-model="checkbox2" text-position="left">复选框</nut-checkbox>
  1. setup() {
  2. return {
  3. checkbox1: false,
  4. checkbox2: false,
  5. };
  6. }

禁用状态

  1. <nut-checkbox v-model="checkbox3" disabled>未选时禁用状态</nut-checkbox>
  2. <nut-checkbox v-model="checkbox4" disabled>选中时禁用状态</nut-checkbox>
  1. setup() {
  2. return {
  3. checkbox3: false,
  4. checkbox4: true,
  5. };
  6. }

自定义尺寸

  1. <nut-checkbox v-model="checkbox5" icon-size="25">自定义尺寸25</nut-checkbox>
  2. <nut-checkbox v-model="checkbox6" icon-size="10">自定义尺寸10</nut-checkbox>
  1. setup() {
  2. return {
  3. checkbox5: true,
  4. checkbox6: false,
  5. };
  6. }

自定义图标

这里建议同时设置 icon-nameicon-active-name 属性

  1. <nut-checkbox v-model="checkbox7" icon-name="checklist" icon-active-name="checklist">自定义图标</nut-checkbox>
  1. setup() {
  2. return {
  3. checkbox7: true,
  4. };
  5. }

change事件

值发生变化时,将触发change事件

  1. <nut-checkbox v-model="checkbox8" @change="changeBox3">change复选框</nut-checkbox>
  1. setup() {
  2. const checkbox = ref(false);
  3. const changeBox3= (checked: boolean, label: string) => {
  4. console.log('change事件触发' + checked, label);
  5. }
  6. return {
  7. checkbox,
  8. changeBox3
  9. };
  10. }

checkboxGroup使用

  1. <nut-checkboxgroup v-model="checkboxgroup1">
  2. <nut-checkbox v-model="checkbox9" label="1">组合复选框</nut-checkbox>
  3. <nut-checkbox v-model="checkbox10" label="2">组合复选框</nut-checkbox>
  4. <nut-checkbox v-model="checkbox11" label="3">组合复选框</nut-checkbox>
  5. <nut-checkbox v-model="checkbox12" label="4">组合复选框</nut-checkbox>
  6. </nut-checkboxgroup>
  1. setup() {
  2. return {
  3. checkbox9: false,
  4. checkbox10: false,
  5. checkbox11: false,
  6. checkbox12: false,
  7. checkboxgroup1: ['2', '3'],
  8. };
  9. }

checkboxGroup 全选/取消

  1. <nut-checkboxgroup v-model="checkboxgroup3" ref="group" @change="changeBox4">
  2. <nut-checkbox v-model="checkbox15" label="1">组合复选框</nut-checkbox>
  3. <nut-checkbox v-model="checkbox16" label="2">组合复选框</nut-checkbox>
  4. </nut-checkboxgroup>
  5. <span class="btn">
  6. <nut-button type="primary" @click="toggleAll(true)">全选</nut-button>
  7. <nut-button type="primary" @click="toggleAll(false)">取消</nut-button>
  8. </span>
  1. setup() {
  2. const group = ref(null);
  3. const changeBox4 = (label: any[]) => {
  4. Toast.text(`${label.length ? '全选' : '取消全选'}`);
  5. };
  6. const toggleAll = (f: boolean) => {
  7. (group.value as any).toggleAll(f);
  8. };
  9. return {
  10. checkbox15: false,
  11. checkbox16: false,
  12. changeBox4: false,
  13. checkbox12: false,
  14. checkboxgroup3: ['2'],
  15. group,
  16. changeBox4,
  17. toggleAll
  18. };
  19. }

Checkbox

字段说明类型默认值
v-model是否处于选中状态Booleanfalse
disabled是否禁用选择Booleanfalse
text-position文本所在的位置,可选值:left,rightStringright
icon-size图标尺寸String、Number18
icon-name图标名称,选中前(建议和icon-active-name一起修改)String‘check-normal’
icon-active-name图标名称,选中后(建议和icon-name一起修改)String‘checked’
label复选框的文本内容String-

CheckboxGroup

字段说明类型默认值
v-model当前选中项的标识符,和 label 相对应String-
disabled是否禁用选择,将用于其下的全部复选框Booleanfalse

Checkbox Event

字段说明回调参数
change值变化时触发(state, label),state代表当前状态,label表示当前选中的值

CheckboxGroup Event

字段说明回调参数
change值变化时触发label,label返回一个数组,表示当前选中项的集合

Checkbox  复选按钮 - 图1