uniapp uni-table 表格封装 单选 多选 插槽 可复用等

<template>
	<view>
		<uni-table ref="table" :loading="loading" border stripe :type="type" emptyText="暂无更多数据"
			@selection-change="selectionChange">
			<uni-tr>
				<!-- single单选 -->
				<uni-th v-if="type =='single'" width="40" style="color: #fff;margin: auto;" align="center" />
				<uni-th v-for="(item, i) in columns" :key="i" :width="item.width"
					:align="item.align">{{item.title}}</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, i) in tableData" :key="i">
				<!-- single单选 -->
				<uni-td v-if="type =='single'" align="center">
					<view v-if="selectedIndex === i" @click="saveCombox(i)" :style="{
					background: '#007aff',
					width: '28rpx',
					height: '28rpx',
					position: 'relative',
					border: '1px solid #d1d1d1',
					margin: 'auto'
				}">
						<uni-icons color="#fff"
							style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);"
							v-if="selectedIndex === i" type="checkmarkempty" size="14" />
					</view>
					<view v-else @click="saveCombox(i)" :style="{
					background: 'none',
					width: '28rpx',
					height: '28rpx',
					position: 'relative',
					border: '1px solid #d1d1d1',
					margin: 'auto'
				}">
					</view>
				</uni-td>
				<uni-td v-for="(temp,i) in columns" :key="i" :style="{textAlign: temp.align}">
					<template v-if="temp.slot">
						<slot :name="temp.slot" :data="item"></slot>
					</template>
					<template v-else>
						<text>{{ item[temp.dataIndex] }}</text>
					</template>
				</uni-td>
			</uni-tr>
		</uni-table>
	</view>
</template>
export default {
		props: {
			type: {
				type: String,
				default: function() {
					return '' //selection 多选 single 单选
				}
			},
			columns: {
				type: Array,
				default: function() {
					return [{
						title: '姓名',
						align: 'center',
						width: 168,
						dataIndex: 'name',
						// slot  插槽
					}, {
						title: '年龄',
						align: 'center',
						width: 168,
						dataIndex: 'age'
					}, ]
				}
			},
			tableData: {
				type: Array,
				default: function() {
					return [{
						"name": "孟浩然",
						"age": "18"
					}, {
						"name": "王昌龄",
						"age": "22"
					}]
				}
			}
		},
		data() {
			return {
				loading: false,
				selectedIndex: '',
			}
		},
		methods: {
			// 多选
			selectionChange(e) {
				console.log(e)
			},
			// 单选
			saveCombox(e) {
				this.selectedIndex = e
				console.log(this.tableData[e])
				this.$emit('change', this.tableData[e])
			}
		}
	}

<style scoped lang="scss">
	/deep/ .uni-table-td {
		padding: 4px;
		font-size: 13px;
	}

	/deep/ .uni-table-th {
		background: #1989fa;
		color: #fff;
		font-size: 14px !important;
		font-weight: bold;
	}

	/deep/ .uni-table-tr:nth-child(1)>.checkbox {
		background: #1989fa;
	}
</style>

上面的只能用于H5端,
下面的可以在微信小程序和H5编译,但是由于小程序不支持动态插槽,就不是用的插槽方案

 
子组件
<template>
	<view>
		<uni-table ref="table" :loading="loading" border stripe :type="type" emptyText="暂无更多数据"
			@selection-change="selectionChange">
			<uni-tr>
				<!-- single单选 -->
				<uni-th v-if="type =='single'" width="40" style="color: #fff;margin: auto;" align="center" />
				<uni-th v-for="(item, i) in columns" :key="item.dataIndex" :width="item.width"
					:align="item.align">{{item.title}}</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, i) in tableData" :key="i">
				<!-- single单选 -->
				<uni-td v-if="type =='single'" align="center">
					<view v-if="selectedIndex === i" @click="saveCombox(i)" :style="{
					background: '#007aff',
					width: '28rpx',
					height: '28rpx',
					position: 'relative',
					border: '1px solid #d1d1d1',
					margin: 'auto'
				}">
						<uni-icons color="#fff"
							style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);"
							v-if="selectedIndex === i" type="checkmarkempty" size="14" />
					</view>
					<view v-else @click="saveCombox(i)" :style="{
					background: 'none',
					width: '28rpx',
					height: '28rpx',
					position: 'relative',
					border: '1px solid #d1d1d1',
					margin: 'auto'
				}">
					</view>
				</uni-td>
				<uni-td v-for="(temp,is) in columns" :key="is" :style="{textAlign: temp.align}">
					<!--自定义插槽 微信不可用 以json 格式来编译使用 -->
					<template v-if="temp.slot">
						<text v-for="(items,isi) in temp.renders " :key="isi" @click="detailsSave(items.function,i)"
							:style="[{color:items.color || ''}]">{{ items.name }}</text>
					</template>

					<template v-else-if="temp.filters">
						<text :style="[{color:temp.textColor[item[temp.dataIndex]] || ''}]">
							{{temp.filters[item[temp.dataIndex]] }}
						</text>
					</template>
					<template v-else>
						<text>{{ item[temp.dataIndex] }}</text>
					</template>



					<!-- 自定义动态插槽名 仅支持 H5 H5可以插槽 -->
					<!-- #ifdef H5 -->
					<!-- 			<template v-if="temp.slot">
						<slot :name="`tab-${temp.slot}`" :data="item" />
					</template>
					<template v-else-if="temp.filters">
						<text :style="{color:temp.textColor[item[temp.dataIndex]] || ''}">
							{{temp.filters[item[temp.dataIndex]] }}
						</text>
					</template>
					<template v-else>
						<text>{{ item[temp.dataIndex] }}</text>
					</template> -->
					<!-- #endif -->


				</uni-td>
			</uni-tr>
		</uni-table>
	</view>
