uni-table 改造源码 添加固定表头功能 附件修改后源码

通过将uni-table源码复制并封装为自定义组件,实现了固定表头的功能。详细改造步骤包括uni-table、uni-tbody、uni-table-td、uni-table-th、uni-table-thead和uni-table-tr的修改。源码修改后的效果可以在github项目中查看,提供uni-ui 1.4.12版本的uni_table源码,提取码为6666。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

github地址:https://github.com/1205093639/uni-table-Fixed_header
实现效果:

1.先找到源码 复制出来单独封装成自己的组件

组件源码位置

2.修改

2.1 uni-table

无需改动

2.2 uni-tbody

在这里插入图片描述

<template>
    <!-- #ifdef H5 -->
    <tbody class="uni-tbody" :style="{'max-height':height}">
        <slot></slot>
    </tbody>
    <!-- #endif -->
    <!-- #ifndef H5 -->
    <view class="uni-tbody" :style="{'max-height':height}">
        <slot></slot>
    </view>
    <!-- #endif -->
</template>

<script>
export default {
   
   
    name: 'uniBody',
    props: ['height'],
    options: {
   
   
        virtualHost: true
    },
    data() {
   
   
        return {
   
   

        }
    },
    created() {
   
    },
    methods: {
   
   }
}
</script>

<style>
.uni-tbody {
   
   
    display: block;
    width: 100%;
    overflow: auto;
}
</style>

2.3 uni-table-td

<template>
    <!-- #ifdef H5 -->
    <td class="uni-table-td" :rowspan="rowspan" :colspan="colspan" :class="{'table--border':border}" :style="{'max-width':width + 'px','text-align':align}">
        <slot></slot>
    </td>
    <!-- #endif -->
    <!-- #ifndef H5 -->
    <!-- :class="{'table--border':border}"  -->
    <view class="uni-table-td" :class="{'table--border':border}" :style="{'max-width':width + 'px','text-align':align}">
        <slot></slot>
    </view>
    <!-- #endif -->
</template>

<script>
/**
 * Td 单元格
 * @description 表格中的标准单元格组件
 * @tutorial https://ext.dcloud.net.cn/plugin?id=3270
 * @property {Number} 	align = [left|center|right]	单元格对齐方式
 */
export default {
   
   
    name: 'uniTd',
    options: {
   
   
        virtualHost: true
    },
    props: {
   
   
        width: {
   
   
            type: [String, Number],
            default: ''
        },
        align: {
   
   
            type: String,
            default: 'left'
        },
        rowspan: {
   
   
            type: [Number, String],
            default: 1
        },
        colspan: {
   
   
            type: [Number, String],
            default: 1
        }
    },
    data() {
   
   
        return {
   
   
            border: false
        };
    },
    created() {
   
   
        this.root = this.getTable()
        this.border = this.root.border
    },
    methods: {
   
   
        /**
         * 获取父元素实例
         */
        getTable() {
   
   
            let parent = this.$parent;
            let parentName = parent.$options.name;
            while (parentName !== 'uniTable') {
   
   
                parent = parent.$parent;
                if (!parent) return false;
                parentName = parent.$options.name;
            }
            return parent;
        },
    }
}
</script>

<style lang="scss">
$border-color: #ebeef5;

.uni-table-td {
   
   
    display: table-cell;
    padding: 8px 10px;
    font-size: 14px;
    border-bottom: 1px $border-color solid;
    font-weight: 400;
    color: #606266;
    line-height: 23px;
    box-sizing: border-box;
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    word-break: break-all;
    word-wrap: break-word;
}
.table--border {
   
   
    border-right: 1px $border-color solid;
}
</style>

2.4 uni-table-th

<template>
    <!-- #ifdef H5 -->
    <th :rowspan="rowspan" :colspan="colspan" class="uni-table-th" :class="{ 'table--border': border }" :style="{ 'max-width': width + 'px', 'text-align': align }">
        <view class="uni-table-th-row">
            <view class="uni-table-th-content" :style="{ 'justify-content': contentAlign }" @click="sort">
                <slot></slot>
                <view v-if="sortable" class="arrow-box">
                    <text class="arrow up" :class="{ active: ascending }" @click.stop="ascendingFn"></text>
                    
### UniApp 中 `uni-table` 组件实现表头固定的原理 在 UniApp 的开发环境中,`uni-table` 是一个常用的表格展示组件。为了实现表头固定功能,可以通过 CSS 属性 `position: sticky;` 来完成这一需求。该属性允许元素在其父容器内的滚动过程中保持粘性定位[^1]。 具体来说,在设置 `position: sticky;` 时,需要指定一个偏移量(如 `top: 40px;`),当页面或容器发生滚动操作时,一旦子元素到达设定的位置,则会触发类似于 `fixed` 定位的行为,从而让表头停留在可视区域内。 以下是基于上述理论的具体实现方式: #### HTML 结构 ```html <template> <view class="table-container"> <uni-table border stripe emptyText="暂无数据"> <!-- 表头 --> <uni-tr> <uni-th>列名1</uni-th> <uni-th>列名2</uni-th> <uni-th>列名3</uni-th> </uni-tr> <!-- 数据行 --> <uni-tr v-for="(item, index) in tableData" :key="index"> <uni-td>{{ item.col1 }}</uni-td> <uni-td>{{ item.col2 }}</uni-td> <uni-td>{{ item.col3 }}</uni-td> </uni-tr> </uni-table> </view> </template> ``` #### 样式定义 通过自定义样式来应用 `position: sticky;` 到表头部分: ```css <style scoped> .table-container { height: 300px; overflow-y: auto; } /* 设置表头的粘性定位 */ uni-tr:first-child uni-th { position: -webkit-sticky; /* 兼容 iOS */ position: sticky; top: 0; /* 距离顶部的距离 */ background-color: white; /* 防止背景透明导致视觉混乱 */ z-index: 999; /* 提升层级防止被其他内容覆盖 */ } </style> ``` 以上代码片段展示了如何利用 `position: sticky;` 将 `uni-table` 的表头固定在视图中的特定位置上。需要注意的是,对于某些平台(尤其是旧版浏览器或者移动端设备),可能还需要额外处理兼容性问题,比如增加 `-webkit-sticky` 前缀以支持更广泛的环境。 此外,如果项目中有大量类似的布局需求,建议封装成独立的公共组件以便于维护和重复使用。 --- 问题
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值