list.nvue 5.5 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
87 88 89
			console.log(cdbRef);
			console.log(9527,getApp().globalData);
			
DCloud_JSON's avatar
DCloud_JSON 已提交
90
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
91
		onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
92 93 94 95 96 97 98 99 100 101 102 103
			this.keyword = getApp().globalData.searchText
			console.log(this.keyword);
			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 已提交
104
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
105 106
		},
		methods: {
107 108 109 110 111 112 113 114
			// testData(data) { //复制同一个数据多遍方便测试,正式项目中不要带
			// 	var testData = []
			// 	for (let i = 0; i < 10; i++) {
			// 		testData.push(...data)
			// 	}
			// 	return testData
			// },
			searchClick(e) { //点击搜索框
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
115 116 117
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
118
					animationType: 'fade-in'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
119
				});
DCloud_JSON's avatar
DCloud_JSON 已提交
120 121 122 123 124 125 126 127 128 129 130 131
			},
			retry(){
				this.refresh()
			},
			refresh(){
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
132 133 134 135 136
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
DCloud_JSON's avatar
DCloud_JSON 已提交
137
			cdbRef.loadMore({clear: true})
DCloud_JSON's avatar
DCloud_JSON 已提交
138 139
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
140
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
141

DCloud_JSON's avatar
DCloud_JSON 已提交
142
<style scoped>
DCloud_JSON's avatar
DCloud_JSON 已提交
143 144 145 146 147 148
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
149 150 151 152 153 154
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
155
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169
	}

	.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 已提交
170 171 172 173 174 175 176 177
		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 已提交
178
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
179
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
180 181 182 183 184

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
185 186 187 188
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
189
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
190
	.show-refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
191 192 193
		transform: translateY(0);
		height: 40px;
		opacity: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	}

	.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;
	}
213 214 215 216
	.uni-list {}
	.f1{
		flex: 1;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
217
</style>