uni-app: Loop data click event to obtain specified data for each row (get parameters)

Effect

Page style

Click on the first line of console to output information

code

:data-id="item.id" : Define id information, e.currentTarget.dataset.id gets the id of the clicked row when the event is clicked

:data-index="index" : Define index information, e.currentTarget.dataset.index gets the index of the clicked row when the event is clicked

:data-item="item" : Define item information. When a click event occurs, e.currentTarget.dataset.item obtains the entire row information of the clicked row.

<template>
	<view>
		<view class="item_all" v-for="(item, index) in allinfo" :key="index">
			<view class='position' :data-id="item.id" :data-index="index" :data-item="item" @tap="deatil">
				<view class="vv_1">id: {
   
   {item.id}}</view>
				<view class="vv_1">name: {
   
   {item.name}}</view>
				<view class="vv_1">age: {
   
   {item.age}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				allinfo: [{
						id: 1,
						name: '张三',
						age: '12'
					},
					{
						id: 2,
						name: '李四',
						age: '21'
					},
					{
						id: 3,
						name: '王五',
						age: '44'
					},
				]
			};
		},
		methods: {
			deatil(e) {
				console.log("id:"+e.currentTarget.dataset.id)//获取行id信息
				console.log("index:"+e.currentTarget.dataset.index)//获取索引index信息
				console.log(e.currentTarget.dataset.item)//获取整行信息
			}
		}
	};
</script>
<style>
	.item_all {
		margin-bottom: 3%;
	}

	.position {
		padding: 6% 0;
		width: 100%;
		background-color: #fff;
		box-shadow: 4rpx 4rpx 4rpx gainsboro;
	}

	.vv_1 {
		margin: 0 5%;
		word-break: break-all;
	}
</style>

Guess you like

Origin blog.csdn.net/weixin_46001736/article/details/133356956