Ant Design Vue & Element-ui 中table 合并行功能,以及带分页并合并行

 1. Ant Design Vue 中 table 合并行功能

   <a-table
            ref="table"
            :dataSource="comList"
            :columns="columns"
            :scroll="{ y: 430 }"
            size="small"
            rowKey="id"
            :rowClassName="rowClassNameHandle"
            :customRow="
                record => {
                    return {
                        on: {
                            click: () => clickRow(record), // 点击行
                        },
                    }
                }
            "
            :pagination="ipagination"
            @change="handleTableChange"
  /> 
   

 columns: [
                {
                    title: '楼层',
                    dataIndex: 'floor',
                    align: 'center',
                    width: 130,
                    ellipsis: true,
                    customRender: (text, row, index) => {
                        const obj = {
                            children: text,
                            attrs: {},
                        }
                        obj.attrs.rowSpan = row.rowSpan
                        return obj
                    },
                },
                {
                    title: '名称',
                    dataIndex: 'name',
                    align: 'center',
                    ellipsis: true,
                },
            ],

      
     this.setRowSpan(this.comList, 'floor')

      //数据处理    data:表格数据,field: 要合并行的属性字段
     setRowSpan(data, field) {
            let count = 0 
            let indexCount = 1 
            while (indexCount < data.length) {
                const item = data[count]
                if (!item.rowSpan) {
                    item.rowSpan = 1
                }
                if (item[field] === data[indexCount][field]) {
                    item.rowSpan++
                    data[indexCount].rowSpan = 0
                } else {
                    count = indexCount
                }
        
                indexCount++
            }
        },

以上,如果表格不用分页,那么按照 floor 字段值一样就合并行的功能就已经实现了。

以下,如果表格是按照10条数据为一页来分页的合并行功能实现:

 // 进一步处理数据,按10条数据分页,来调整合并行的值
 this.comList.forEach((item, index) => {
                const rowSpan = item.rowSpan
                const remain = index % 10
                const pages = parseInt(item.rowSpan / 10)
                const currentPage = parseInt(index / 10)
                if (rowSpan > 10 - remain) {
                    item.rowSpan = 10 - remain //首
                      const rowSpanRemain =
                        pages  > 1 ? rowSpan - (pages - 1) * 10 - (10 - remain) 
                        : rowSpan - (10 - remain)
                    for (let i = 1; i < pages; i++) {
                        arr[(i + currentPage) * 10].rowSpan = 10
                    }
                    let end = (pages + currentPage) * 10
                    if(rowSpan - (10 - remain) > 0 && pages === 0){
                        end = (pages + currentPage + 1) * 10
                    }
                    arr[end].rowSpan = rowSpanRemain //尾
                }
            })

2. Element ui 中table 合并行功能

  <el-table
            :data="
                comList.slice(
                    (ipagination.pageNo - 1) * ipagination.pageSize,
                    ipagination.pageNo * ipagination.pageSize,
                )
            "
            style="width: 100%"
            max-height="450"
            :span-method="objectSpanMethod"
            stripe
            border
            size="mini"
        >
            <el-table-column prop="floor" label="楼栋" width="150"> </el-table-column>
            <el-table-column prop="name" label="wifi名称"> </el-table-column>
        </el-table>
        <el-pagination
            :current-page="ipagination.pageNo"
            :page-size="ipagination.pageSize"
            layout="total, prev, pager, next"
            :total="ipagination.total"
            @current-change="handleCurrentChange"
            size="mini"
        >
        </el-pagination>


  
   // arr 为表格数据源
   this.setRowSpan(arr, 'floor')

   // 如果有分页才需要此段代码
            arr.forEach((item, index) => {
                const rowSpan = item.rowSpan
                const remain = index % 10
                const pages = parseInt(item.rowSpan / 10)
                const currentPage = parseInt(index / 10)
                if (rowSpan > 10 - remain) {
                    item.rowSpan = 10 - remain //首
                    const rowSpanRemain =
                        pages  > 1 ? rowSpan - (pages - 1) * 10 - (10 - remain) 
                        : rowSpan - (10 - remain)
                    for (let i = 1; i < pages; i++) {
                        arr[(i + currentPage) * 10].rowSpan = 10
                    }
                    let end = (pages + currentPage) * 10
                    if(rowSpan - (10 - remain) > 0 && pages === 0){
                        end = (pages + currentPage + 1) * 10
                    }
                    arr[end].rowSpan = rowSpanRemain //尾
                }
            })

    this.comList = arr


  

  objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0) {
                console.log(row)
                if (row.rowSpan > 0) {
                    return { rowspan: row.rowSpan, colspan: 1 }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 0,
                    }
                }
            }
        },

### 寻找适用于Vue.js的数据表组件替代方案 对于寻找类似于 `vxe-table` 的功能插件,有多个优秀的数据表组件可供选择。以下是几个推荐的选择: #### 1. Element Plus Table Component Element Plus 是一个基于 Vue 3.x 的 UI 组件库,提供了丰富的交互组件,其中包括强大的表组件。 - **特点** - 支持分页、排序、筛选等功能- 提供多种列配置选项,如固定列、自定义表头等。 - 良好的文档支持和社区活跃度高[^6]。 ```javascript // 安装 Element Plus npm install element-plus ``` ```html &lt;!-- 使用示例 --&gt; &lt;template&gt; &lt;el-table :data=&quot;tableData&quot;&gt; &lt;el-table-column prop=&quot;date&quot; label=&quot;日期&quot;&gt;&lt;/el-table-column&gt; &lt;el-table-column prop=&quot;name&quot; label=&quot;姓名&quot;&gt;&lt;/el-table-column&gt; &lt;/el-table&gt; &lt;/template&gt; &lt;script setup&gt; import { ref } from &#39;vue&#39; const tableData = ref([ { date: &#39;2016-05-02&#39;, name: &#39;王小虎&#39; }, // 更多数据... ]) &lt;/script&gt; ``` #### 2. Ant Design of Vue (Antdv) Ant Design of Vue 同样是一个流Vue 数据表解决方案,继承了蚂蚁金服的设计体系。 - **特点** - 设计风现代且统一。 -具备高级特性,比如树形结构展示、可编辑单元等。 - 社区资源丰富,适合企业级应用开发[^7]. ```bash # 安装 ant-design-vue npm i ant-design-vue --save ``` ```html &lt;!-- 使用实例 --&gt; &lt;template&gt; &lt;a-table :columns=&quot;columns&quot; :dataSource=&quot;data&quot;/&gt; &lt;/template&gt; &lt;script&gt; export default { data() { return { columns: [ { title: &#39;Name&#39;, dataIndex: &#39;name&#39;, key: &#39;name&#39;, }, // 其他列配置... ], data: [ { key: &#39;1&#39;, name: &#39;John Brown&#39;, }, // 更多数据项... ] } } } &lt;/script&gt; ``` #### 3. Naive UI Data Table Naive UI 是另一个高质量的 Vue 组件集合,其表组件具有良好的性能表现以及灵活定制能力。 - **特点** - 性能优化良好,尤其针对大数据量场景做了特别处理。 - API设计简洁直观,易于集成到现有项目中。 - 文档详尽,官方维护积极[^8]. ```shell # 添加 naive-ui 到项目 pnpm add naive-ui ``` ```html &lt;!-- 示例代码片段 --&gt; &lt;n-data-table :columns=&quot;columns&quot; :data=&quot;data&quot; /&gt; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值