</template>

<script>
	export default {
		props: {
			type: {
				type: String,
				default: function() {
					return '' //selection 多选 single 单选
				}
			},
			columns: {
				type: Array
			},
			tableData: {
				type: Array
			}
		},
		options: {
			dynamicSlots: true, // 启用动态 slot
			multipleSlots: true, // 启用动态 slot
		},
		data() {
			return {
				loading: false,
				selectedIndex: '',
			}
		},
		methods: {
			// 多选
			selectionChange(e) {
				console.log(e)
			},
			// 单选
			saveCombox(e) {
				this.selectedIndex = e
				console.log(this.tableData[e])
				this.$emit('change', this.tableData[e])
			},
			detailsSave(e, temp) {
				console.log(temp)
				console.log(e, this.tableData[temp])
				this.$emit(e, this.tableData[temp])
			}
		}
	}
</script>
父组件 引用
<Jtable :columns="columns" :tableData="tableData" type='' @handleDetail="handleDetail">

handleDetail//方法名 你自己命名就可以 
handleDetail(e){  <!-- e 就可以拿到当前点击的 列的整条数据-->}
{
		title: '错误信息',
		width: 44,
		align: 'center',
		slot: 'action',
		renders: [{
			name: '查看',
			color: '#1989FA',
			function: 'handleDetail'
		}]
}
//表头
columns: [{
		title: '检测类型名称',
		width: 58,
		align: 'center',
		dataIndex: 'name01'
		},
		{
		title: '检测结果',
		width: 45,
		align: 'center',
	    dataIndex: 'name04',
		filters: {
			0: '不通过',
			1: '通过'
		},
		textColor: {
			0: '#24A88F',
			1: '#E83544'
			}

		}, {
	    title: '错误信息',
			width: 44,
			align: 'center',
		    slot: 'action',
			renders: [{
				name: '查看',
				color: '#1989FA',
				function: 'handleDetail'
			}]
			}
	],
//数据
tableData: [{
    	"id": 0,
	    "name01": "生产班次结单检测",
	    "name04": 1,
    },
    {
	    "id": 1,
    	"name01": "生产班次结单检测",
    	"name04": 0
    }
]

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniappuni-table表格的使用可以通过设置rowspan和colspan参数来实现合并单元格的效果。这些参数可以直接在代码中使用,通过设置对应的值来控制合并的行数和列数。[1]举个例子,可以在uni-th标签中设置rowspan和colspan来合并单元格。比如,设置rowspan="3"表示该单元格纵向合并3行,而设置colspan="2"表示横向合并2列。这样就可以实现表格中的合并效果。 此外,uni-table还提供了一些关键的代码来实现其他功能。比如,可以使用toggleRowSelection方法来选中指定行的数据。通过传入选中行的index下标数组,可以在页面渲染完成后使用$nextTick方法调用toggleRowSelection来实现选中操作。这样就可以实现在uni-table中选中特定行的功能。 综上所述,uniappuni-table表格可以通过设置rowspan和colspan参数来实现合并单元格,同时还可以使用关键代码来实现其他功能,如选中行等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [uniapp uni-table表格组件 合并单元格](https://blog.csdn.net/qq_17627195/article/details/130227353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [uniapp uni ui uni-table表格 回显](https://blog.csdn.net/qq_22182989/article/details/129143508)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值