list.nvue 5.5 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3 4 5 6
	<view>
		<!-- #ifdef APP-PLUS -->
		<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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
				<cell v-if="error">
					<template slot="body">
						<!-- 设置网络 -->
						<uni-network @change="refresh" @retry="refresh"></uni-network>
					</template>
				</cell>
				
				<template v-else>
					<cell class="get-data-state" v-if="data.length===0&&pagination.current===1">
						<!-- 数据为空 当前页码为1,且正在加载中;这里为了演示,更加直观的表达内部逻辑。商用项目建议将这部分封装为组件,更好的让业务逻辑与功能分离-->
						<uni-load-more v-if="loading" status="loading"></uni-load-more>
						<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 :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 48
					<!-- 存在下一页数据 && 不是正在加载中 && 已经满一页; 通过 loadMore 组件实现上拉加载效果,如需自定义显示内容,可参考:https://ext.dcloud.net.cn/plugin?id=29 -->
					<template v-if="!loading&&data.length>10">
DCloud_JSON's avatar
DCloud_JSON 已提交
49
						<cell v-if="hasMore">
50
							<uni-load-more status="more"></uni-load-more>
DCloud_JSON's avatar
DCloud_JSON 已提交
51
						</cell>
52 53
						<cell v-else>
							<uni-load-more status="noMore"></uni-load-more>
DCloud_JSON's avatar
DCloud_JSON 已提交
54 55 56
						</cell>
					</template>
				</template>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
57 58
			</uni-list>
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
59 60 61 62
	</view>
</template>

<script>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
63
	var cdbRef, currentWebview;
DCloud_JSON's avatar
DCloud_JSON 已提交
64 65 66
	export default {
		data() {
			return {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
67 68 69
				where: "",
				keyword: "",
				refreshState: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
70
				listHight: 0
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
71
			}
72 73 74 75 76 77 78 79 80
		},
		watch: {
			keyword(keyword, oldValue) {
				if(keyword){
					this.where = `/${keyword}/.test(title)`;
				}else{
					this.where = '';
				}
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
81
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
82
		onReady() {
83 84 85 86 87 88
			// #ifdef APP-NVUE
			this.listHight = uni.getSystemInfoSync().windowHeight - 96 + 'px'
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
			// #endif
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
89 90
			cdbRef = this.$refs.udb
			console.log(cdbRef);
DCloud_JSON's avatar
DCloud_JSON 已提交
91
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
92
		onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
93 94 95 96 97 98 99 100 101 102 103 104
			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 已提交
105
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
106 107
		},
		methods: {
108 109 110 111 112 113 114 115
			// testData(data) { //复制同一个数据多遍方便测试,正式项目中不要带
			// 	var testData = []
			// 	for (let i = 0; i < 10; i++) {
			// 		testData.push(...data)
			// 	}
			// 	return testData
			// },
			searchClick(e) { //点击搜索框
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
116 117 118
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
119
					animationType: 'fade-in'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
120
				});
DCloud_JSON's avatar
DCloud_JSON 已提交
121 122 123 124 125 126 127 128 129 130 131 132
			},
			retry(){
				this.refresh()
			},
			refresh(){
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
133 134 135 136 137
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
DCloud_JSON's avatar
DCloud_JSON 已提交
138
			cdbRef.loadMore({clear: true})
DCloud_JSON's avatar
DCloud_JSON 已提交
139 140
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
141
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
142

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

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

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

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

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

	.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;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
214
	.uni-list {}
DCloud_JSON's avatar
DCloud_JSON 已提交
215
</style>