uni-table 中uni-td没有提供单元格点击事件的解决方法

1.重写一个uni-td

   增加@click="getRow"触发事件

   getRow(){
                this.$emit("row-click")

   }

<template>
	<!-- #ifdef H5 -->
	<td @click="getRow" class="uni-table-td" :rowspan="rowspan" :colspan="colspan" :class="{'table--border':border}" :style="{width:width + 'px','text-align':align}">
		<slot></slot>
	</td>
	<!-- #endif -->
	<!-- #ifndef H5 -->
	<!-- :class="{'table--border':border}"  -->
	<view @click="getRow" class="uni-table-td" :class="{'table--border':border}" :style="{width:width + 'px','text-align':align}">
		<slot></slot>
	</view>
	<!-- #endif -->
	
</template>

<script>
	/**
	 * Td 单元格
	 * @description 表格中的标准单元格组件
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=3270
	 * @property {Number} 	align = [left|center|right]	单元格对齐方式
	 */
	export default {
		name: 'uniTd',
		options: {
			virtualHost: true
		},
		props: {
			width: {
				type: [String, Number],
				default: ''
			},
			align: {
				type: String,
				default: 'left'
			},
			rowspan: {
				type: [Number,String],
				default: 1
			},
			colspan: {
					type: [Number,String],
				default: 1
			}
		},
		data() {
			return {
				border: false
			};
		},
		created() {
			this.root = this.getTable()
			this.border = this.root.border
		},
		methods: {
			getRow(){
				this.$emit("row-click")
			},
			/**
			 * 获取父元素实例
			 */
			getTable() {
				let parent = this.$parent;
				let parentName = parent.$options.name;
				while (parentName !== 'uniTable') {
					parent = parent.$parent;
					if (!parent) return false;
					parentName = parent.$options.name;
				}
				return parent;
			},
		}
	}
</script>

<style lang="scss">
	$border-color:#EBEEF5;

	.uni-table-td {
		display: table-cell;
		padding: 8px 10px;
		font-size: 14px;
		border-bottom: 1px $border-color solid;
		font-weight: 400;
		color: #606266;
		line-height: 23px;
		box-sizing: border-box;
	}

	.table--border {
		border-right: 1px $border-color solid;
	}
</style>

 2. 需要增加触发的父级标签uni-tr中增加点击事件@click="getRow"
重写的uni-td的定义为tableTd,增加 @row-click=""

<uni-table ref="table" :loading="loading" :border="false" :stripe="false" type="" emptyText="暂无更多数据" style="border-radius: 8px;">
	<uni-tr style="background-color: #D2E2F9;color: #000;">
	  <uni-th width="20%" align="center" style="color: #000;">风险等级</uni-th>
	  <uni-th width="20%" align="center" style="color: #000;">重点事件</uni-th>
	  <uni-th width="20%" align="center" style="color: #000;">重点人</uni-th>
	  <uni-th width="20%" align="center" style="color: #000;">重点物品</uni-th>
	  <uni-th width="20%" align="center" style="color: #000;">重点场所</uni-th>
    </uni-tr>
	<uni-tr v-for="(item, index) in tableData" :key="index" @click="getRow">
		<tableTd align="center" :style="item.style"  style="min-width: 76px;">{{ item.level }}</tableTd>
		<tableTd align="center" style="min-width: 76px;" @row-click="handelTableNavTo('eventNum',item.level)">{{ item.eventNum }}</tableTd>
		<tableTd align="center" style="min-width: 76px;" @row-click="handelTableNavTo('personNum',item.level)">{{ item.personNum }}</tableTd>
		<tableTd align="center" style="min-width: 76px;" @row-click="handelTableNavTo('placeNum',item.level)">{{ item.placeNum }}</tableTd>
		<tableTd align="center" style="min-width: 76px;" @row-click="handelTableNavTo('goodsNum',item.level)">{{ item.goodsNum }}</tableTd>
		</uni-tr>
</uni-table>

import tableTd from '@/component/table-td.vue'

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值