list.nvue 4.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
	<view class="pages">
3 4 5
		<!-- #ifndef H5 -->
		<statusBar></statusBar>
		<!-- #endif -->
6
		<!-- 搜索功能 -->
7 8
		<uni-search-bar @click="searchClick" class="uni-search-box" v-model="keyword" ref="searchBar" radius="100"
			cancelButton="none" disabled />
9 10
		<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"
11
			field="avatar,title,last_modify_date,user_id.username">
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
12
			<!-- 基于 uni-list 的页面布局 -->
13 14
			<uni-list class="uni-list" :border="false" :bounce="true" :alwaysScrollableVertical="true"
				:style="{height:listHight}">
15
				<uni-list-item :to="'/pages/list/detail?id='+item._id+'&title='+item.title" v-for="(item,index) in data"
16
					:key="index">
17 18 19 20 21 22 23 24 25
					<!-- 通过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>
26 27
							<uni-dateformat class="last_modify_date" :date="item.last_modify_date" format="yyyy-MM-dd"
								:threshold="[60000, 2592000000]" />
28 29 30
						</view>
					</view>
				</uni-list-item>
31 32 33 34 35
				<uni-list-item>
					<template slot="body">
						<uni-load-state @networkResume="refresh" :state="{data,pagination,hasMore, loading, error}">
						</uni-load-state>
					</template>
36
				</uni-list-item>
37
			</uni-list>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
38
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
39 40 41 42
	</view>
</template>

<script>
43
	var cdbRef, currentWebview;
44
	import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";
45 46 47 48
	export default {
		components: {
			statusBar
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
49 50
		data() {
			return {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
51 52 53
				where: "",
				keyword: "",
				refreshState: 0,
DCloud_JSON's avatar
DCloud_JSON 已提交
54
				listHight: 0
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
55
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
56
		},
57 58 59 60 61 62 63 64 65
		watch: {
			keyword(keyword, oldValue) {
				if (keyword) {
					this.where = `/${keyword}/.test(title)`;
				} else {
					this.where = '';
				}
			}
		},
66
		async onReady() {
67 68 69 70 71
			// #ifdef APP-NVUE
			this.listHight = uni.getSystemInfoSync().windowHeight - 96 + 'px'
			// #endif
			// #ifndef APP-NVUE
			this.listHight = 'auto'
72
			// #endif
73
			cdbRef = this.$refs.udb
DCloud_JSON's avatar
DCloud_JSON 已提交
74
		},
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
75
		onShow() {
DCloud_JSON's avatar
DCloud_JSON 已提交
76 77
			this.keyword = getApp().globalData.searchText
			getApp().globalData.searchText = ''
78 79 80 81 82 83 84 85 86
			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 已提交
87
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
88 89
		},
		methods: {
90
			searchClick(e) { //点击搜索框
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
91 92 93
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
94
					animationType: 'fade-in'
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
95
				});
96 97 98 99 100 101 102 103 104 105 106
			},
			retry() {
				this.refresh()
			},
			refresh() {
				cdbRef.loadData({
					clear: true
				}, () => {
					uni.stopPullDownRefresh()
				})
				console.log('refresh');
DCloud_JSON's avatar
DCloud_JSON 已提交
107
			}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
108 109 110 111 112
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {
113 114 115
			cdbRef.loadMore({
				clear: true
			})
DCloud_JSON's avatar
DCloud_JSON 已提交
116 117
		}
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
118
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
119

120
<style scoped>
121
	.pages {
122
		background-color: #FFFFFF;
DCloud_JSON's avatar
DCloud_JSON 已提交
123
	}
124

DCloud_JSON's avatar
DCloud_JSON 已提交
125 126 127 128 129 130
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
131 132 133 134 135 136
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
137
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151
	}

	.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 已提交
152 153 154 155 156 157 158 159
		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 已提交
160
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
161
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
162 163 164 165 166

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
167 168 169 170
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
171 172
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
173
	.show-refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
174 175 176
		transform: translateY(0);
		height: 40px;
		opacity: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	}

	.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;
	}
196 197 198 199 200

	.uni-list {}

	.f1 {
		flex: 1;
201
	}
202
</style>