grid.vue 6.9 KB
Newer Older
芊里 已提交
1 2 3 4 5 6 7 8 9
<template>
	<view class="warp">
		<!-- 搜索 -->
		<template>
			<status-bar />
			<uni-search-bar ref="searchBar" style="flex:1;" radius="100" @click.native="searchClick" cancelButton="none"
				disabled />
		</template>
		<!-- banner -->
L
123  
linju 已提交
10
		<unicloud-db ref="bannerdb" v-slot:default="{data, loading, error, options}" :collection="collection"
芊里 已提交
11 12 13 14 15 16 17 18 19 20 21
			:field="field" @load="load">
			<uni-swiper-dot class="uni-swiper-dot-box" @clickItem="clickItem" :info="bannerFormate(data)"
				:current="current" :mode="mode" :dots-styles="dotsStyles" field="content">
				<swiper class="swiper-box" @change="changeSwiper" :current="swiperDotIndex">
					<swiper-item v-for="(item, index) in bannerFormate(data)" :key="item._id">
						<view :draggable="false" class="swiper-item" @click="clickBannerItem(item)">
							<image class="swiper-image" :src="item.bannerfile" mode="aspectFill" :draggable="false" />
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
L
123  
linju 已提交
22
		</unicloud-db>
芊里 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
		<!-- 宫格 -->
		<uni-section title="宫格组件" style="margin: 0;" type="line"></uni-section>
		<view class="example-body">
			<uni-grid :column="3" :highlight="true" @change="change">
				<uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
					<view class="grid-item-box" style="background-color: #fff;">
						<image :src="item.url" class="image" mode="aspectFill" />
						<text class="text">{{ item.text }}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
	</view>
</template>

L
123  
linju 已提交
38
<script>
芊里 已提交
39 40 41 42 43 44
	import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";
	export default {
		components: { statusBar },
		data() {
			return {
				list: [{
芊里 已提交
45
						url: '/static/grid/c1.png',
芊里 已提交
46 47 48 49 50
						text: 'Grid 1',
						badge: '0',
						type: "primary"
					},
					{
芊里 已提交
51
						url: '/static/grid/c2.png',
芊里 已提交
52 53 54 55 56
						text: 'Grid 2',
						badge: '1',
						type: "success"
					},
					{
芊里 已提交
57
						url: '/static/grid/c3.png',
芊里 已提交
58 59 60 61 62
						text: 'Grid 3',
						badge: '99',
						type: "warning"
					},
					{
芊里 已提交
63
						url: '/static/grid/c4.png',
芊里 已提交
64 65 66 67 68
						text: 'Grid 4',
						badge: '2',
						type: "error"
					},
					{
芊里 已提交
69
						url: '/static/grid/c5.png',
芊里 已提交
70 71 72
						text: 'Grid 5'
					},
					{
芊里 已提交
73
						url: '/static/grid/c6.png',
芊里 已提交
74 75 76
						text: 'Grid 6'
					},
					{
芊里 已提交
77
						url: '/static/grid/c7.png',
芊里 已提交
78 79 80
						text: 'Grid 7'
					},
					{
芊里 已提交
81
						url: '/static/grid/c8.png',
芊里 已提交
82 83 84
						text: 'Grid 8'
					},
					{
芊里 已提交
85
						url: '/static/grid/c9.png',
芊里 已提交
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
						text: 'Grid 9'
					}
				],

				collection: 'opendb-banner',
				// 查询字段,多个字段用 , 分割
				field: '_id,bannerfile,open_url,title',
				where: 'category_id==grid',
				// info: [],
				dotStyle: [{
						backgroundColor: 'rgba(0, 0, 0, .3)',
						border: '1px rgba(0, 0, 0, .3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(0, 0, 0, .9)',
						selectedBorder: '1px rgba(0, 0, 0, .9) solid'
					},
					{
						backgroundColor: 'rgba(255, 90, 95,0.3)',
						border: '1px rgba(255, 90, 95,0.3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(255, 90, 95,0.9)',
						selectedBorder: '1px rgba(255, 90, 95,0.9) solid'
					},
					{
						backgroundColor: 'rgba(83, 200, 249,0.3)',
						border: '1px rgba(83, 200, 249,0.3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(83, 200, 249,0.9)',
						selectedBorder: '1px rgba(83, 200, 249,0.9) solid'
					}
				],
				modeIndex: -1,
				styleIndex: -1,
				current: 0,
				mode: 'default',
				dotsStyles: {},
				swiperDotIndex: 0
			}
		},
		methods: {
			change(e) {
				let {
					index
				} = e.detail
				this.list[index].badge && this.list[index].badge++

				uni.showToast({
					title: `点击第${index+1}个宫格`,
					icon: 'none'
				})
			},
			searchClick() {
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/search/search',
					animationType: 'fade-in'
				});
			},
			/**
芊里 已提交
145
			 * banner加载后触发的回调
芊里 已提交
146 147
			 */
			load(data) {
芊里 已提交
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
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			selectStyle(index) {
				this.dotsStyles = this.dotStyle[index]
				this.styleIndex = index
			},
			selectMode(mode, index) {
				this.mode = mode
				this.modeIndex = index
				this.styleIndex = -1
				this.dotsStyles = this.dotStyle[0]
			},
			clickItem(e) {
				this.swiperDotIndex = e
			},
			/**
			 * 点击banner的处理
			 */
			clickBannerItem(item) {
				// 有外部链接-跳转url
				if (item.open_url) {
					//#ifdef APP-PLUS
					plus.runtime.openWeb(item.open_url);
					//#endif
					//#ifdef H5
					window.open(item.open_url)
					//#endif
				}
				// 其余业务处理
			},
			/**
			 * banner数据过滤
			 */
			bannerFormate(bannerList) {
				if (bannerList.length > 0) return bannerList;
				// 无数据添加默认值
				let list = [{
					"_id": -1,
芊里 已提交
189
					"bannerfile": "/static/grid/empty.png",
芊里 已提交
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
					"open_url": "https://www.dcloud.io/",
					"title": "内容 A",
				}]
				return list;
			}
		}
	}
</script>

<style>
	@charset "UTF-8";

	/* 头条小程序组件内不能引入字体 */
	/* #ifdef MP-TOUTIAO */
	@font-face {
		font-family: uniicons;
		font-weight: normal;
		font-style: normal;
		src: url("~@/static/uni.ttf") format("truetype");
	}

	/* #endif */
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #efeff4;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: center;
		padding: 0;
		font-size: 14px;
		background-color: #ffffff;
	}

	/* #endif */

	.example-body {
		flex-direction: column;
		padding: 15px;
		background-color: #ffffff;
	}

	.image {
		width: 50rpx;
		height: 50rpx;
	}

	.text {
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.swiper-image {
		width: 750rpx;
		height: 400rpx;
	}

	.swiper-box {
		height: 400rpx;
	}

	.search-icons {
		padding: 16rpx;
	}

	.search-container-bar {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		position: fixed;
		left: 0;
		right: 0;
		z-index: 10;
		background-color: #fff;
	}

	/* #ifndef APP-NVUE */
	/deep/
	/* #endif */
	.uni-searchbar__box {
		border-width: 0;
	}

	/* #ifndef APP-NVUE */
	/deep/
	/* #endif */
	.uni-input-placeholder {
		font-size: 28rpx;
	}
L
123  
linju 已提交
314
</style>