当前位置:首页 > uni-app

uniapp表格合并行

2026-03-05 10:14:03uni-app

uniapp表格合并行实现方法

在uniapp中实现表格合并行功能,可以通过以下方式实现:

使用<table>标签和rowspan属性 通过HTML原生表格的rowspan属性可以实现行合并。在uniapp的vue文件中可以直接使用HTML标签。

<template>
  <view>
    <table border="1" style="width:100%">
      <tr>
        <th>姓名</th>
        <th>科目</th>
        <th>成绩</th>
      </tr>
      <tr>
        <td rowspan="2">张三</td>
        <td>数学</td>
        <td>90</td>
      </tr>
      <tr>
        <td>语文</td>
        <td>85</td>
      </tr>
    </table>
  </view>
</template>

使用uniapp的<uni-table>组件 如果使用uniapp的官方表格组件,需要通过计算属性动态处理数据:

<template>
  <view>
    <uni-table>
      <uni-tr>
        <uni-th>姓名</uni-th>
        <uni-th>科目</uni-th>
        <uni-th>成绩</uni-th>
      </uni-tr>
      <uni-tr v-for="(item,index) in tableData" :key="index">
        <uni-td v-if="item.showName" :rowspan="item.rowspan">{{item.name}}</uni-td>
        <uni-td>{{item.subject}}</uni-td>
        <uni-td>{{item.score}}</uni-td>
      </uni-tr>
    </uni-table>
  </view>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', subject: '数学', score: 90, showName: true, rowspan: 2 },
        { name: '张三', subject: '语文', score: 85, showName: false, rowspan: 0 }
      ]
    }
  }
}
</script>

使用CSS实现伪合并效果 通过绝对定位和相对定位模拟合并效果:

<template>
  <view class="table-container">
    <view class="table-row" v-for="(row,index) in tableData" :key="index">
      <view class="table-cell" v-if="row.showName" :style="{height: row.rowspan*50+'px'}">
        {{row.name}}
      </view>
      <view class="table-cell">
        {{row.subject}}
      </view>
      <view class="table-cell">
        {{row.score}}
      </view>
    </view>
  </view>
</template>

<style>
.table-container {
  display: flex;
  flex-direction: column;
}
.table-row {
  display: flex;
  position: relative;
}
.table-cell {
  flex: 1;
  border: 1px solid #ddd;
  padding: 10px;
}
</style>

使用第三方表格组件 可以引入第三方表格组件如uViewu-table组件:

uniapp表格合并行

<template>
  <u-table>
    <u-tr>
      <u-th>姓名</u-th>
      <u-th>科目</u-th>
      <u-th>成绩</u-th>
    </u-tr>
    <u-tr v-for="(item,index) in tableData" :key="index">
      <u-td v-if="item.showName" :rowspan="item.rowspan">{{item.name}}</u-td>
      <u-td>{{item.subject}}</u-td>
      <u-td>{{item.score}}</u-td>
    </u-tr>
  </u-table>
</template>

在实际开发中,需要根据项目需求选择合适的实现方式。原生HTML表格简单直接,uniapp组件更符合uniapp生态,CSS方案灵活性高,第三方组件功能丰富但会增加包体积。

标签: 表格uniapp
分享给朋友:

相关文章

uniapp组件

uniapp组件

uniapp组件基础概念 uniapp的组件系统基于Vue.js,支持跨平台开发(H5、小程序、App等)。组件分为内置基础组件(如<view>、<button>)和自定义组件…

uniapp 录像

uniapp 录像

uniapp 录像功能实现 在uniapp中实现录像功能可以通过调用uni-app的API或结合第三方插件完成。以下是常见的实现方法: 使用uni.chooseVideo方法 uni.choose…

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

介绍uniapp

介绍uniapp

Uniapp 概述 Uniapp 是一款基于 Vue.js 的跨平台开发框架,允许开发者使用一套代码同时生成 iOS、Android、Web 以及小程序应用。其核心优势在于高效开发和代码复用,大幅降低…

uniapp 日志

uniapp 日志

uniapp 日志管理方法 uniapp本身没有内置日志系统,但可以通过以下方式实现日志记录功能: 使用console.log输出日志 开发阶段可以直接使用console.log()、console…

uniapp定位描述

uniapp定位描述

uniapp定位功能实现 uniapp提供了多种定位方式,包括H5、小程序和App端的定位功能。通过uni.getLocation方法可以获取设备当前位置信息。 基本定位方法 使用uni.getLo…