list.nvue 7.0 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2
<template>
	<view>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
3 4 5 6 7
		<!-- 搜索功能 小程序用绘制的且带原生导航,h5和app用原生自带绘制 -->
		<!-- #ifndef APP-PLUS -->
		<view class="uni-search-box">
			<uni-search-bar class="uni-search-bar" v-model="keyword" ref="searchBar" radius="100"
				@click.native="searchClick" cancelButton="none" disabled />
DCloud_JSON's avatar
DCloud_JSON 已提交
8
		</view>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
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
		<!-- #endif -->

		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @load="handleLoad"
			:where="where" collection="opendb-news-articles,uni-id-users"
			field="avatar,title,last_modify_date,user_id{username}">
			<!-- 基于 uni-list 的页面布局 -->
			<uni-list class="uni-list" :border="false" :style='{height:windowHeight+"px"}' :bounce="true"
				:alwaysScrollableVertical="true">
				<!-- #ifdef APP-NVUE -->
				<refresh @refresh="refresh" @pullingdown="onpullingdown" :display="!loading ? 'show' : 'hide'">
					<refreshBox :state="refreshState"></refreshBox>
				</refresh>
				<!-- #endif -->
				<cell class="get-data-state" v-if="data.length===0&&pagination.current===1">
					<!-- 刷新页面后的顶部提示框 当前弹出内容没有实际逻辑 ,可根据当前业务修改弹出提示 -->
					<text class="refresh-tip"
						:class="{ 'show-refresh-tip':showRefreshTip }">为您更新了{{data.length}}条内容</text>
					<!-- 数据为空 当前页码为1,且正在加载中;这里为了演示,更加直观的表达内部逻辑。商用项目建议将这部分封装为组件,更好的让业务逻辑与功能分离-->
					<template v-if="loading">
						<image class="get-data-state-img" src="@/static/getDataState/loading.png" mode="widthFix">
						</image>
						<text class="get-data-state-text">加载中...</text>
					</template>
					<template v-else>
						<image class="get-data-state-img" src="@/static/getDataState/nodata.png" mode="widthFix">
						</image>
						<text class="get-data-state-text">内容为空</text>
					</template>
				</cell>
				<uni-list-item v-if="error">{{error.message}}</uni-list-item>
				<uni-list-item v-else :to="'./detail?id='+item._id+'&title='+item.title"
					v-for="(item,index) in testData(data)" :key="index">
					<!-- 通过header插槽定义列表左侧图片 -->
					<template v-slot:header>
						<image class="avatar" :src="item.avatar" mode="aspectFill"></image>
					</template>
					<!-- 通过body插槽定义布局 -->
					<view slot="body" class="main">
						<text class="title">{{ item.title }}</text>
						<view class="info">
							<text class="author">{{item.user_id[0].username}}</text>
							<uni-dateformat class="last_modify_date" :date="item.last_modify_date" format="yyyy-MM-dd"
								:threshold="[60000, 2592000000]" />
DCloud_JSON's avatar
DCloud_JSON 已提交
52
						</view>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
53 54 55 56 57 58 59 60 61 62 63 64
					</view>
				</uni-list-item>

				<!-- 存在下一页数据,且不在加载中 通过 loadMore 组件实现上拉加载效果,如需自定义显示内容,可参考:https://ext.dcloud.net.cn/plugin?id=29 -->
				<cell v-if="hasMore">
					<uni-load-more v-if="!error &&!loading&&hasMore" :status="options.status"></uni-load-more>
				</cell>
				<cell v-else>
					<text class="noMore">- 没有更多数据了 -</text>
				</cell>
			</uni-list>
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
65 66 67 68
	</view>
</template>

<script>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
69
	var cdbRef, currentWebview;
