• 解决el-checkbox点击文字也会选中


    最近要做一个 多选框嵌套下拉框的一个功能,在点击下拉框时,多选框一直会被选中或者取消,这里做一下解决记录
    首先展示一下要做的功能
    在这里插入图片描述
    出现原因:
    el 的checkbox的组件整个是由lable包裹的,所以重写el-checkbox就可以了
    原编码:
    在这里插入图片描述
    解决:
    这里用div或者span都是可以的,div会处理成 line-block

    <template>
      <div
        :id="id"
        class="el-checkbox"
        :class="[
          border && checkboxSize ? 'el-checkbox--' + checkboxSize : '',
          { 'is-disabled': isDisabled },
          { 'is-bordered': border },
          { 'is-checked': isChecked }
        ]"
      >
        <label>
          <span
            class="el-checkbox__input"
            :class="{
              'is-disabled': isDisabled,
              'is-checked': isChecked,
              'is-indeterminate': indeterminate,
              'is-focus': focus
            }"
            :tabindex="indeterminate ? 0 : false"
            :role="indeterminate ? 'checkbox' : false"
            :aria-checked="indeterminate ? 'mixed' : false"
          >
            <span class="el-checkbox__inner" />
            <input
              v-if="trueLabel || falseLabel"
              v-model="model"
              class="el-checkbox__original"
              type="checkbox"
              :aria-hidden="indeterminate ? 'true' : 'false'"
              :name="name"
              :disabled="isDisabled"
              :true-value="trueLabel"
              :false-value="falseLabel"
              @change="handleChange"
              @focus="focus = true"
              @blur="focus = false"
            >
            <input
              v-else
              v-model="model"
              class="el-checkbox__original"
              type="checkbox"
              :aria-hidden="indeterminate ? 'true' : 'false'"
              :disabled="isDisabled"
              :value="label"
              :name="name"
              @change="handleChange"
              @focus="focus = true"
              @blur="focus = false"
            >
          span>
        label>
        <span v-if="$slots.default || label" class="el-checkbox__label">
          <slot />
          <template v-if="!$slots.default">{{ label }}template>
        span>
      div>
    template>
    <script>
    import Emitter from 'element-ui/src/mixins/emitter'
    
    export default {
      name: 'RCheckbox',
    
      mixins: [Emitter],
    
      inject: {
        elForm: {
          default: ''
        },
        elFormItem: {
          default: ''
        }
      },
    
      componentName: 'RCheckbox',
    
      props: {
        value: {},
        label: {},
        indeterminate: Boolean,
        disabled: Boolean,
        checked: Boolean,
        name: String,
        trueLabel: [String, Number],
        falseLabel: [String, Number],
        id: String, /* 当indeterminate为真时,为controls提供相关连的checkbox的id,表明元素间的控制关系*/
        controls: String, /* 当indeterminate为真时,为controls提供相关连的checkbox的id,表明元素间的控制关系*/
        border: Boolean,
        size: String
      },
    
      data() {
        return {
          selfModel: false,
          focus: false,
          isLimitExceeded: false
        }
      },
    
      computed: {
        model: {
          get() {
            return this.isGroup
              ? this.store : this.value !== undefined
                ? this.value : this.selfModel
          },
    
          set(val) {
            if (this.isGroup) {
              this.isLimitExceeded = false;
              (this._checkboxGroup.min !== undefined &&
                val.length < this._checkboxGroup.min &&
                (this.isLimitExceeded = true));
    
              (this._checkboxGroup.max !== undefined &&
                val.length > this._checkboxGroup.max &&
                (this.isLimitExceeded = true))
    
              this.isLimitExceeded === false &&
              this.dispatch('ElCheckboxGroup', 'input', [val])
            } else {
              this.$emit('input', val)
              this.selfModel = val
            }
          }
        },
    
        isChecked() {
          if ({}.toString.call(this.model) === '[object Boolean]') {
            return this.model
          } else if (Array.isArray(this.model)) {
            return this.model.indexOf(this.label) > -1
          } else if (this.model !== null && this.model !== undefined) {
            return this.model === this.trueLabel
          }
        },
    
        isGroup() {
          let parent = this.$parent
          while (parent) {
            if (parent.$options.componentName !== 'ElCheckboxGroup') {
              parent = parent.$parent
            } else {
              this._checkboxGroup = parent
              return true
            }
          }
          return false
        },
    
        store() {
          return this._checkboxGroup ? this._checkboxGroup.value : this.value
        },
    
        /* used to make the isDisabled judgment under max/min props */
        isLimitDisabled() {
          const { max, min } = this._checkboxGroup
          return !!(max || min) &&
            (this.model.length >= max && !this.isChecked) ||
            (this.model.length <= min && this.isChecked)
        },
    
        isDisabled() {
          return this.isGroup
            ? this._checkboxGroup.disabled || this.disabled || (this.elForm || {}).disabled || this.isLimitDisabled
            : this.disabled || (this.elForm || {}).disabled
        },
    
        _elFormItemSize() {
          return (this.elFormItem || {}).elFormItemSize
        },
    
        checkboxSize() {
          const temCheckboxSize = this.size || this._elFormItemSize || (this.$ELEMENT || {}).size
          return this.isGroup
            ? this._checkboxGroup.checkboxGroupSize || temCheckboxSize
            : temCheckboxSize
        }
      },
    
      watch: {
        value(value) {
          this.dispatch('ElFormItem', 'el.form.change', value)
        }
      },
    
      created() {
        this.checked && this.addToStore()
      },
      mounted() { // 为indeterminate元素 添加aria-controls 属性
        if (this.indeterminate) {
          this.$el.setAttribute('aria-controls', this.controls)
        }
      },
    
      methods: {
        addToStore() {
          if (
            Array.isArray(this.model) &&
            this.model.indexOf(this.label) === -1
          ) {
            this.model.push(this.label)
          } else {
            this.model = this.trueLabel || true
          }
        },
        handleChange(ev) {
          if (this.isLimitExceeded) return
          let value
          if (ev.target.checked) {
            value = this.trueLabel === undefined ? true : this.trueLabel
          } else {
            value = this.falseLabel === undefined ? false : this.falseLabel
          }
          this.$emit('change', value, ev)
          this.$nextTick(() => {
            if (this.isGroup) {
              this.dispatch('ElCheckboxGroup', 'change', [this._checkboxGroup.value])
            }
          })
        }
      }
    }
    script>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228

    引入:

    import RCheckbox from '@/rewrite/r-checkbox.vue'
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    分析报告有样板了-奥威BI数据可视化报表模板
    搬家公司 马蹄集
    触摸屏如何利用无线PPI通信模块远程采集PLC数据?
    Nginx监控模块
    【UCIe】UCIe Multi-Module Link 介绍
    彻底弄懂C#中delegate、event、EventHandler、Action、Func的使用和区别
    【GDB】常用命令
    Unity UGUI透明区域点击无效
    设计模式之创建型模式
    Java基础-继承性
  • 原文地址:https://blog.csdn.net/gd898989/article/details/126927895