spring
6 天以前 f26f29d84e0a68831a6af14dab3eec5500496d2e
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
<template>
    <view class="u-flex-1">
        <u-checkbox-group :disabled="disabled" @change="change">
            <u-checkbox v-model="item.checked" v-for="(item, index) in options.items" :key="index"
                :name="item[itemValue]">{{item[itemLabel]}}</u-checkbox>
        </u-checkbox-group>
    </view>
</template>
<script>
/**
 * 复选框组件 {{item[itemLabel]}}
 * @property {Object} value 用于双向绑定选择框的值,返回选择框的 Value
 * @property {Boolean} disabled 是否禁用模式,是否只读模式
 * @property {String} dictType 字典类型,从字典里获取,自动设置 items、itemLabel、itemValue
 * @property {String} items 列表数据,可接受对象集合,如:[{name: '是', value: '否'}]
 * @property {String} itemLabel 指定列表数据中的什么属性名作为 option 的标签名,如 name
 * @property {String} itemValue 指定列表数据中的什么属性名作为 option 的 value 值,如 value
 * @example <js-select v-model="model.type" dict-type="sys_yes_no"></js-select>
 * @description Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * @author ThinkGem
 * @version 2021-3-11
 */
export default {
    props: {
        value: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false
        },
        dictType: {
            type: String,
            default: ''
        },
        items: {
            type: Array,
            default() {
                return [];
            }
        },
        itemLabel: {
            type: String,
            default: 'label'
        },
        itemValue: {
            type: String,
            default: 'value'
        }
    },
    data() {
        return {
            options: {
                value: (this.value && this.value.split(',')) || [],
                items: this.items
            }
        };
    },
    watch: {
        value(val, oldVal) {
            this.options.value = (val && val.split(',')) || [];
      this.loadData();
        },
        items(val, oldVal){
            this.options.items = val;
      this.loadData();
        }
    },
    methods: {
        loadData(){
            if (this.dictType != ''){
                this.$u.api.dictData({dictType: this.dictType}).then(res => {
                    // this.options.items = res;
                    this.selectValue(res.data);
                });
            }else{
                // this.options.items = this.items;
                this.selectValue(this.items);
            }
        },
        selectValue(items){
            // 微信小程序,需要延迟下,否则获取不 value 导致无法回显数据
            this.$nextTick(() => {
                let vals = this.options.value;
                for (let i in items){
          this.$set(items[i],'checked',vals.includes(items[i][this.itemValue]))
                }
                this.options.items = items;
            });
        },
        change(val, name){
            this.$emit('input', (val && val.join(',')) || '');
        }
    }
}
</script>
<style lang="scss" scoped>
 
</style>