DCloud_JSON's avatar
DCloud_JSON 已提交
70 71 72
	export default {
		data() {
			return {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
73 74 75 76 77 78
				showRefreshTip: true,
				where: "",
				keyword: "",
				refreshState: 0,
				windowHeight: 0 //窗口的高
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
79
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
80 81 82 83 84
		onReady() {
			this.windowHeight = uni.getSystemInfoSync().windowHeight
			this.showRefreshTip = false
			cdbRef = this.$refs.udb
			console.log(cdbRef);
DCloud_JSON's avatar
DCloud_JSON 已提交
85
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
86 87 88 89 90 91 92 93 94 95
		onShow() {
			try{
				this.keyword = getApp().globalData.searchText
				console.log(this.keyword);
				getApp().globalData.searchText = ''
				this.setInputText()
			}catch(e){
				console.log(e);
				//TODO handle the exception
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
96 97
		},
		methods: {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
			setInputText() {
				// 设置 searchInput的 text
				// #ifdef APP-PLUS
				if (!currentWebview) {
					let pages = getCurrentPages();
					currentWebview = pages[pages.length - 1].$getAppWebview();
				}
				currentWebview.setTitleNViewSearchInputText(this.keyword)
				// #endif
			},
			testData(data) {
				var testData = []
				for (let i = 0; i < 10; i++) {
					testData.push(...data)
				}
				return testData
DCloud_JSON's avatar
DCloud_JSON 已提交
114
			},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
			handleLoad(data, ended, pagination) {
				// `data` 当前查询结果 `ended` 是否有更多数据 `pagination` 分页信息 HBuilderX 3.1.5+ 支持
				console.log(9527, data, ended, pagination);
				//上拉加载成功
				this.loadMoreEnd()
			},
			searchClick() {
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
					animationType: 'fade-in',
					events: {
						acceptDataFromOpenedPage(e) {
							console.log(e);
						}
					}
				});
			},
			refresh() {
				cdbRef.loadData({
DCloud_JSON's avatar
DCloud_JSON 已提交
135 136
					clear: true
				}, () => {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
137 138 139 140 141 142 143 144 145 146
					this.showRefreshTip = true
					setTimeout(() => {
						this.showRefreshTip = false
					}, 1000)
					this.refreshEnd()
				})
			},
			loadMore() {
				cdbRef.loadMore({
					clear: true
DCloud_JSON's avatar
DCloud_JSON 已提交
147 148
				})
			},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
149 150 151 152 153 154 155 156
			refreshEnd() {
				// #ifdef APP-NVUE
				this.refreshState = 0
				// #endif
				uni.stopPullDownRefresh()
			},
			loadMoreEnd() {
				console.log('loadMoreEnd');
DCloud_JSON's avatar
DCloud_JSON 已提交
157
			},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
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
			// #ifdef APP-NVUE
			onpullingdown({
				pullingDistance,
				viewHeight
			}) {
				if (pullingDistance > viewHeight) {
					this.refreshState = 1
				}
			},
			// #endif
		},
		// #ifndef APP-NVUE
		onPullDownRefresh() {
			this.refresh()
			console.log('refresh');
		},
		// #endif
		onReachBottom() {
			this.loadMore()
			console.log('触底了');
		},
		onNavigationBarSearchInputClicked() {
			this.searchClick()
		},
		onNavigationBarSearchInputChanged(e) {
			console.log('变了', e);
			if (e.text) {
				this.where = 'title == ' + e.text
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
187 188
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
189
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
190

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
191
<style>
DCloud_JSON's avatar
DCloud_JSON 已提交
192 193 194 195 196 197
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
	}

	.info {
		flex-direction: row;
		justify-content: space-between;
	}

	.author,
	.last_modify_date {
		font-size: 28rpx;
		color: #999999;
	}

	.refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
218 219 220 221 222 223 224 225
		color: #67c23a;
		font-size: 14px;
		line-height: 40px;
		text-align: center;
		background-color: #f0f9eb;
		height: 0;
		opacity: 0;
		transform: translateY(-100%);
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
226
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
227
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
228 229 230 231 232 233 234 235 236

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
		left: 0;
		/* #ifndef APP-PLUS */
		z-index:9;
		/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
237
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
238 239

	.show-refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
240 241 242
		transform: translateY(0);
		height: 40px;
		opacity: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
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
	}

	.get-data-state {
		width: 750rpx;
		align-items: center;
	}

	.get-data-state-img {
		width: 500rpx;
	}

	.get-data-state-text {
		width: 32rpx;
		color: #999999;
		line-height: 50rpx;
		height: 50rpx;
		width: 750rpx;
		text-align: center;
	}

	.uni-list {
	}

	.noMore {
		width: 750rpx;
		text-align: center;
		color: #6E6E6E;
		font-size: 26rpx;
		height: 55px;
		line-height: 55px;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
274
</style>