cdk
2025-03-24 06e6cf5719a7de9d7919cee2f6fcc3cfc6f9eb9d
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
<template>
    <view class="uni-grid-wrap">
        <view :id="elId" ref="uni-grid" class="uni-grid" :class="{ 'uni-grid--border': showBorder }" :style="{ 'border-left-style':'solid','border-left-color':borderColor, 'border-left-width':showBorder?'1px':0 }">
            <slot />
        </view>
    </view>
</template>
 
<script>
    // #ifdef APP-NVUE
    const dom = uni.requireNativePlugin('dom');
    // #endif
    export default {
        name: 'UniGrid',
        props: {
            // 每列显示个数
            column: {
                type: Number,
                default: 3
            },
            // 是否显示边框
            showBorder: {
                type: Boolean,
                default: true
            },
            // 边框颜色
            borderColor: {
                type: String,
                default: '#e5e5e5'
            },
            // 是否正方形显示,默认为 true
            square: {
                type: Boolean,
                default: true
            },
            highlight: {
                type: Boolean,
                default: true
            }
        },
        provide() {
            return {
                grid: this
            }
        },
        data() {
            const elId = `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
            return {
                elId,
                width: 0
            }
        },
        created() {
            this.children = []
        },
        mounted() {
            this.init()
        },
        methods: {
            init() {
                setTimeout(() => {
                    this._getSize((width) => {
                        this.children.forEach((item, index) => {
                            item.width = width
                        })
                    })
                }, 50)
            },
            change(e) {
                this.$emit('change', e)
            },
            _getSize(fn) {
                // #ifndef APP-NVUE
                uni.createSelectorQuery()
                    .in(this)
                    .select(`#${this.elId}`)
                    .boundingClientRect()
                    .exec(ret => {
                        this.width = parseInt((ret[0].width-1) / this.column) + 'px'
                        fn(this.width)
                    })
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['uni-grid'], (ret) => {
                    this.width = parseInt((ret.size.width-1) / this.column)  + 'px'
                    fn(this.width)
                })
                // #endif
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .uni-grid-wrap {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex: 1;
        flex-direction: column;
        /* #ifdef H5 */
        width: 100%;
        /* #endif */
    }
 
    .uni-grid {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        // flex: 1;
        flex-direction: row;
        flex-wrap: wrap;
    }
 
    .uni-grid--border {
        border-left-color: $uni-border-color;
        border-left-style: solid;
        border-left-width: 1px;
    }
</style>