long-list.uvue 3.1 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1 2
<template>
	<view style="width: 100%;height: 100%;">
3
		<list-view class="uni-list" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
张磊 已提交
4
			:refresher-triggered="refresherTriggered" scroll-y = true>
5
			<list-item v-for="(value, index) in listData" :key="index">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
6 7 8 9 10 11 12 13 14 15 16
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="goDetail(value)">
					<view class="uni-media-list">
						<image class="uni-media-list-logo" :src="value.img"></image>
						<view class="uni-media-list-body">
							<text class="uni-media-list-text-top">{{ value.title }}</text>
							<view class="uni-media-list-text-bottom">
								<text class="uni-media-list-text">{{ value.subTitle }}</text>
							</view>
						</view>
					</view>
				</view>
17 18
			</list-item>
		</list-view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
19 20 21 22 23 24 25 26 27 28 29
	</view>
</template>

<script>
	type Item = {
		title: string
		subTitle: string,
		img: string,
		detail: string,
	}

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
30

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	export default {
		data() {
			return {
				refresherTriggered: false,
				listData: [] as Item[],
				last_id: '',
				pageVisible: false
			};
		},
		onLoad() {
			this.pageVisible = true;
			this.getList();
		},
		onUnload() {
			this.pageVisible = false;
		},
		methods: {
			getList() {
49 50 51 52
				const fileManager = uni.getFileSystemManager()
                fileManager.readFile({
					encoding:'utf-8',
					filePath:'static/list-mock/mock.json',
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
53 54
					success:function(result){
						const content = result.data;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
55
						const items = [] as Item[];
56 57
						const jsonArr = JSON.parse<Array<UTSJSONObject>>(content);
						if(jsonArr != null){
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70
              jsonArr.forEach((json) => {
                const title = json["title"] as string;
                const subTitle = json["subTitle"] as string;
                const img = json["img"] as string;
                const detail = json["detail"] as string;
                const item:Item = {
                	title,
                	subTitle,
                	img,
                	detail
                }
                items.push(item);
              })
71 72 73 74
							let temp = [] as Item[];
							for(let i = 0; i < 100; i++){
								temp = temp.concat(items);
							}
75
							this.listData = temp;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
76
						}
77 78 79 80 81
						setTimeout(()=>{
							this.refresherTriggered = false
						},0);
					},
					complete:function(_){
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
82

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
83
					}
84
				} as ReadFileOptions)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
85 86 87
			},
			goDetail(e: Item) {
				uni.navigateTo({
88
					url: '/pages/component/long-list/detail/detail?content=' + e.detail
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
				});
			},
			onRefresherrefresh() {
				if(this.pageVisible){
					this.refresherTriggered = true
					this.getList();
				}
			}
		}
	};
</script>

<style>
	.uni-media-list {
		padding: 22rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		width: 100%;
		flex-direction: row;
	}

	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
	}

	.uni-media-list-body {
		flex: 1;
		padding-left: 15rpx;
	}

	.uni-media-list-text-top {
		font-size: 28rpx;
		lines:2;
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
		margin-top: 10rpx;
	}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
130

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
131 132 133 134
	.uni-media-list-text {
		color: #9D9D9F;
		font-size: 25rpx;
	}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
135
</style>