demo4.vue 964 B

123456789101112131415161718192021222324252627282930313233343536
  1. <template>
  2. <m-checkbox
  3. v-model="checkAll"
  4. :indeterminate="isIndeterminate"
  5. @change="handleCheckAllChange">
  6. 全选
  7. </m-checkbox>
  8. <div style="margin: 10px 0;"></div>
  9. <m-checkbox-group
  10. v-model="checkedCities"
  11. @change="handleCheckedCitiesChange">
  12. <m-checkbox v-for="city in cities" :label="city" :key="city">
  13. {{city}}
  14. </m-checkbox>
  15. </m-checkbox-group>
  16. </template>
  17. <script setup>
  18. import { ref } from 'vue-demi'
  19. const cities = ['上海', '北京', '广州', '深圳']
  20. const checkAll = ref(false)
  21. const isIndeterminate = ref(true)
  22. const checkedCities = ref(['上海', '北京'])
  23. const handleCheckAllChange = (val) => {
  24. checkedCities.value = val ? cities : []
  25. isIndeterminate.value = false
  26. }
  27. const handleCheckedCitiesChange = (value) => {
  28. const checkedCount = value.length
  29. checkAll.value = checkedCount === cities.length
  30. isIndeterminate.value = checkedCount > 0 && checkedCount < cities.length
  31. }
  32. </script>