滚动表格封装

滚动表格封装
  1. 我们先设定接收的参数
    需要表头内容columns,表格数据data,需要currentSlides来控制当前页展示几行
const props = defineProps({
    // 表头内容
    columns: {
        type: Array,
        default: () => [],
        required: true,
    },
    // 表格数据
    data: {
        type: Array,
        default: () => [],
    },
    // 当前一页显示几行,超过几行开始滚动
    currentSlides: {
        type: Number,
        default: 5
    }
});
  1. 写好表格基本样式(这个不通用,可根据需求自行调整)
  2. 表格通常会有操作列,那么我们通过slot来实现,传递当前行的数据item,提供给点击事件
<slot v-if="props.columns[index].slot" :name="props.columns[index].name" :item="item"  />
  1. 封转滚动文字组件,文字超出则滚动显示
    详情可参考另一篇文章戳这里
<HorseRaceLamp
	:width="props.columns[index].width"
	font="14px AppleSystemUIFont"
	:text="item[val]"
	:speed="((item[val] && item[val].length) ? item[val].length : 1) * 0.5"
/>

表格完整代码如下:

<template>
    <div class="table">
        <div class="row thead">
            <div v-for="item in props.columns" :key="item.name" :style="{ width: `${item.width}px` }">{{ item.label }}</div>
        </div>
        <div class="tbody">
            <Swiper :modules="modules" :autoplay="swiperOption.autoplay" direction="vertical" :slides-per-view="currentSlides" :space-between="11">
                <SwiperSlide v-for="(item, ind) in props.data" :key="ind" class="row">
                    <template v-for="(val, index) in columnKeys">
                        <slot v-if="props.columns[index].slot" :name="props.columns[index].name" :item="item"  />
                        <div :title="item[val]" v-else :key="`${index}${val}`" :style="{ width: `${props.columns[index].width}px` }">
                            <HorseRaceLamp
                                :width="props.columns[index].width"
                                font="14px AppleSystemUIFont"
                                :text="item[val]"
                                :speed="((item[val] && item[val].length) ? item[val].length : 1) * 0.5"
                            />
                        </div>
                    </template>
                </SwiperSlide>
            </Swiper>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { Swiper, SwiperSlide } from "swiper/vue";
import { Autoplay } from "swiper";
import "swiper/css";
import HorseRaceLamp from '@/components/HorseRaceLamp.vue';

const modules = [Autoplay];
const swiperOption = reactive({
    autoplay: {
        delay: 3000,
        disableOnInteraction: false,
        pauseOnMouseEnter: true,
    },
});

const props = defineProps({
    // 表头内容
    columns: {
        type: Array,
        default: () => [],
        required: true,
    },
    // 表格数据
    data: {
        type: Array,
        default: () => [],
    },
    // 当前一页显示几行,超过几行开始滚动
    currentSlides: {
        type: Number,
        default: 5
    }
});
const columnKeys = ref([]);

columnKeys.value = props.columns.map((item) => item.name);
</script>

<style lang="scss" scoped>
.table {
    width: 100%;
    height: 100%;
    color: #fff;
    font-size: 14PX;
    font-family: AppleSystemUIFont;

    .thead {
        background-color: rgb(21 77 160 / 20%);

        &.row {
            color: rgba(212, 237, 253, 1);
            font-size: 12PX;
        }
    }

    .row {
        display: flex;
        align-items: center;
        height: 28PX;
        padding: 0 30PX;

        > div {
            overflow: hidden;
            width: 100%;
            margin-right: 10PX;
            text-align: center;
            text-overflow: ellipsis;
            white-space: nowrap;

            &:nth-last-of-type(1) {
                margin-right: 0;
            }
        }
    }

    .swiper {
        width: 100%;
        height: 100%;
    }

    .tbody {
        box-sizing: border-box;
        width: 100%;
        height: calc(100% - 28PX);
        padding-top: 11PX;

        .row {
            position: relative;
            display: flex;
            align-items: center;
            cursor: pointer;
        }
    }
}
</style>

使用方式:

  • 需要给Table组件包裹一个父元素,自定义设置其宽高
<div class="content">
    <Table :columns="columns" :data="tableData" :currentSlides="6">
        <template #handler="{ item }">
            <div class="btn" @click="close(item)">关闭</div>
        </template>
    </Table>
 </div>
// 需要传递的参数
const tableData = ref([])
const columns = [
    { name: 'redRank', label: '红榜', width: 148 },
    { name: 'blackRank', label: '黑榜', width: 148 },
    { name: 'handler', label: '操作', slot: true },
];
// 自定义slot插槽操作事件
const close = (item) => {
    console.log(item)
}
  • 设置其宽高
.content {
	width: 446px;
    height: 250px;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值