elementUI el-table内容自动滚动方案(二)vue-seamless-scroll

31 篇文章 0 订阅
10 篇文章 0 订阅

table父组件

<template>
    <div class="scroll_table">
      <div style="display: inline-block; width: 100%" v-if="wrapScroll">
        <el-table :data="data" class="table">
          <el-table-column
            v-for="(item,index) in columns"
            :key="index+'i'"
            :label="item.label"
            :prop="item.prop"
          ></el-table-column>
        </el-table>
        <baseScroll

          :listData="data"
          :limitMoveNum="data.length"
          class="seamless-warp"
        >
          <el-table
            v-if="showt"
            v-bind="$attr"
            :data="data"
            :border="border"
            :stripe="stripe"
            height="100%"
            class="cus-table"
            style="width: 100%">
            <el-table-column v-if="colIndex" label="序号" type="index" width="66" align="center" ></el-table-column>
            <child :headAlign="headAlign" :subHeadAlign="subHeadAlign"  v-for="(item,index) in columns" :key="index" :col="item">
              <template #cusSlotScope="{row,cols}">
                <slot :data="row" :name="cols.slotName"></slot>
              </template>
            </child>
            <slot></slot>
          </el-table>
        </baseScroll>

      </div>
      <el-table
        v-else
        v-bind="$attr"
        :data="data"
        :border="border"
        :stripe="stripe"
        height="100%"
        class="cus-table"
        style="width: 100%">
        <el-table-column v-if="colIndex" label="序号" type="index" width="66" align="center" ></el-table-column>
        <child :headAlign="headAlign" :subHeadAlign="subHeadAlign"  v-for="(item,index) in columns" :key="index" :col="item">
          <template #cusSlotScope="{row,cols}">
            <slot :data="row" :name="cols.slotName"></slot>
          </template>
        </child>
        <slot></slot>
      </el-table>
    </div>

</template>

<script>
import child from "@/component/elTableCom/childTable.vue";
import baseScroll from "@/component/BaseScroll.vue";
import { ref } from "vue";
const tableNode = ref(null);
export default {
  name: "elTableCom",
  props: {
    scrollNum: {
      type: [Number],
      default: 3,
    },
    tableTrHeight: {
      type: [Number],
      default: 39,
    },
    wrapScroll: {
      type: [Boolean],
      default: false,
    },
    tableData: {
      type: [Array],
      default: ()=>{
        return [];
      },
    },
    colIndex: {
      type: [Boolean],
      default: false,
    },
    columns: {
      type: [Array],
      default: ()=>{
        return [];
      },
    },
    border: {
      type: [String, Boolean],
      default: false,
    },
    stripe: {
      type: [String, Boolean],
      default: false,
    },
    headAlign: {
      type: [String],
      default: "left",
    },
    subHeadAlign: {
      type: [String],
      default: "left",
    },
  },
  watch: {
    tableData: {
      handler: function (val) {
        this.data = val;
        this.initScroll();
      },
      immediate: true,
      deep: true,
    },
  },
  data () {
    return {
      data: [],
      showt: false,
    };
  },
  components: {child, baseScroll},
  mounted () {
  },
  beforeUnmount () {
  },
  methods: {
    initScroll () {
      if (!this.wrapScroll) {
        return;
      }
      let scrollHeight = this.tableTrHeight * this.data.length;
      this.showt = true;
      this.$nextTick(()=>{
        tableNode.value = document.querySelector(".cus-table").querySelector(".el-scrollbar__wrap");
        tableNode.value.style.height = `${scrollHeight}px`;
        // console.log(tableNode.value.querySelectorAll("tr")[0].offsetHeight, this.tableTrHeight, this.data.length, tableNode.value.style.height);
      });
    },
  },
};
</script>

<style scoped lang="scss">
  .scroll_table {
    flex:1;
    :deep(.table .el-table__body-wrapper) {
      display: none;
    }
    // 重点注意这段样式
    .seamless-warp {
      height: 100%;
      display:flex;
      flex-direction:column;
      :deep( .cus-table) {
        .el-table__header-wrapper {
          display: none;
        }
      }
    }
  }
</style>

子组件见方案一

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue中实现带有固定表头和滚动效果的滚动表格,你可以按照以下步骤进行操作: 1. 首先,创建两个`el-table`组件。第一个`el-table`用于显示固定的表头,而第个`el-table`则放置在一个滚动组件内,并用于显示滚动的表体。 [1] 2. 隐藏第一个`el-table`的表体部分,以使其只显示表头。这可以通过设置`show-header`属性为`false`来实现。 [1] 3. 隐藏第个`el-table`的表头部分,同时将其放置在一个具有固定高度的滚动组件中(例如使用`vue-seamless-scroll`插件)。这样,整个表格都会有滚动效果。 [1 [2] 4. 使用`vue-seamless-scroll`插件,你需要在项目中安装该插件。通过运行命令`npm install vue-seamless-scroll --save`来安装插件。 [2] 5. 在`main.js`文件中,引入并注册`vue-seamless-scroll`插件。 [2] 综上所述,你可以按照以上步骤在Vue中实现带有固定表头和滚动效果的滚动表格。如果需要更详细的代码示例和使用说明,你可以参考`vue-seamless-scroll`的GitHub开源地址。 [2 [3<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue3 el-table结合seamless-scroll实现表格数据滚动](https://blog.csdn.net/wkh___/article/details/125638796)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [【el-table固定表格头循环滚动vue结合vue-seamless-scroll操作表格自动滚动效果](https://blog.csdn.net/weixin_50790427/article/details/121698352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值