zhangwei
2024-08-14 9d8994790fe403935ed46ad478f83ae110bb7a01
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:  163,营业执照号:91   5101   3 1   3 3 20  0  6193 K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-grid__wrap" :class="[isShow?'fui-grid__wrap-show':'fui-grid__wrap-hidden']">
        <view :id="elemId" ref="fui_grid" class="fui-grid"
            :class="{ 'fui-grid__border': showBorder,'fui-grid__between':between }"
            :style="{ 'border-left-color':borderColor,'border-top-color':borderColor}">
            <slot></slot>
            <!-- #ifndef APP-VUE || H5 || MP-WEIXIN -->
            <template v-if="seats.length > 0 && between">
                <view v-for="(item,index) in seats" :key="index"
                    :style="'width:'+width+';'+(height?'height:'+height:'')"></view>
            </template>
            <!-- #endif -->
        </view>
    </view>
</template>
 
<script>
    // #ifdef APP-NVUE
    const dom = uni.requireNativePlugin('dom');
    // #endif
    export default {
        name: "fui-grid",
        emits: ['click'],
        props: {
            // 每行显示个数
            columns: {
                type: Number,
                default: 3
            },
            // 是否显示边框
            showBorder: {
                type: Boolean,
                default: true
            },
            // 边框颜色 仅nvue有效
            borderColor: {
                type: String,
                default: '#EEEEEE'
            },
            // 是否正方形显示,默认为 true
            square: {
                type: Boolean,
                default: true
            },
            // item 项是否两端对齐,仅在宽度无法拉满且影响美观时使用,谨慎使用 v2.0.0+
            between: {
                type: Boolean,
                default: false
            },
            //当数据无法铺满时,且设置了between 布局,末尾补足占位元素个数
            //最后一行数据个数+emptyElements 不可大于 columns 值
            emptyElements: {
                type: [Number, String],
                default: 0
            }
        },
        provide() {
            return {
                grid: this
            }
        },
        data() {
            const elemId = `fui_${Math.ceil(Math.random() * 10e5).toString(36)}`
            return {
                elemId: elemId,
                width: 0,
                height: 0,
                isShow: false,
                seats: []
            };
        },
        created() {
            this.children = []
            let sys = uni.getSystemInfoSync()
            this.width = (100 / this.columns) + '%'
            if (this.square) {
                this.height = parseInt((sys.windowWidth - 1) / this.columns * 10) / 10 + 'px'
            }
        },
        watch: {
            columns(val) {
                this.$nextTick(() => {
                    this.init()
                })
            },
            showBorder(val) {
                this.childChange()
            },
            emptyElements(val) {
                this.createEmptyEl(val)
            }
        },
        mounted() {
            this.$nextTick(() => {
                this.init()
                this.createEmptyEl(this.emptyElements)
            })
        },
        methods: {
            init() {
                setTimeout(() => {
                    this.getWidth((width, height) => {
                        this.children.forEach((item, index) => {
                            item.width = width
                            item.height = height
                        })
                        this.isShow = true
                    })
                }, 50)
            },
            createEmptyEl(val) {
                // 百度小程序在页面循环数字有问题
                const nums = Number(val)
                let seats = []
                if (nums && nums > 0) {
                    for (let i = 0; i < nums; i++) {
                        seats.push(i)
                    }
                    this.seats = seats
                } else {
                    this.seats = []
                }
            },
            childChange() {
                this.children.forEach((item, index) => {
                    item.showBorder = this.showBorder
                })
            },
            handleClick(e) {
                this.$emit('click', e)
            },
            getWidth(fn) {
                let isNoSupported = false
                // #ifdef MP-BAIDU || MP-TOUTIAO || MP-QQ || MP-KUAISHOU || MP-JD || MP-360 || MP-LARK
                isNoSupported = true
                // #endif
 
                // #ifndef APP-NVUE
                this.width = (100 / this.columns) + '%'
                if (this.square || isNoSupported) {
                    uni.createSelectorQuery()
                        // #ifndef MP-ALIPAY
                        .in(this)
                        // #endif
                        .select(`#${this.elemId}`)
                        .boundingClientRect()
                        .exec(ret => {
                            //使用 parseInt 不使用 Number 避免 部分android机 换行(小数渲染有兼容性问题) ,但是可能会出现误差无法铺满
                            let width = (ret[0].width - 1) / this.columns
                            width = (parseInt(width * 10) / 10) + 'px'
                            if (this.square)
                                this.height = width;
                            if (isNoSupported)
                                this.width = width;
                            fn(this.width, this.height)
                        })
                } else {
                    fn(this.width, this.height)
                }
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['fui_grid'], (ret) => {
                    //使用 parseInt 不使用 Number 可避免 部分android机 换行,但是可能会出现误差无法铺满
                    // 注:以真机测试为准,如果还有换行,则只能不保留小数
                    let width = (ret.size.width - 1) / this.columns
                    this.width = parseInt(width * 10) / 10 + 'px'
                    if (this.square) {
                        this.height = this.width;
                    }
                    fn(this.width, this.height)
                })
                // #endif
            }
        }
 
    }
</script>
 
<style scoped>
    .fui-grid__wrap {
        /* #ifndef APP-NVUE */
        width: 100%;
        display: flex;
        box-sizing: border-box;
        /* #endif */
        /* #ifdef APP-NVUE */
        flex: 1;
        /* #endif */
        flex-direction: column;
 
        /* 以下css可能导致数据过多nvue端直接白屏不显示 */
        /* #ifndef APP-NVUE */
        transition-property: opacity;
        transition-duration: .2s;
        transition-timing-function: ease-in-out;
        /* #endif */
    }
 
    .fui-grid__wrap-hidden {
        opacity: 0;
    }
 
    .fui-grid__wrap-show {
        opacity: 1 !important;
    }
 
    .fui-grid {
        /* #ifndef APP-NVUE */
        display: flex;
        width: 100%;
        /* #endif */
        flex-direction: row;
        flex-wrap: wrap;
    }
 
    /* #ifndef APP-VUE || H5 || MP-WEIXIN */
    .fui-grid__between {
        justify-content: space-between;
    }
 
    /* #endif */
 
    .fui-grid__border {
        position: relative;
        /* #ifdef APP-NVUE */
        border-left-style: solid;
        border-left-width: 0.5px;
        border-top-style: solid;
        border-top-width: 0.5px;
        /* #endif */
        /* #ifndef APP-NVUE */
        z-index: 1;
        border-left: 0;
        /* #endif */
    }
 
    /* #ifndef APP-NVUE */
    .fui-grid__border::before {
        content: ' ';
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 1px;
        border-top: 1px solid var(--fui-color-border, #EEEEEE);
        -webkit-transform-origin: 0 0;
        transform-origin: 0 0;
        -webkit-transform: scaleY(0.5);
        transform: scaleY(0.5);
        z-index: 1;
    }
 
    .fui-grid__border::after {
        content: ' ';
        position: absolute;
        left: 0;
        top: 0;
        width: 1px;
        height: 100%;
        border-left: 1px solid var(--fui-color-border, #EEEEEE);
        -webkit-transform-origin: 0 0;
        transform-origin: 0 0;
        -webkit-transform: scaleX(0.5);
        transform: scaleX(0.5);
    }
 
    /* #endif */
</style>