您好,登录后才能下订单哦!
ElementUI 是一套基于 Vue.js 的桌面端组件库,提供了丰富的 UI 组件,其中 el-table
是常用的表格组件。在实际开发中,我们经常需要在表格的表尾插入一行,用于显示汇总信息、统计结果或其他自定义内容。本文将详细介绍如何在 ElementUI 的 el-table
中实现表尾插入行,并提供多种实现方式和代码示例。
append
插槽ElementUI 的 el-table
组件提供了一个 append
插槽,可以在表格的最后插入自定义内容。这是实现表尾插入行最直接和推荐的方式。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
<template #append>
<tr class="custom-footer">
<td colspan="3">汇总信息</td>
</tr>
</template>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
};
</script>
<style>
.custom-footer {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
}
</style>
append
插槽:在表格的最后插入自定义内容。colspan
:合并单元格,确保汇总信息占据整行。footer
插槽ElementUI 的 el-table
组件还提供了一个 footer
插槽,可以在表格的底部插入自定义内容。与 append
插槽不同,footer
插槽的内容会显示在表格的底部,而不是表格的最后一行。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
<template #footer>
<div class="custom-footer">
汇总信息
</div>
</template>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
};
</script>
<style>
.custom-footer {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
padding: 10px;
}
</style>
footer
插槽:在表格的底部插入自定义内容。div
:使用 div
或其他 HTML 元素包裹内容。summary-method
属性ElementUI 的 el-table
组件提供了一个 summary-method
属性,可以在表格的最后一行显示汇总信息。这种方式适用于需要在表格的最后一行显示统计结果或汇总数据的场景。
<template>
<el-table :data="tableData" style="width: 100%" :summary-method="getSummaries">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
methods: {
getSummaries(param) {
const { columns, data } = param;
const sums = [];
columns.forEach((column, index) => {
if (index === 0) {
sums[index] = '汇总';
return;
}
if (index === 1) {
sums[index] = `总人数:${data.length}`;
return;
}
sums[index] = '';
});
return sums;
},
},
};
</script>
<style>
.el-table__footer-wrapper tbody td {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
}
</style>
summary-method
:自定义汇总方法,返回一个数组,数组中的每个元素对应表格的每一列。getSummaries
:自定义汇总方法,计算并返回汇总信息。footer
插槽与 summary-method
结合在某些场景下,我们可能需要在表格的底部显示汇总信息,并在表格的最后一行显示统计结果。此时,可以结合使用 footer
插槽和 summary-method
属性。
<template>
<el-table :data="tableData" style="width: 100%" :summary-method="getSummaries">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
<template #footer>
<div class="custom-footer">
汇总信息
</div>
</template>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
methods: {
getSummaries(param) {
const { columns, data } = param;
const sums = [];
columns.forEach((column, index) => {
if (index === 0) {
sums[index] = '汇总';
return;
}
if (index === 1) {
sums[index] = `总人数:${data.length}`;
return;
}
sums[index] = '';
});
return sums;
},
},
};
</script>
<style>
.custom-footer {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
padding: 10px;
}
</style>
footer
插槽:在表格的底部插入自定义内容。summary-method
:自定义汇总方法,返回一个数组,数组中的每个元素对应表格的每一列。footer
插槽和 summary-method
的优点。el-table-column
的 footer
插槽ElementUI 的 el-table-column
组件提供了一个 footer
插槽,可以在表格的每一列的底部插入自定义内容。这种方式适用于需要在每一列的底部显示不同汇总信息的场景。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180">
<template #footer>
<div class="custom-footer">
日期汇总
</div>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="180">
<template #footer>
<div class="custom-footer">
姓名汇总
</div>
</template>
</el-table-column>
<el-table-column prop="address" label="地址">
<template #footer>
<div class="custom-footer">
地址汇总
</div>
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
};
</script>
<style>
.custom-footer {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
padding: 10px;
}
</style>
footer
插槽:在表格的每一列的底部插入自定义内容。div
:使用 div
或其他 HTML 元素包裹内容。footer
插槽,代码复杂度较高。el-table-column
的 footer
插槽与 summary-method
结合在某些场景下,我们可能需要在表格的每一列的底部显示不同的汇总信息,并在表格的最后一行显示统计结果。此时,可以结合使用 el-table-column
的 footer
插槽和 summary-method
属性。
<template>
<el-table :data="tableData" style="width: 100%" :summary-method="getSummaries">
<el-table-column prop="date" label="日期" width="180">
<template #footer>
<div class="custom-footer">
日期汇总
</div>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="180">
<template #footer>
<div class="custom-footer">
姓名汇总
</div>
</template>
</el-table-column>
<el-table-column prop="address" label="地址">
<template #footer>
<div class="custom-footer">
地址汇总
</div>
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
methods: {
getSummaries(param) {
const { columns, data } = param;
const sums = [];
columns.forEach((column, index) => {
if (index === 0) {
sums[index] = '汇总';
return;
}
if (index === 1) {
sums[index] = `总人数:${data.length}`;
return;
}
sums[index] = '';
});
return sums;
},
},
};
</script>
<style>
.custom-footer {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
padding: 10px;
}
</style>
footer
插槽:在表格的每一列的底部插入自定义内容。summary-method
:自定义汇总方法,返回一个数组,数组中的每个元素对应表格的每一列。footer
插槽和 summary-method
的优点。el-table-column
的 footer
插槽与 append
插槽结合在某些场景下,我们可能需要在表格的每一列的底部显示不同的汇总信息,并在表格的最后一行插入自定义内容。此时,可以结合使用 el-table-column
的 footer
插槽和 append
插槽。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180">
<template #footer>
<div class="custom-footer">
日期汇总
</div>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="180">
<template #footer>
<div class="custom-footer">
姓名汇总
</div>
</template>
</el-table-column>
<el-table-column prop="address" label="地址">
<template #footer>
<div class="custom-footer">
地址汇总
</div>
</template>
</el-table-column>
<template #append>
<tr class="custom-footer">
<td colspan="3">汇总信息</td>
</tr>
</template>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
};
</script>
<style>
.custom-footer {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
padding: 10px;
}
</style>
footer
插槽:在表格的每一列的底部插入自定义内容。append
插槽:在表格的最后插入自定义内容。footer
插槽和 append
插槽的优点。el-table-column
的 footer
插槽与 footer
插槽结合在某些场景下,我们可能需要在表格的每一列的底部显示不同的汇总信息,并在表格的底部插入自定义内容。此时,可以结合使用 el-table-column
的 footer
插槽和 footer
插槽。
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180">
<template #footer>
<div class="custom-footer">
日期汇总
</div>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="180">
<template #footer>
<div class="custom-footer">
姓名汇总
</div>
</template>
</el-table-column>
<el-table-column prop="address" label="地址">
<template #footer>
<div class="custom-footer">
地址汇总
</div>
</template>
</el-table-column>
<template #footer>
<div class="custom-footer">
汇总信息
</div>
</template>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ date: '2023-01-01', name: '张三', address: '北京市' },
{ date: '2023-01-02', name: '李四', address: '上海市' },
{ date: '2023-01-03', name: '王五', address: '广州市' },
],
};
},
};
</script>
<style>
.custom-footer {
background-color: #f5f7fa;
font-weight: bold;
text-align: center;
padding: 10px;
}
</style>
footer
插槽:在表格的每一列的底部插入自定义内容。免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。