- <template>
- <div>
- <el-table show-summary :summary-method="getSummaries" :span-method="objectSpanMethod" :data="tableData" row-key="id" ref="tableDom" border>
- <el-table-column label="序号" width="55" align="center">
- <template slot-scope="scope">{{ scope.$index + 1 }}</template>
- </el-table-column>
- <el-table-column prop="name" label="分类" align="center"></el-table-column>
- <el-table-column prop="num1" label="数量1" align="center"></el-table-column>
- <el-table-column prop="num2" label="数量2" align="center"></el-table-column>
- <el-table-column prop="num3" label="数量3" align="center"></el-table-column>
- <el-table-column prop="type" label="类型" align="center"></el-table-column>
- <el-table-column prop="num4" label="数量4" align="center"></el-table-column>
- <el-table-column prop="num5" label="数量5" align="center"></el-table-column>
- </el-table>
- </div>
- </template>
- <script>
- export default {
- name: '',
- components: {},
- props: {},
- data() {
- return {
- tableData: [{
- name: '名称1',
- num1: '2',
- num2: '3',
- num3: '4',
- num4: '连续',
- num5: '6',
- num6: '7',
- }, {
- name: '名称2',
- num1: '2',
- num2: '3',
- num3: '4',
- num4: '连续',
- num5: '6',
- num6: '7',
- }, {
- name: '名称2',
- num1: '2',
- num2: '3',
- num3: '4',
- num4: '连续',
- num5: '6',
- num6: '7',
- }, {
- name: '名称3',
- num1: '2',
- num2: '3',
- num3: '4',
- num4: '连续',
- num5: '6',
- num6: '7',
- }, {
- name: '名称4',
- num1: '2',
- num2: '3',
- num3: '4',
- num4: '连续',
- num5: '6',
- num6: '7',
- }, {
- name: '名称4',
- num1: '2',
- num2: '3',
- num3: '4',
- num4: '连续',
- num5: '6',
- num6: '7',
- }]
- }
- },
- filters: {},
- computed: {},
- watch: {},
- created() {},
- mounted() {},
- beforeDestroy() {},
- methods: {
- // 合计
- getSummaries (param) {
- const { columns, data } = param
- const sums = []
- columns.forEach((column, index) => {
- if (index === 0) {
- sums[index] = '合计'
- } else if (index === 2 || index === 7) {
- const values = data.map(item => Number(item[column.property]))
- if (!values.every(value => isNaN(value))) {
- sums[index] = values.reduce((prev, curr) => {
- const value = Number(curr)
- if (!isNaN(value)) {
- return prev + curr
- } else {
- return prev
- }
- }, 0)
- } else {
- sums[index] = ''
- }
- } else {
- sums[index] = ''
- }
- })
- return sums
- },
- // table合并列
- objectSpanMethod({ row, column, rowIndex, columnIndex }) {
- let data = this.tableData; //拿到当前table中数据
- let cellValue = row[column.property]; //当前位置的值
- let noSortArr = ['num1', 'num2', 'num3', 'num4', 'num5', 'num6']; //不需要合并的字段(不进行合并行的prop)
- if (cellValue && !noSortArr.includes(column.property)) {
- let prevRow = data[rowIndex - 1]; //获取到上一条数据
- let nextRow = data[rowIndex + 1]; //下一条数据
- if (prevRow && prevRow[column.property] === cellValue) { //当有上一条数据,并且和当前值相等时
- return { rowspan: 0, colspan: 0 };
- } else {
- let countRowspan = 1;
- while (nextRow && nextRow[column.property] === cellValue) { //当有下一条数据并且和当前值相等时,获取新的下一条
- nextRow = data[++countRowspan + rowIndex];
- }
- if (countRowspan > 1) {
- return { rowspan: countRowspan, colspan: 1 };
- }
- }
- }
- },
- }
- }
- </script>
- <style scoped lang="less">
- </style>
vue2的 element 表格单元格合并
luckycoke2023-07-22 16:02
相关推荐
Martin -Tang1 小时前
Vue 3 中,ref 和 reactive的区别FakeOccupational2 小时前
nodejs 020: React语法规则 props和state放逐者-保持本心,方可放逐2 小时前
react 组件应用曹天骄4 小时前
next中服务端组件共享接口数据郝晨妤5 小时前
鸿蒙ArkTS和TS有什么区别?AvatarGiser6 小时前
《ElementPlus 与 ElementUI 差异集合》Icon 图标 More 差异说明喝旺仔la6 小时前
vue的样式知识点别忘了微笑_cuicui6 小时前
elementUI中2个日期组件实现开始时间、结束时间(禁用日期面板、控制开始时间不能超过结束时间的时分秒)实现方案尝尝你的优乐美6 小时前
vue3.0中h函数的简单使用windy1a6 小时前
【C语言】js写一个冒泡顺序