基于uniapp+vue3自定义增强版table表格组件「兼容H5+小程序+App端」

 更新时间:2024年05月18日 15:55:56   作者:xiaoyan2017  
uv3-table:一款基于uniapp+vue3跨端自定义手机端增强版表格组件,支持固定表头/列、边框、斑马纹、单选/多选,自定义表头/表体插槽、左右固定列阴影高亮显示,支持编译兼容H5+小程序端+App端,H5+小程序+App端,多端运行一致

uv3-table:一款基于uniapp+vue3跨端自定义手机端增强版表格组件。支持固定表头/列、边框、斑马纹、单选/多选,自定义表头/表体插槽、左右固定列阴影高亮显示。支持编译兼容H5+小程序端+App端

如下图:H5+小程序+App端,多端运行一致。

uv3-table表格插件是最新原创项目uniapp-os后台管理系统的一个独立版组件。

由于在开发uni-os手机后台系统需要用到table表格组件。无奈uniapp官方及插件市场table表格组件无论功能及UI上都不满足要求,于是自己爆肝一个多日夜开发了一款全新uniapp+vue3综合表格组件。

目前该项目已经出阶段性成果接近尾声了,相信很快就能和大家见面,到时也会做一些技术分享,敬请期待!

uv3-table使用示例

将uv3-table组件放到uniapp项目components目录下,无需在页面再次引入,即可使用。

基础用法

<uv3-table :columns="columns" :dataSource="data.list" />

自定义条纹样式

<uv3-table
    :columns="columns"
    :dataSource="data.list"
    stripe
    stripeColor="#eee"
    padding="5px"
    height="450rpx"
/>

综合用法(固定表头/列、自定义插槽内容)

<script setup>
    import { ref } from 'vue'
    import Mock from 'mockjs'

    const columns = ref([
        {type: 'selection', align: 'center', width: 80, fixed: true}, // 多选
        {type: 'index', label: 'ID', align: 'center', width: 80, fixed: true}, // 索引序号
        {prop: 'author', label: '作者', align: 'center', width: 120},
        {prop: 'title', label: '标题', align: 'left', width: 350},
        {prop: 'image', label: '图片', align: 'center', width: 120},
        {prop: 'switch', label: '推荐', align: 'center', width: 100},
        {prop: 'tags', label: '标签', align: 'center', width: 100},
        {prop: 'rate', label: '评分', align: 'center', width: 200},
        {prop: 'date', label: '发布时间', align: 'left', width: 250}, // 时间
        {prop: 'action', label: '操作', align: 'center', width: 150, fixed: 'right'}, // 操作
    ])
    const data = ref(Mock.mock({
        total: 100,
        page: 1,
        pagesize: 10,
        'list|20': [
            {
                id: '@id()',
                author: '@cname()',
                title: '@ctitle(10, 20)',
                image: `//api.yimian.xyz/img?id=@integer(100, 300)`,
                switch: '@boolean()',
                'tags|1': ['admin', 'test', 'dev'],
                rate: '@integer(1, 5)',
                date: '@datetime()',
                color: '@color()',
            }
        ]
    }))
</script>
<uv3-table
    :dataSource="data.list"
    :columns="columns"
    :headerBold="true"
    headerBackground="#ecf5ff"
    stripe
    border
    padding="5px"
    maxHeight="650rpx"
    @rowClick="handleRowClick"
    @select="handleSelect"
>
    <!-- 自定义header插槽内容 -->
    <template #headerCell="{ key, col, index }">
        <template v-if="key == 'title'">
            <view >{{col.label}} <input placeholder="搜索" size="small" /></view>
        </template>
        <template v-else-if="key == 'date'">
            <uni-icons type="calendar"></uni-icons> {{col.label}}
        </template>
        <template v-else>{{col.label}}</template>
    </template>
    
    <!-- 自定义body插槽内容(由于小程序不支持动态:name插槽,通过key标识来自定义表格内容) -->
    <template #default="{ key, value, row, col, index }">
        <template v-if="key == 'image'">
            <uv-image :src="value" lazyLoad observeLazyLoad @click="previewImage(value)" />
        </template>
        <template v-else-if="key == 'switch'">
            <switch :checked="value"  />
        </template>
        <template v-else-if="key == 'tags'">
            <uv-tags :text="value" :color="row.color" :borderColor="row.color" plain size="mini" />
        </template>
        <template v-else-if="key == 'rate'">
            <uni-rate :value="value" size="14" readonly />
        </template>
        <template v-else-if="key == 'action'">
            <uni-icons type="compose" color="#00aa7f" @click="handleEdit(row)" />
            <uni-icons type="trash" color="#ff007f"  @click="handleDel(row)" />
        </template>
        <template v-else>{{value}}</template>
    </template>
</uv3-table>

rowClick点击表格行,会返回该行数据。

select单选/多选,会返回表格选中数据。

uv3Table编码实现

uv3-table表格参数配置

