list.nvue 5.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2
	<view>
3
		<!-- #ifndef H5 -->
DCloud_JSON's avatar
DCloud_JSON 已提交
4 5 6
		<uni-nav-bar :border="false"></uni-nav-bar>
		<!-- #endif -->
		<!-- 搜索功能 -->
7 8 9
		<uni-search-bar @click="searchClick" class="uni-search-box" v-model="keyword" ref="searchBar" radius="100" cancelButton="none" disabled/>
		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @error="onqueryerror"
			:where="where" collection="opendb-news-articles,uni-id-users" :page-size="10"
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
10 11
			field="avatar,title,last_modify_date,user_id{username}">
			<!-- 基于 uni-list 的页面布局 -->
12
			<uni-list class="uni-list" :border="false" :bounce="true" :alwaysScrollableVertical="true"  :style="{height:listHight}">
DCloud_JSON's avatar
DCloud_JSON 已提交
13
				<uni-list-item v-if="error">
DCloud_JSON's avatar
DCloud_JSON 已提交
14 15 16 17
					<template slot="body">
						<!-- 设置网络 -->
						<uni-network @change="refresh" @retry="refresh"></uni-network>
					</template>
DCloud_JSON's avatar
DCloud_JSON 已提交
18
				</uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
19 20
				
				<template v-else>
DCloud_JSON's avatar
DCloud_JSON 已提交
21
					<uni-list-item class="get-data-state" v-if="data.length===0&&pagination.current===1">
22
						<view class="f1" slot="body">
DCloud_JSON's avatar
DCloud_JSON 已提交
23 24 25 26 27
							<!-- 数据为空 当前页码为1,且正在加载中;这里为了演示,更加直观的表达内部逻辑。商用项目建议将这部分封装为组件,更好的让业务逻辑与功能分离-->
							<uni-load-more v-if="loading" status="loading"></uni-load-more>
							<template v-else>
								<text class="get-data-state-text">内容为空</text>
							</template>
28
						</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
29
					</uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
30
					<uni-list-item :to="'./detail?id='+item._id+'&title='+item.title"
31
						v-for="(item,index) in data" :key="index">
DCloud_JSON's avatar
DCloud_JSON 已提交
32 33 34 35 36 37 38 39 40 41 42 43
						<!-- 通过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]" />
							</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
44
						</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
45 46
					</uni-list-item>

47
					<!-- 存在下一页数据 && 不是正在加载中 && 已经满一页; 通过 loadMore 组件实现上拉加载效果,如需自定义显示内容,可参考:https://ext.dcloud.net.cn/plugin?id=29 -->
DCloud_JSON's avatar
DCloud_JSON 已提交
48 49 50 51 52
					<uni-list-item v-if="!loading&&data.length>10">
						<template slot="body">
							<uni-load-more :status="hasMore ? 'more' : 'noMore'"></uni-load-more>
						</template>
					</uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
53
				</template>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
54 55
			</uni-list>
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
56 57 58 59
	</view>
</template>

<script>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
60
	var cdbRef, currentWebview;
DCloud_JSON's avatar
DCloud_JSON 已提交
61 62 63
	export default {
		data() {
			return {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
64 65 66
				where: "",
				keyword: "",
				refreshState: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
67
				listHight: 0
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
68
			}
69 70 71 72 73 74 75 76 77
		},
		watch: {
			keyword(keyword, oldValue) {
				if(keyword){
					this.where = `/${keyword}/.test(title)`;
				}else{
					this.where = '';
				}
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
78
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
79
		onReady() {
80 81 82 83 84 85
			// #ifdef APP-NVUE
			this.listHight = uni.getSystemInfoSync().windowHeight - 96 + 'px'
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
			// #endif
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
86
			cdbRef = this.$refs.udb
DCloud_JSON's avatar
DCloud_JSON 已提交
87
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
88
		onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
89 90 91 92 93 94 95 96 97 98 99
			this.keyword = getApp().globalData.searchText
			getApp().globalData.searchText = ''
			if(this.keyword){
				// #ifdef APP-PLUS
				if (!currentWebview) {
					let pages = getCurrentPages();
					currentWebview = pages[pages.length - 1].$getAppWebview();
				}
				// 设置 searchInput的 text
				currentWebview.setTitleNViewSearchInputText(this.keyword)
				// #endif
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
100
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
101 102
		},
		methods: {
103
			searchClick(e) { //点击搜索框
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
104 105 106
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
107
					animationType: 'fade-in'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
108
				});
DCloud_JSON's avatar
DCloud_JSON 已提交
109 110 111 112 113 114 115 116 117 118 119 120
			},
			retry(){
				this.refresh()
			},
			refresh(){
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
121 122 123 124 125
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
DCloud_JSON's avatar
DCloud_JSON 已提交
126
			cdbRef.loadMore({clear: true})
DCloud_JSON's avatar
DCloud_JSON 已提交
127 128
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
129
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
130

DCloud_JSON's avatar
DCloud_JSON 已提交
131
<style scoped>
DCloud_JSON's avatar
DCloud_JSON 已提交
132 133 134 135 136 137
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
138 139 140 141 142 143
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
144
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158
	}

	.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 已提交
159 160 161 162 163 164 165 166
		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 已提交
167
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
168
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
169 170 171 172 173

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
174 175 176 177
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
178
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
179
	.show-refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
180 181 182
		transform: translateY(0);
		height: 40px;
		opacity: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	}

	.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;
	}
202 203 204 205
	.uni-list {}
	.f1{
		flex: 1;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
206
</style>