long-list.uvue 3.0 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<template>
	<view style="width: 100%;height: 100%;">
		<list class="uni-list" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
			:refresher-triggered="refresherTriggered">
			<cell v-for="(value, index) in listData" :key="index">
				<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>
			</cell>
		</list>
	</view>
</template>

<script>
23
import { readFile, ReadFileOptions,ReadFileSuccessResult } from '@/uni_modules/uts-file-manager';
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
24 25 26 27 28 29 30
	type Item = {
		title: string
		subTitle: string,
		img: string,
		detail: string,
	}

31
    
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	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() {
50 51 52 53 54 55 56 57
                readFile({
					type:'text',
					path:'static/list-mock/mock.json',
					success:function(result:ReadFileSuccessResult){
						const content = result.content;
						if (content != null){
							const items = [] as Item[];
							const jsonArr = JSON.parse(content);
杜庆泉's avatar
杜庆泉 已提交
58 59
							jsonArr?.forEach((res)=>{
								const json = res as UTSJSONObject;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
								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);
							})
							let temp = [] as Item[];
							for(let i = 0; i < 100; i++){
								temp = temp.concat(items);
							}
							listData = temp;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
77
						}
78 79 80 81 82 83
						setTimeout(()=>{
							this.refresherTriggered = false
						},0);
					},
					complete:function(_){
						
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
84
					}
85
				} as ReadFileOptions)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
86 87 88
			},
			goDetail(e: Item) {
				uni.navigateTo({
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
89
					url: 'pages/component/long-list/detail/detail?content=' + e.detail
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
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 130 131 132 133 134 135 136
				});
			},
			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;
	}
	
	.uni-media-list-text {
		color: #9D9D9F;
		font-size: 25rpx;
	}
</style>