vue3+elementplus封装表格和分页

组件封装

<template>
	<div class="table">
		<el-table
			:data="tableData"
			border
			max-height="650"
			style="width: 100%"
			v-loading="loading"
			@selection-change="selectionChange"
		>
			<el-table-column type="selection" width="50" />
			<template v-for="(item, index) in tableHeader" :key="item.prop">
				<el-table-column
					:prop="item.prop"
					:label="item.label"
					:fixed="item.fixed"
					:align="item.align || 'center'"
					:show-overflow-tooltip="item.overHidden || false"
					:min-width="item.minWidth || '100px'"
					:sortable="item.sortable || false"
					:type="item.type"
					:width="item.width"
				>
					<!-- 插槽:预留功能 -->
					<template #default="scope" v-if="item.slot">
						<slot :name="item.slot" :data="scope.row"></slot>
					</template>
					<template #default="scope" v-else>
						<span>{{ scope.row[item.prop] ? scope.row[item.prop] : "" }}</span>
					</template>
				</el-table-column>
			</template>
		</el-table>
		<!-- 分页 -->
		<el-pagination
			:locale="locale"
			background
			class="pagination"
			small
			:page-sizes="pageSizesArr"
			@size-change="sizeChange"
			@current-change="currentChange"
			:current-page="current"
			:page-size="size"
			:total="total"
			:layout="layout"
		/>
	</div>
</template>
<script setup>
import { defineProps, defineEmits, onMounted, reactive, ref } from "vue";

const emits = defineEmits(["selectionChange", "sizeChange", "currentChange"]);
const props = defineProps({
	// 表格显示的数据
	tableData: {
		type: String,
		default: function () {
			return "";
		}
	},
	// 表头数据
	tableHeader: {
		type: String,
		default: function () {
			return [];
		}
	},
	// 总页数
	total: {
		type: Number,
		// 类型
		required: true,
		default: 0
	},
	// 分页的页容量数组
	pageSizesArr: {
		type: Array,
		default() {
			return [20, 40, 50];
		}
	},
	// 分页的布局
	layout: {
		type: String,
		default: "total, sizes, prev, pager, next, jumper"
	}
});
//表格事件
const selectionChange = val => {
	emits("selectionChange", val);
};

// 页数改变的时候触发的事件
const sizeChange = val => {
	emits("handleSizeChange", val);
};
// 当前页改变的时候触发的事件
const currentChange = val => {
	emits("handleCurrentChange", val);
};
</script>
<style scoped>
.table {
	margin-top: 1rem;
	height: 100%;
}
.pagination {
	float: right;
	margin-top: 1.25rem;
	margin-bottom: 1.25rem;
}
</style>

使用

import CommonTable from "组件名";
<CommonTable
	:isOperate="isOperate"
	:operateWidth="operateWidth"
	:tableData="tableData"
	:tableHeader="tableHeader"
	:total="total"
	@handleCurrentChange="currentChange"
	@handleSizeChange="sizeChange"
>
	<template #isShow="{ data }">
		<el-tag v-if="data.isShow == true" type="success">显示</el-tag>
		<el-tag v-if="data.isShow == false" type="info">隐藏</el-tag>
	</template>
	<template #action="{ data }">
		<el-tag size="mini" style="cursor: pointer; margin-right: 5px" type="success" @click="updateCommentReview(data)">
			编辑
		</el-tag>
		<el-tag v-if="action == 0" size="mini" style="cursor: pointer" type="error" @click="removeCommentReview(data)">
			删除
		</el-tag>
	</template>
</CommonTable>

const tableHeader = reactive([
	{
		prop: "title",
		fixed: "left",
		label: "页面名称",
		width: "130px",
		align: "center" // 对齐方式
	},
	{
		prop: "name",
		label: "页面ID",
		width: "130px",
		align: "center" // 对齐方式
	},
	{
		prop: "component",
		label: "本地路径",
		width: "130px",
		align: "left" // 对齐方式
	},
	{
		prop: "icon",
		label: "图标",
		width: "130px",
		align: "left" // 对齐方式
	},
	{
		prop: "path",
		label: "路由地址",
		width: "130px",
		align: "left" // 对齐方式
	},
	{
		prop: "isShow",
		label: "是否显示",
		slot: "isShow", //插槽根据需求显示
		width: "130px"
	},
	{
		label: "操作",
		slot: "action",
		fixed: "right",
		width: "130px",
		align: "center" // 对齐方式
	}
]);

const operateWidth = ref(160); // 操作列宽度
const isOperate = ref(true); // 操作列是否显示
let pageSize = ref(20); // 每页显示条数
let pageNum = ref(1); // 当前页码
let total = ref(0); // 总条数
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的乌龟哟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值