打工日记-Vue3+Ts二次封装el-table

el-table是elementUI中的表格组件,在后台把管理系统中,也是一个比较常用的组件,目前有一个比较好的开源项目ProTable,这个项目做的很好,集成了搜索,表格,分页器功能很强大。但在我的实际使用中也有一些需要更改的地方,因此,我也尝试着封装自己的table组件。

需求

  • 定制化:这是最基本的要求,每个表格的表头都是不一样的,封装table组件,首先就要满足它的定制化输入。
  • 表格操作列
  • 末尾添加一行

制作

定制化

让用户能够自定义配置列表项

思路

父子通信,将父组件配置好的columns传递给子组件,然后使用v-for遍历生成表格cell。

实现

首先我们需要在父组件得到传递过来的参数,使用defineProps接收

const props = defineProps({
	data: {
		type: Array,
		default: () => []
	},
	config: {
		type: Object,
		default: () => ({})
	}
}

在这里data的就是将来我们接收tabledata的,config则用来接收columns配置
将接收到的数据进行渲染

<el-table-column v-for="(item, index) in config.columns" :key="index"  :label="item.label" :width="item.width">
			<template #default="{ row }">
				<el-input class="inputBox" v-if="item.type === 'txt'" v-model="row[item.prop]" />
			</template>
</el-table-column>

似乎这样,就封装好了一个表格组件,但是实际使用中一点代码提示都没有,我们根本不知道需要组件预留的方法,我们似乎都没怎么使用到TS,因此我们来改造一下代码,为config添加一个config配置文件

type Type = "txt" | "tag";

export interface TableColumns<U> {
	type: Type;
	prop: keyof U;
	label: string;
	width?: number;
}

export interface TableConfigTest<T> {
	columns: TableColumns<T>[];
}

在这个配置文件中声明了两个TS接口,明确columns该怎么写,并且需要接收一个参数,将来使用组件时,将tabledata的类型接口传进来,就会自动解析prop(使用keyof实现,keyof 是 TypeScript 中的一个关键字,用于获取某个类型的所有属性名组成的联合类型)。

现在来重写一下congig,使用类型断言缩小config的类型

config: {
		type: Object as PropType<TableConfigTest<Object | any>>,
		default: () => ({})
	}

这样我们就完成组件的第一个需求,定制化。

表格操作列

因为我只需要一个删除行的操作,所以这里使用了一个简单的方法,就是在组件中直接书写操作

<el-table-column fixed="right" label="操作" width="120">
			<template #default="{ row }">
				<el-button type="danger" icon="el-icon-delete" @click="deleteRow(row)"> 删除 </el-button>
			</template>
</el-table-column>

然后将删除方法实现

const deleteRow = (row: any) => {
	const index = props.data.indexOf(row);
	if (index > -1) {
		props.data.splice(index, 1);
	}
};

使用演示

<MyTable :config="tableConfigEdu" :data="tableDataEdu" :border="true" />
<script setup lang="ts">
interface TableDataTrain {
	startDate: string;
	endDate: string;
	address: string;
	content: string;
}
const tableConfigTrain = ref<TableConfigTest<TableDataTrain>>({
	columns: [
		{
			type: "txt",
			prop: "startDate",
			label: "开始日期",
			width: 200
		},
		{
			type: "txt",
			prop: "endDate",
			label: "截止日期",
			width: 200
		},
		{
			type: "txt",
			prop: "address",
			label: "地点"
		},
		{
			type: "txt",
			prop: "content",
			label: "内容"
		}
	]
});
const tableDataTrain = ref<TableDataTrain[]>([{ startDate: "", endDate: "", address: "", content: "" }]);
</script>

封装组件是为了更好的工作,随着工作的需求增加,会不断增加组件的功能,同时也会更新文档。

gitee仓库

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一个基本的ElDialog封装,供你参考: ```vue <template> <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleClose" :close-on-click-modal="false" :custom-class="customClass" :width="width" :lock-scroll="lockScroll" :modal-append-to-body="modalAppendToBody" :destroy-on-close="destroyOnClose" :center="center" @opened="handleOpen" @closed="handleClosed" v-bind="$attrs" v-on="$listeners" > <slot></slot> </el-dialog> </template> <script lang="ts"> import { defineComponent } from 'vue'; import { ElDialog } from 'element-plus'; export default defineComponent({ name: 'MyDialog', props: { title: { type: String, default: '', }, dialogVisible: { type: Boolean, default: false, }, customClass: { type: String, default: '', }, width: { type: String, default: '50%', }, lockScroll: { type: Boolean, default: true, }, modalAppendToBody: { type: Boolean, default: true, }, destroyOnClose: { type: Boolean, default: false, }, center: { type: Boolean, default: true, }, }, emits: ['update:dialogVisible', 'opened', 'closed'], methods: { handleClose(done: () => void) { // 自定义关闭操作 done(); }, handleOpen() { this.$emit('opened'); }, handleClosed() { this.$emit('closed'); }, }, components: { ElDialog, }, }); </script> ``` 这里我们使用了Vue3的Composition API,使用`defineComponent`定义了一个组件,并引入了Element Plus的ElDialog组件。 我们将ElDialog组件的属性和事件通过props和emits暴露出来,并在组件内部进行了一些自定义操作,如自定义关闭操作和自定义事件触发。 你可以根据自己的需求对组件进行进一步封装和定制化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值