const props = defineProps({
    // 表格数据
    dataSource: {
        type: Array,
        default() {
            return []
        }
    },
    /**
     * @params {string} background 对应列背景色
     * @params {string} type 对应列类型(多选selection 索引index)
     * @params {string} label 显示的列标题
     * @params {string} prop 对应的列字段名
     * @params {string} align 列水平对齐方式(left center right)
     * @params {number|string} width 对应列宽度
     * @params {boolean|string} fixed 该列固定到左侧(fixed:true|'left')或右侧(fixed:'right')
     * @params {string} columnStyle 对应列自定义样式
     * @params {string} className/class 表格列的类名className
     */
    columns: {
        type: Array,
        default() {
            return []
        }
    },
    // 表格宽度
    width: { type: [Number, String] },
    // 表格高度
    height: { type: [Number, String] },
    // 表格最大高度
    maxHeight: { type: [Number, String] },
    // 是否为斑马纹
    stripe: { type: [Boolean, String] },
    // 斑马纹背景
    stripeColor: { type: String, default: '#fafafa' },
    // 是否带有边框
    border: { type: [Boolean, String] },
    // 列宽度(推荐默认rpx)
    columnWidth: { type: [Number, String], default: 200 },
    // 单元格间距
    padding: { type: String, default: '5rpx 10rpx' },
    // 是否显示表头
    showHeader: { type: [Boolean, String], default: true },
    // 表头背景色
    headerBackground: { type: String, default: '#ebeef5' },
    // 表头颜色
    headerColor: { type: String, default: '#333' },
    // 表头字体加粗
    headerBold: { type: [Boolean, String], default: true },
    // 表格背景色
    background: { type: String, default: '#fff' },
    // 表格颜色
    color: { type: String, default: '#606266' },
    // 空数据时显示的文本内容,也可以通过 #empty 设置
    emptyText: { type: String, default: '暂无数据' }
})

模板结构如下

<template>
    <view
        
        ...
    >
        <!-- 表头 -->
        <view v-if="showHeader"  :>
            <view
                v-for="(col, cindex) in columns"
                :key="cindex"
                
                :
                ...
                @click="handleHeaderClick(col)"
            >
                ...
            </view>
        </view>
        <!-- 表体 -->
        <view >
            ...
        </view>
    </view>
</template>

Props参数

columns参数

background 对应列背景色 type 对应列类型(多选selection 索引index) label 显示的列标题 prop 对应的列字段名 align 列水平对齐方式(left center right) width 对应列宽度 fixed 该列固定到左侧(fixed:true|‘left’) 或 右侧(fixed:‘right’) columnStyle 对应列自定义样式 className/class 表格列的类名className

事件

@headerClick 点击表头 @rowClick 点击行触发 @select 多选/单选

自定义插槽

headerCell 自定义表头内容 default 默认表体内容 empty 无数据插槽

希望以上分享对大家有些帮助,开发不易,一起fighting~~💝

到此这篇关于基于uniapp+vue3自定义增强版table表格组件「兼容H5+小程序+App端」的文章就介绍到这了,更多相关uniapp+vue3自定义增强版table表格组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 如何为vuex实现带参数的 getter和state.commit

    如何为vuex实现带参数的 getter和state.commit

    这篇文章主要介绍了如何为vuex实现带参数的getter和state.commit,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • vue移动端项目缓存问题实践记录

    vue移动端项目缓存问题实践记录

    最近在做一个vue移动端项目,被缓存问题搞得头都大了,积累了一些经验,特此记录总结下,分享到脚本之家平台,对vue移动端项目缓存问题实践记录感兴趣的朋友跟随小编一起看看吧
    2018-10-10
  • Vue 配置代理详情

    Vue 配置代理详情

    这篇文章主要介绍了Vue 配置代理详情,文章围绕主题的相关资料展开详细内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-04-04
  • 在antd4.0中Form使用initialValue操作

    在antd4.0中Form使用initialValue操作

    这篇文章主要介绍了在antd4.0中Form使用initialValue操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • unplugin-auto-import与unplugin-vue-components安装问题解析

    unplugin-auto-import与unplugin-vue-components安装问题解析

    这篇文章主要为大家介绍了unplugin-auto-import与unplugin-vue-components问题解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • vue中watch监听不到变化的解决

    vue中watch监听不到变化的解决

    本文主要介绍了vue中watch监听不到变化的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • 详解vuex中action何时完成以及如何正确调用dispatch的思考

    详解vuex中action何时完成以及如何正确调用dispatch的思考

    这篇文章主要介绍了详解vuex中action何时完成以及如何正确调用dispatch的思考,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Vue之前端体系与前后端分离详解

    Vue之前端体系与前后端分离详解

    本篇文章主要介绍了Vue之前端体系与前后端分离,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-10-10
  • Vue SPA 如何解决浏览器缓存问题

    Vue SPA 如何解决浏览器缓存问题

    这篇文章主要介绍了Vue SPA 如何解决浏览器缓存问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 完美解决axios在ie下的兼容性问题

    完美解决axios在ie下的兼容性问题

    下面小编就为大家分享一篇完美解决axios在ie下的兼容性问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03

最新评论