vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程
公司渲染表格數(shù)據(jù)時(shí)需要將空數(shù)據(jù)顯示‘-’,并且對(duì)于每一列數(shù)據(jù)的顯示也有一定的要求,基于這個(gè)需求對(duì)element-plus簡(jiǎn)單進(jìn)行了二次封裝。
具體包括以下幾點(diǎn)(持續(xù)更新中):
1.空數(shù)據(jù)顯示‘-’
2.固定表格高度
3.支持多選表格
4. 自定義列寬
<template>
<div>
<el-table
:data="dataSource"
v-loading="loading"
:height="vdaH"
:max-height="vdaH"
:fit="fit"
:border="border"
:header-cell-class-name="headerCellClassName"
highlight-current-row
:tooltip-options="{
effect: 'dark',
placement: 'bottom',
showArrow: true,
}"
show-overflow-tooltip
@selection-change="handleSelectionChange"
>
<el-table-column
v-if="isMoreSelect"
type="selection"
width="55"
:selectable="handleSelectable"
/>
<el-table-column type="index" label="序號(hào)" width="55" />
<template v-for="(column, index) in columns" :key="index">
<el-table-column
show-overflow-tooltip
v-if="column.scopeVal"
:prop="column.prop"
:label="column.label"
:min-width="column.width || column.label.length * 20 + 20"
>
<template #default="scope">
<slot
:column="column"
:record="scope.row"
:text="scope.row[column.prop]"
:index="dataSource.indexOf(scope.row)"
:name="column.prop"
>
</slot>
</template>
</el-table-column>
<!-- :min-width="column.width || column.label.length * 20 + 20" -->
<el-table-column
v-else
:prop="column.prop"
:label="column.label"
:min-width="
column.width ||
getColumnWidth(column.label, column.prop, dataSource)
"
>
<template #default="{ row }">
{{ checkEmpty(row[column.prop]) }}
</template>
</el-table-column>
</template>
<!-- 操作 -->
<el-table-column
v-if="!hideOperation"
fixed="right"
label="操作"
align="center"
:width="operationWidth"
>
<template #default="scope">
<slot v-bind="scope"></slot>
</template>
</el-table-column>
</el-table>
<div class="pagination">
<el-pagination
v-show="totalNum > 0"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
v-model:current-page.sync="page"
:page-sizes="[10, 20, 50, 100]"
v-model:page-size="size"
layout="total, sizes, prev, pager, next, jumper"
:total="totalNum"
background
small
/>
</div>
</div>
</template>
<script lang="ts" setup>
import { checkEmpty, getColumnWidth } from "@/utils/util";
const props = defineProps({
dataSource: {
type: Array<any>,
default: () => [],
},
columns: {
type: Array<any>,
default: () => [],
},
vdaH: {
type: Number,
default: 300,
},
hideOperation: {
type: Boolean,
default: false,
},
operationWidth: {
type: String,
default: "100",
},
loading: {
type: Boolean,
default: false,
},
//是否多選顯示
isMoreSelect: {
type: Boolean,
default: false,
},
fit: {
type: Boolean,
default: true,
},
border: {
type: Boolean,
default: false,
},
headerCellClassName: {
type: String,
default: "custmorTableHeader",
},
// 當(dāng)前頁
currentPage: {
type: Number,
default: 0,
},
// 展示頁數(shù)
pageSize: {
type: Number,
default: 0,
},
//總頁數(shù)
totalNum: {
type: Number,
default: 0,
},
//多選
handleSelection: {
type: Function,
default: () => {},
},
});
// // 測(cè)試列寬
// /**
// * el-table擴(kuò)展工具 -- 列寬度自適應(yīng)
// * @param {*} prop 字段名稱(string)
// * @param {*} records table數(shù)據(jù)列表集(array)
// * @returns 列寬(int)
// */
// function getColumnWidth(prop: string, records: any) {
// const minWidth = 80; // 最小寬度
// const padding = 12; // 列內(nèi)邊距
// const contentWidths = records.map((item: any) => {
// console.log("item", item);
// console.log("PROP", prop);
// const value = item[prop] ? String(item[prop]) : "";
// const textWidth = getTextWidth(value);
// return textWidth + padding;
// });
// console.log("contentWidths", contentWidths);
// let maxWidth = Math.max(...contentWidths);
// if (maxWidth > 240) {
// maxWidth = 240;
// }
// return Math.max(minWidth, maxWidth);
// }
// /**
// * el-table擴(kuò)展工具 -- 列寬度自適應(yīng) - 獲取列寬內(nèi)文本寬度
// * @param {*} text 文本內(nèi)容
// * @returns 文本寬度(int)
// */
// function getTextWidth(text: string) {
// const span = document.createElement("span");
// span.style.visibility = "hidden";
// span.style.position = "absolute";
// span.style.top = "-9999px";
// span.style.whiteSpace = "nowrap";
// span.innerText = text;
// document.body.appendChild(span);
// const width = span.offsetWidth + 5;
// document.body.removeChild(span);
// return width;
// }
// ...其他方法
const emit = defineEmits([
"pagination",
"update:currentPage",
"update:pageSize",
"selection-change",
]);
const page = useVModel(props, "currentPage", emit);
const size = useVModel(props, "pageSize", emit);
function handleSizeChange(val: number) {
emit("pagination", { currentPage: page, pageSize: val });
}
function handleCurrentChange(val: number) {
// console.log("val", val);
page.value = val;
emit("pagination", { currentPage: val, pageSize: props.pageSize });
}
const handleSelectionChange = (val: any) => {
emit("selection-change", val);
};
const handleSelectable = (row: any) => {
// console.log("row", row);
return row.selectable;
};
</script>
<style lang="scss" scoped>
.pagination {
display: flex;
justify-content: end;
padding: 12px;
margin-top: 5px;
&.hidden {
display: none;
}
}
</style>對(duì)于表格列寬實(shí)現(xiàn)了根據(jù)數(shù)據(jù)長(zhǎng)度進(jìn)行每一列的展示:
/**
* el-table擴(kuò)展工具 -- 列寬度自適應(yīng)
* @param {*} prop 字段名稱(string)
* @param {*} records table數(shù)據(jù)列表集(array)
* @returns 列寬(int)
*/
export function getColumnWidth(label: string, prop: string, tableData: any) {
//label表頭名稱
//prop對(duì)應(yīng)的內(nèi)容
//tableData表格數(shù)據(jù)
const minWidth = 90; // 最小寬度
const padding = 10; // 列內(nèi)邊距
const arr = tableData.map((item: any) => item[prop]);
arr.push(label); //拼接內(nèi)容和表頭數(shù)據(jù)
const contentWidths = arr.map((item: any) => {
// console.log("item", item);
// console.log("PROP", prop);
const value = item ? String(item) : "";
const textWidth = getTextWidth(value);
return textWidth + padding;
});
// console.log("contentWidths", contentWidths);
let maxWidth = Math.max(...contentWidths);
if (maxWidth > 240) {
maxWidth = 240;
}
return Math.max(minWidth, maxWidth);
}
/**
* el-table擴(kuò)展工具 -- 列寬度自適應(yīng) - 獲取列寬內(nèi)文本寬度
* @param {*} text 文本內(nèi)容
* @returns 文本寬度(int)
*/
function getTextWidth(text: string) {
const span = document.createElement("span");
span.style.visibility = "hidden";
span.style.position = "absolute";
span.style.top = "-9999px";
span.style.whiteSpace = "nowrap";
span.innerText = text;
document.body.appendChild(span);
const width = span.offsetWidth + 5;
document.body.removeChild(span);
return width;
}到此這篇關(guān)于vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程的文章就介紹到這了,更多相關(guān)vue表格二次封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3?element-plus?實(shí)現(xiàn)表格數(shù)據(jù)更改功能詳細(xì)步驟
- Vue3實(shí)現(xiàn)Element Plus表格的多選功能與條件操作
- 基于Vue3和Element Plus實(shí)現(xiàn)可擴(kuò)展的表格組件
- Vue3?使用Element?Plus表格單選帶checkbox功能
- vue3+element?Plus實(shí)現(xiàn)表格前端分頁完整示例
- element-plus+Vue3實(shí)現(xiàn)表格數(shù)據(jù)動(dòng)態(tài)渲染
- Vue3中的element-plus表格實(shí)現(xiàn)代碼
- vue3 + ElementPlus 封裝列表表格組件包含分頁
- Vue3+Element Plus 通用表格組件封裝與使用實(shí)踐
相關(guān)文章
vue3實(shí)現(xiàn)點(diǎn)擊空白區(qū)域隱藏div
這篇文章主要介紹了vue3實(shí)現(xiàn)點(diǎn)擊空白區(qū)域隱藏div方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Vue 固定頭 固定列 點(diǎn)擊表頭可排序的表格組件
這篇文章主要介紹了Vue 固定頭 固定列 點(diǎn)擊表頭可排序的表格組件的相關(guān)資料,需要的朋友可以參考下2016-11-11
vue+Java后端進(jìn)行調(diào)試時(shí)解決跨域問題的方式
今天在開發(fā)中遇到有點(diǎn)小問題,vue+Java后端進(jìn)行調(diào)試時(shí)如何解決跨域問題,下面小編給大家分享解決方法,感興趣的朋友一起看看吧2017-10-10
通過Element ui往頁面上加一個(gè)分頁導(dǎo)航條的方法
這篇文章主要介紹了通過Element ui往頁面上加一個(gè)分頁導(dǎo)航條的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
vue3+vite使用History路由模式打包部署項(xiàng)目的步驟及注意事項(xiàng)
這篇文章主要介紹了vue3+vite使用History路由模式打包部署項(xiàng)目的步驟及注意事項(xiàng),配置過程包括在Vue項(xiàng)目中設(shè)置路由模式、調(diào)整打包配置以及Nginx服務(wù)器的配置,正確的部署配置能夠確保應(yīng)用順利運(yùn)行,提升用戶體驗(yàn),需要的朋友可以參考下2024-10-10
Vue?CLI項(xiàng)目遷移eslint報(bào)錯(cuò)解決辦法及最佳實(shí)踐
ESLint是一個(gè)ECMAScript/JavaScript語法規(guī)則和代碼風(fēng)格的檢查工具,它的目標(biāo)是保證代碼的一致性和避免錯(cuò)誤,這篇文章主要介紹了Vue?CLI項(xiàng)目遷移eslint報(bào)錯(cuò)解決辦法及最佳實(shí)踐的相關(guān)資料,需要的朋友可以參考下2025-09-09
vue3.x使用swiperUI動(dòng)態(tài)加載圖片失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了vue3.x使用swiperUI動(dòng)態(tài)加載圖片失敗的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
解決el-tree數(shù)據(jù)回顯時(shí)子節(jié)點(diǎn)部分選中父節(jié)點(diǎn)都全選中的坑
本文主要介紹了解決el-tree數(shù)據(jù)回顯時(shí)子節(jié)點(diǎn)部分選中父節(jié)點(diǎn)都全選中的坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue創(chuàng)建項(xiàng)目卡住不動(dòng),vue?create?project卡住不動(dòng)的解決
這篇文章主要介紹了vue創(chuàng)建項(xiàng)目卡住不動(dòng),vue?create?project卡住不動(dòng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

