基于Vue3+TS封装的Table表格

<template>
  <div class="system-table-box">
    <el-table
      v-bind="$attrs"
      ref="table"
      class="system-table"
      border
      height="100%"
      :data="data"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        align="center"
        width="50"
        v-if="showSelection"
        :reserve-selection="pagingMemory"
      />
      <el-table-column label="序号" width="60" align="center" v-if="showIndex">
        <template #default="scope">
          {{ (page.index - 1) * page.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <slot></slot>
    </el-table>
    <el-pagination
      v-if="showPage"
      v-model:current-page="page.index"
      class="system-page"
      background
      :layout="pageLayout"
      :total="page.total"
      :page-size="page.size"
      :page-sizes="pageSizes"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, onActivated, onMounted } from 'vue'
export default defineComponent({
  props: {
    data: { type: Array, default: () => [] }, // 数据源
    select: { type: Array, default: () => [] }, // 已选择的数据,与selection结合使用
    showIndex: { type: Boolean, default: false }, // 是否展示index选择,默认否
    showSelection: { type: Boolean, default: false }, // 是否展示选择框,默认否
    pagingMemory: { type: Boolean, default: false }, // 分页记忆
    showPage: { type: Boolean, default: true }, // 是否展示页级组件,默认是
    page: { // 分页参数
      type: Object,
      default: () => {
        return { index: 1, size: 20, total: 0 }
      }
    },
    pageLayout: { type: String, default: "total, sizes, prev, pager, next, jumper" }, // 分页需要显示的东西,默认全部
    pageSizes: { type: Array, default: [10, 20, 50, 100] }
  },
  setup(props, context) {
    const table: any = ref(null)
    let timer: any = null
    // 分页相关:监听页码切换事件
    const handleCurrentChange = (val: Number) => {
      if (timer) {
        props.page.index = 1
      } else {
        props.page.index = val
        context.emit("getTableData")
      }
    }
    // 分页相关:监听单页显示数量切换事件
    const handleSizeChange = (val: Number) => {
      timer = 'work'
      setTimeout(() => {
        timer = null
      }, 100)
      props.page.size = val
      context.emit("getTableData", true)
    }
    // 选择监听器
    const handleSelectionChange = (val: []) =>{
      context.emit("selection-change", val)
    }
    // 解决BUG:keep-alive组件使用时,表格浮层高度不对的问题
    onActivated(() => {
      table.value.doLayout()
    })
    return {
      table,
      handleCurrentChange,
      handleSizeChange,
      handleSelectionChange
    }
  }
})
</script>

<style lang="scss" scoped>
  .system-table-box {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    height: 100%;
    .system-table {
      flex: 1;
      height: 100%;
    }
    
    .system-page {
      margin-top: 20px;
    }
  }
</style>

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个基于Bootstrap的自定义封装弹出框表格代码,可以用于展示数据或者进行数据选择等操作: HTML代码: ```html <!-- 弹出框模态框 --> <div class="modal fade" id="tableModal" tabindex="-1" role="dialog" aria-labelledby="tableModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="tableModalLabel">选择数据</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <table id="dataTable" class="table table-striped table-bordered"> <thead> <tr> <th>列1</th> <th>列2</th> <th>列3</th> </tr> </thead> <tbody> <!-- 数据行会通过JS动态生成 --> </tbody> </table> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button> <button type="button" class="btn btn-primary" id="selectBtn">选择</button> </div> </div> </div> </div> ``` JS代码: ```javascript // 默认选中的数据行索引 var selectedRowIndex = -1; // 打开弹出框表格 function openTableModal(data, selectedIndex) { // 清空表格 $("#dataTable tbody").empty(); // 生成表格数据行 for (var i = 0; i < data.length; i++) { var row = $("<tr></tr>"); for (var j = 0; j < data[i].length; j++) { var cell = $("<td></td>").text(data[i][j]); row.append(cell); } $("#dataTable tbody").append(row); } // 设置默认选中行,并高亮显示 if (selectedIndex >= 0 && selectedIndex < data.length) { selectedRowIndex = selectedIndex; $("#dataTable tbody tr").eq(selectedRowIndex).addClass("table-primary"); } // 显示弹出框 $("#tableModal").modal("show"); } // 选择数据行 function selectTableRow(rowIndex) { // 取消之前选中的行的高亮显示 $("#dataTable tbody tr").eq(selectedRowIndex).removeClass("table-primary"); // 设置当前选中的行,并高亮显示 selectedRowIndex = rowIndex; $("#dataTable tbody tr").eq(selectedRowIndex).addClass("table-primary"); } // 获取选中的数据行 function getSelectedTableRow() { var rowData = []; if (selectedRowIndex >= 0) { var row = $("#dataTable tbody tr").eq(selectedRowIndex); row.find("td").each(function() { rowData.push($(this).text()); }); } return rowData; } // 绑定选择按钮点击事件 $("#selectBtn").click(function() { var rowData = getSelectedTableRow(); if (rowData.length > 0) { // TODO: 处理选中数据行的逻辑 console.log(rowData); } // 关闭弹出框 $("#tableModal").modal("hide"); }); ``` 使用示例: ```javascript // 打开弹出框表格,data为二维数组形式的表格数据,selectedIndex为默认选中的行索引 openTableModal([ ["数据1-1", "数据1-2", "数据1-3"], ["数据2-1", "数据2-2", "数据2-3"], ["数据3-1", "数据3-2", "数据3-3"], ], 1); ``` 注意,以上代码仅供参考,具体实现应根据自己的需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web_Design_Duke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值