list.nvue 4.3 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
DCloud_JSON 已提交
2
	<view class="pagse">
3
		<!-- #ifndef H5 -->
4
		<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"
10
			field="avatar,title,last_modify_date,user_id{username}">
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
11
			<!-- 基于 uni-list 的页面布局 -->
12
			<uni-list class="uni-list" :border="false" :bounce="true" :alwaysScrollableVertical="true"  :style="{height:listHight}">
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
				<uni-list-item :to="'./detail?id='+item._id+'&title='+item.title"
					v-for="(item,index) in 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>
					</view>
				</uni-list-item>
				<uni-list-item>
DCloud_JSON's avatar
DCloud_JSON 已提交
30
					<template slot="body">
31
						<uni-load-state @networkResume="refresh" :state="{data,pagination,hasMore, loading, error}"></uni-load-state>
DCloud_JSON's avatar
DCloud_JSON 已提交
32
					</template>
33 34
				</uni-list-item>
			</uni-list>
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
35
		</unicloud-db>
DCloud_JSON's avatar
DCloud_JSON 已提交
36 37 38 39
	</view>
</template>

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

DCloud_JSON's avatar
DCloud_JSON 已提交
113 114 115 116
<style scoped>
	.pagse{
		background-color: #FFFFFF;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
117 118 119 120 121 122
	.avatar {
		width: 200rpx;
		height: 200rpx;
		margin-right: 10rpx;
	}

DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
123 124 125 126 127 128
	.main {
		justify-content: space-between;
	}

	.title {
		width: 480rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
129
		font-size: 32rpx;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
	}

	.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 已提交
144 145 146 147 148 149 150 151
		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 已提交
152
		transition: height 0.3s;
DCloud_JSON's avatar
DCloud_JSON 已提交
153
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
154 155 156 157 158

	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		top: 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
159 160 161 162
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
163
	}
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
164
	.show-refresh-tip {
DCloud_JSON's avatar
DCloud_JSON 已提交
165 166 167
		transform: translateY(0);
		height: 40px;
		opacity: 1;
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	}

	.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;
	}
187 188 189 190
	.uni-list {}
	.f1{
		flex: 1;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
191
</style>