list.nvue 5.8 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2 3 4 5 6 7 8 9 10
	<view>
		<!-- #ifdef APP-PLUS -->
		<uni-nav-bar :border="false"></uni-nav-bar>
		<!-- #endif -->
		<!-- 搜索功能 -->
		<uni-search-bar class="uni-search-box" v-model="keyword" ref="searchBar" radius="100"
			@click.native="searchClick" cancelButton="none" disabled />

		<unicloud-db ref='udb' v-slot:default="{data,pagination,hasMore, loading, error, options}" @load="handleLoad" @error="onqueryerror"
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
11 12 13
			:where="where" collection="opendb-news-articles,uni-id-users"
			field="avatar,title,last_modify_date,user_id{username}">
			<!-- 基于 uni-list 的页面布局 -->
DCloud_JSON's avatar
DCloud_JSON 已提交
14
			<uni-list class="uni-list" :border="false" :bounce="true" :alwaysScrollableVertical="true"
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
15
				<!-- #ifdef APP-NVUE -->
DCloud_JSON's avatar
DCloud_JSON 已提交
16
				 :style='{height:listHight+"px"}'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
17
				<!-- #endif -->
DCloud_JSON's avatar
DCloud_JSON 已提交
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
			>
				<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"
						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]" />
							</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
50
						</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
51 52 53 54 55 56 57 58 59 60 61 62
					</uni-list-item>

					<!-- 存在下一页数据,且不在加载中 通过 loadMore 组件实现上拉加载效果,如需自定义显示内容,可参考:https://ext.dcloud.net.cn/plugin?id=29 -->
					<template v-if="!loading">
						<cell v-if="hasMore">
							<uni-load-more :status="options.status"></uni-load-more>
						</cell>
						<cell v-else>
							<text class="noMore">- 没有更多数据了 -</text>
						</cell>
					</template>
				</template>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
63 64
			</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
				where: "",
				keyword: "",
				refreshState: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
76
				listHight: 0
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
77
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
78
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
79
		onReady() {
DCloud_JSON's avatar
DCloud_JSON 已提交
80
			this.listHight = uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight - 65
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
81 82
			cdbRef = this.$refs.udb
			console.log(cdbRef);
DCloud_JSON's avatar
DCloud_JSON 已提交
83
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
84
		onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
85 86 87 88 89 90 91 92 93 94 95 96
			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 已提交
97
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
98 99
		},
		methods: {
DCloud_JSON's avatar
DCloud_JSON 已提交
100
			testData(data) { //复制同一个数据多遍方便测试,正式项目中不要带
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
101 102 103 104 105
				var testData = []
				for (let i = 0; i < 10; i++) {
					testData.push(...data)
				}
				return testData
DCloud_JSON's avatar
DCloud_JSON 已提交
106
			},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
			handleLoad(data, ended, pagination) {
				// `data` 当前查询结果 `ended` 是否有更多数据 `pagination` 分页信息 HBuilderX 3.1.5+ 支持
				console.log(9527, data, ended, pagination);
				//上拉加载成功
			},
			searchClick() {
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
					animationType: 'fade-in',
					events: {
						acceptDataFromOpenedPage(e) {
							console.log(e);
						}
					}
				});
DCloud_JSON's avatar
DCloud_JSON 已提交
123 124 125 126 127 128 129 130 131 132 133 134
			},
			retry(){
				this.refresh()
			},
			refresh(){
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
135 136 137 138 139
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
DCloud_JSON's avatar
DCloud_JSON 已提交
140
			cdbRef.loadMore({clear: true})
DCloud_JSON's avatar
DCloud_JSON 已提交
141 142
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
143
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
144

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

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

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

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

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
188 189 190 191
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
192
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
193 194

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

	.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 已提交
218
	.uni-list {}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
219 220 221 222 223 224 225 226 227

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