list.nvue 5.3 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2
	<view class="pagse">
3 4
		<!-- #ifdef APP-PLUS -->
		<statusBar></statusBar>
DCloud_JSON's avatar
DCloud_JSON 已提交
5 6
		<!-- #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>
60 61 62 63
	var cdbRef, currentWebview;
	import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";
	export default {
		components: { statusBar },
DCloud_JSON's avatar
DCloud_JSON 已提交
64 65
		data() {
			return {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
66 67 68
				where: "",
				keyword: "",
				refreshState: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
69
				listHight: 0
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
70
			}
71 72 73 74 75 76 77 78 79
		},
		watch: {
			keyword(keyword, oldValue) {
				if(keyword){
					this.where = `/${keyword}/.test(title)`;
				}else{
					this.where = '';
				}
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
80
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
81
		onReady() {
82 83 84 85 86 87
			// #ifdef APP-NVUE
			this.listHight = uni.getSystemInfoSync().windowHeight - 96 + 'px'
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
			// #endif
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
88
			cdbRef = this.$refs.udb
DCloud_JSON's avatar
DCloud_JSON 已提交
89
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
90
		onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
91 92 93 94 95 96 97 98 99 100 101
			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 已提交
102
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
103 104
		},
		methods: {
105
			searchClick(e) { //点击搜索框
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
106 107 108
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
109
					animationType: 'fade-in'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
110
				});
DCloud_JSON's avatar
DCloud_JSON 已提交
111 112 113 114 115 116 117 118 119 120 121 122
			},
			retry(){
				this.refresh()
			},
			refresh(){
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
123 124 125 126 127
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
DCloud_JSON's avatar
DCloud_JSON 已提交
128
			cdbRef.loadMore({clear: true})
DCloud_JSON's avatar
DCloud_JSON 已提交
129 130
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
131
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
132

DCloud_JSON's avatar
DCloud_JSON 已提交
133 134 135 136
<style scoped>
	.pagse{
		background-color: #FFFFFF;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
137 138 139 140 141 142
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
143 144 145 146 147 148
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
149
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163
	}

	.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 已提交
164 165 166 167 168 169 170 171
		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 已提交
172
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
173
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
174 175 176 177 178

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

	.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;
	}
207 208 209 210
	.uni-list {}
	.f1{
		flex: 1;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
211
</style>