基于Element UI库的Vue组件实现一个单选/多选框组合效果可以根据 type 属性的不同值来切换选框默认)和按钮式单选框/多选框

创建组件index.vue (src/commonui/radioGroup/index.vue)
<template>
  <el-radio-group
      v-model="hValue"
      :size="size"
      :disabled="disabled"
      :text-color="textColor"
      :fill="fill"
  &gt;
    <template v-for="(item, index) in dataSource"&gt;
      <el-radio-button
          v-if="type === 'button'"
          :key="index"
          :label="item[hProps.value]"
          :disabled="item[hProps.disabled]"
          :name="item[hProps.name]"
      &gt;{{ item[hProps.label] }}</el-radio-button
      >

      <el-radio
          v-else
          :key="index+'else'"
          :label="item[hProps.value]"
          :disabled="item[hProps.disabled]"
          :border="item[hProps.border] || border"
          :size="item[hProps.size]"
          :name="item[hProps.name]"
          :class="{ vertical }"
      >{{ item[hProps.label] }}</el-radio
      >

    </template>
  </el-radio-group>
</template>

<script>
  export default {
    name: "HRadioGroup",
    props: {
      border: Boolean,
      dataSource: {
        type: Array,
        label: [String, Number],
        value: [String, Number, Boolean],
        disabled: {
          type: Boolean,
          default: false
        },
        border: {
          type: Boolean,
          default: false
        },
        size: {
          type: String,
          validator(value) {
            return ["medium", "small", "mini"].indexOf(value) !== -1;
          }
        },
        name: String
      },
      disabled: {
        type: Boolean,
        default: false
      },
      fill: String,
      size: {
        type: String,
        validator(value) {
          return ["medium", "small", "mini"].indexOf(value) !== -1;
        }
      },
      textColor: String,
      type: {
        type: String,
        default: "normal",
        validator(value) {
          return ["normal", "button"].indexOf(value) !== -1;
        }
      },
      value: {
        type: [String, Number, Boolean]
      },
      vertical: Boolean,
      props: {
        type: Object,
        default() {
          return {};
        }
      }
    },
    computed: {
      hValue: {
        get() {
          return this.value;
        },
        set(value) {
          this.$emit("input", value);
          this.$emit("change", value);
        }
      },
      hProps() {
        return {
          label: "label",
          value: "value",
          disabled: "disabled",
          border: "border",
          size: "size",
          name: "name",
          ...this.props
        };
      }
    }
  };
</script>

<style lang="scss" scoped>
  .vertical {
    display: block;
    margin-left: 0 !important;
    &amp; + .vertical {
      margin-top: 10px;
    }
  }
</style>



页面引入

<template>
  <div>
    <h-radio-group
        :value="selectedValue"
        :data-source="dataSource"
        @change="val => handleRadioGroupChange(val)"
    >
    </h-radio-group>
  </div>
</template>
<script>
  import HRadioGroup from '@/common-ui/radioGroup/index'

  export default {
    components: {
      HRadioGroup
    },
    data() {
      return {
        filterVal: '',
        dataSource: [],
        selectedValue: ''
      }
    },
    methods: {
      handleRadioGroupChange(val, row) {
        console.log(val,'选中数据')
      },
    }
    // ...
  }
</script>

确保你已经安装了Vue.js和Element UI,并在项目中引入它们。

原文地址:https://blog.csdn.net/weixin_46328739/article/details/134737354

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_27022.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注