基于uniapp+vue3自定義增強版table表格組件「兼容H5+小程序+App端」
uv3-table:一款基于uniapp+vue3跨端自定義手機端增強版表格組件。支持固定表頭/列、邊框、斑馬紋、單選/多選,自定義表頭/表體插槽、左右固定列陰影高亮顯示。支持編譯兼容H5+小程序端+App端。
如下圖:H5+小程序+App端,多端運行一致。

uv3-table表格插件是最新原創(chuàng)項目uniapp-os后臺管理系統(tǒng)的一個獨立版組件。
由于在開發(fā)uni-os手機后臺系統(tǒng)需要用到table表格組件。無奈uniapp官方及插件市場table表格組件無論功能及UI上都不滿足要求,于是自己爆肝一個多日夜開發(fā)了一款全新uniapp+vue3綜合表格組件。

目前該項目已經(jīng)出階段性成果接近尾聲了,相信很快就能和大家見面,到時也會做一些技術(shù)分享,敬請期待!

uv3-table使用示例
將uv3-table組件放到uniapp項目components目錄下,無需在頁面再次引入,即可使用。

基礎(chǔ)用法
<uv3-table :columns="columns" :dataSource="data.list" />
自定義條紋樣式
<uv3-table
:columns="columns"
:dataSource="data.list"
stripe
stripeColor="#eee"
padding="5px"
height="450rpx"
/>綜合用法(固定表頭/列、自定義插槽內(nèi)容)
<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: '標(biāo)題', align: 'left', width: 350},
{prop: 'image', label: '圖片', align: 'center', width: 120},
{prop: 'switch', label: '推薦', align: 'center', width: 100},
{prop: 'tags', label: '標(biāo)簽', align: 'center', width: 100},
{prop: 'rate', label: '評分', align: 'center', width: 200},
{prop: 'date', label: '發(fā)布時間', 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插槽內(nèi)容 -->
<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插槽內(nèi)容(由于小程序不支持動態(tài):name插槽,通過key標(biāo)識來自定義表格內(nèi)容) -->
<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點擊表格行,會返回該行數(shù)據(jù)。

select單選/多選,會返回表格選中數(shù)據(jù)。

uv3Table編碼實現(xiàn)
uv3-table表格參數(shù)配置
const props = defineProps({
// 表格數(shù)據(jù)
dataSource: {
type: Array,
default() {
return []
}
},
/**
* @params {string} background 對應(yīng)列背景色
* @params {string} type 對應(yīng)列類型(多選selection 索引index)
* @params {string} label 顯示的列標(biāo)題
* @params {string} prop 對應(yīng)的列字段名
* @params {string} align 列水平對齊方式(left center right)
* @params {number|string} width 對應(yīng)列寬度
* @params {boolean|string} fixed 該列固定到左側(cè)(fixed:true|'left')或右側(cè)(fixed:'right')
* @params {string} columnStyle 對應(yīng)列自定義樣式
* @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] },
// 列寬度(推薦默認(rèn)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' },
// 空數(shù)據(jù)時顯示的文本內(nèi)容,也可以通過 #empty 設(shè)置
emptyText: { type: String, default: '暫無數(shù)據(jù)' }
})模板結(jié)構(gòu)如下
<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參數(shù)

columns參數(shù)
background 對應(yīng)列背景色 type 對應(yīng)列類型(多選selection 索引index) label 顯示的列標(biāo)題 prop 對應(yīng)的列字段名 align 列水平對齊方式(left center right) width 對應(yīng)列寬度 fixed 該列固定到左側(cè)(fixed:true|‘left’) 或 右側(cè)(fixed:‘right’) columnStyle 對應(yīng)列自定義樣式 className/class 表格列的類名className
事件
@headerClick 點擊表頭 @rowClick 點擊行觸發(fā) @select 多選/單選
自定義插槽
headerCell 自定義表頭內(nèi)容 default 默認(rèn)表體內(nèi)容 empty 無數(shù)據(jù)插槽
希望以上分享對大家有些幫助,開發(fā)不易,一起fighting~~??
到此這篇關(guān)于基于uniapp+vue3自定義增強版table表格組件「兼容H5+小程序+App端」的文章就介紹到這了,更多相關(guān)uniapp+vue3自定義增強版table表格組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE DEMO之模擬登錄個人中心頁面之間數(shù)據(jù)傳值實例
今天小編就為大家分享一篇VUE DEMO之模擬登錄個人中心頁面之間數(shù)據(jù)傳值實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue中使用localStorage存儲token并設(shè)置時效
這篇文章主要為大家介紹了Vue中使用localStorage存儲token并設(shè)置時效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
詳解Vue如何進(jìn)行表單聯(lián)動與級聯(lián)選擇
表單聯(lián)動和級聯(lián)選擇是Vue.js中常見的功能,在下面的文章中,我們將討論如何在Vue.js中實現(xiàn)表單聯(lián)動和級聯(lián)選擇,感興趣的小伙伴可以了解一下2023-06-06
如何正確解決VuePress本地訪問出現(xiàn)資源報錯404的問題
這篇文章主要介紹了如何正確解決VuePress本地訪問出現(xiàn)資源報錯404的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

