index.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!--
  2. * @Author: gaoshoujun gaosj@yueworld.cn
  3. * @Date: 2022-06-28 11:00:24
  4. * @LastEditors: 傅豪杰 18516149270@163.com
  5. * @LastEditTime: 2023-05-12 17:05:08
  6. -->
  7. <template>
  8. <!-- 全选 -->
  9. <div>
  10. <el-checkbox
  11. v-if="hasCheckAll"
  12. class="f-checkbox"
  13. v-model="checkAllData"
  14. v-bind="$attrs"
  15. :disabled="isDisabled"
  16. :indeterminate="currentValue.length>0&&currentValue.length<list.length"
  17. @change="handleCheckAllChange"
  18. >
  19. 全选
  20. </el-checkbox>
  21. <el-checkbox-group
  22. v-bind="$attrs"
  23. v-model="currentValue"
  24. @change="handleChange"
  25. :disabled="isDisabled"
  26. >
  27. <el-checkbox
  28. v-for="(item, index) in list"
  29. class="f-checkbox"
  30. :key="'checkbox' + index"
  31. :label="item[listKey]"
  32. >
  33. {{ item[listName] }}
  34. </el-checkbox>
  35. </el-checkbox-group>
  36. </div>
  37. </template>
  38. <script>
  39. import formMixins from'../../mixins/formMixins.js'
  40. export default {
  41. name: 'FCheckbox',
  42. mixins:[formMixins],
  43. data() {
  44. return {
  45. checkAll: false
  46. };
  47. },
  48. props:{
  49. value: {}, // 绑定的数据
  50. // 渲染的options
  51. list:{
  52. type:Array,
  53. default:()=>[]
  54. },
  55. // 选项value
  56. listKey:{
  57. type:String,
  58. default:'value'
  59. },
  60. // 选项name
  61. listName:{
  62. type:String,
  63. default:'name'
  64. },
  65. // 是否有全选
  66. hasCheckAll:{
  67. type:Boolean,
  68. default:false
  69. }
  70. },
  71. computed: {
  72. currentValue:{
  73. get(){
  74. return this.value || []
  75. },
  76. set(val){
  77. this.$emit('input',val)
  78. }
  79. },
  80. checkAllData: {
  81. get() {
  82. // 监听是否权限
  83. if (this.currentValue.length === this.list.length) {
  84. return true;
  85. }
  86. if (this.currentValue.length === 0) {
  87. return false;
  88. }
  89. return this.checkAll;
  90. },
  91. set(value) {
  92. this.checkAll = value;
  93. }
  94. }
  95. },
  96. methods: {
  97. handleCheckAllChange(val) {
  98. const list = this.list.map(item => item[this.listKey]);
  99. this.$emit('input',val ? list : [])
  100. },
  101. // change事件
  102. handleChange(val){
  103. this.$emit('change',val)
  104. }
  105. }
  106. };
  107. </script>