当前位置: 首页 > article >正文

页面小组件-表格封装(基础版)

说明

这一版封装的主要原因是当时有很多报表,数据列很多很多,基本在二十个左右。相应领导安排简洁代码,也为了偷懒,直接插槽循环搞起。其余是为了统一样式修改。

组件源码

<template>
  <el-table
    v-loading="loading"
    class="page-table"
    :data="tableData"
    :header-cell-style="headerCellStyle"
    :highlight-current-row="highlightCurrentRow"
    :height="propsHeight"
    row-key="id"
    ref="tableRef"
    :border='border'
    >
    <!-- 空状态显示 -->
    <template slot="empty">
      <div class="empty-table">
        <i class="empty-icon el-icon-folder-opened" />
        <span class="empty-text">暂无数据</span>
      </div>
    </template>
    <!-- 表格数据勾选、序号等 -->
    <slot />
    <el-table-column
      v-if="showIndex"
      label="序号"
      type="index"
      align="center"
      width="80"
      />
    <!-- 列数据渲染 -->
    <template v-for="column in userColumn">
      <el-table-column
        v-if="!column.hide"
        :key="column.prop"
        :label="column.label"
        :prop="column.prop"
        align="center"
        :sortable="column.sortable"
        :width="column.width"
        :min-width="column.minWidth"
        >
        <template slot-scope="{row}">
          <!-- 针对每列数据都设置作用域插槽,便于自定义显示内容 -->
          <slot
            :name="column.prop"
            :row="row"
            >{{ row[column.prop] }}</slot>
        </template>
      </el-table-column>
    </template>
    <!-- 操作列 -->
    <slot name="operation" />
  </el-table>
</template>
<script>
export default {
  name: 'CustomTable',
  props: {
    tableData: { type: Array, default: () => [] },
    headerCellStyle: { type: Object, default: () => { } },
    highlightCurrentRow: { type: Boolean, default: false },
    loading: { type: Boolean, default: false },
    column: { type: Array, default: () => [] },
    showIndex: { type: Boolean, default: false },
    propsHeight: '',
    border: { type: Boolean, default: false }
  },
  data () {
    return {
      userColumn: []
    }
  },
  mounted () {
    if (this.column && this.column.length > 0) {
      this.userColumn = this.column
    } else {
      this.$message({
        type: 'error',
        message: '缺少列名信息'
      })
      return
    }
  },
}
</script>
<style lang="scss" scoped>
  .page-table {
    width: 100%;
    border: 1px solid #e3e3e3;
    border-bottom: none;
    border-radius: 4px;
  }
  .empty-table {
    text-align: center;
    padding: 56px 0 48px 0;
    .empty-icon {
        display: block;
        width: 75px;
        height: 57px;
        margin: 0px auto 0px auto;
        font-size: 67px;
    }
  }
}
</style>

重点说明

列数据配置
tableColumn: [ // 表格列数据
  {
    prop: 'stateNames',
    align: 'center',
    label: '阶段'
  },
  {
    prop: 'opinionsName',
    align: 'center',
    showOverflowTooltip: true,
    label: '意见内容'
  },
  {
    prop: 'operatorName',
    align: 'center',
    width: 150,
    label: '发布人'
  }
]
支持无数据插槽自定义和默认显示
<!-- 空状态显示 -->
<template slot="empty">
  <div class="empty-table">
    <i class="empty-icon el-icon-folder-opened" />
    <span class="empty-text">暂无数据</span>
  </div>
</template>
列渲染

列循环渲染具名插槽。这样子方便某些列自定义显示内容格式,如:显示Tag标签、Switch开关、内容点击Click绑定等

<!-- 列数据渲染 -->
<template v-for="column in userColumn">
  <el-table-column
    v-if="!column.hide"
    // XXX 列属性
    >
    <template slot-scope="{row}">
      <!-- 针对每列数据都设置作用域插槽,便于自定义显示内容 -->
      <slot
        :name="column.prop"
        :row="row"
        >{{ row[column.prop] }}</slot>
    </template>
  </el-table-column>
</template>
操作列

操作列的具名插槽。这里虽然是具名插槽,但并未给出作用域,因为此处并非是列!!!

<template slot="operation">
  <el-table-column
    fixed="right"
    label="操作"
    align="center"
    width="200"
  >
    <template slot-scope="{row}">
      <el-button
        @click="handleOperateData(row, 'detail')"
        type="text"
        size="small"
      >查看</el-button>
      <!-- 生效状态才可编辑 -->
      <el-button
        type="text"
        size="small"
        @click="handleOperateData(row, 'edit')"
      >编辑</el-button>
      <el-button
        type="danger"
        size="small"
        @click="handleOperateData(row, 'delete')"
      >删除</el-button>
    </template>
  </el-table-column>
</template>

http://www.kler.cn/news/290771.html

相关文章:

  • idea新建父工程和添加导入新模块的步骤
  • Gin 验证器详解与案例分析
  • 代码随想录第50天|图论
  • 【并行计算】CUDA基础
  • 行为型设计模式-命令(command)模式-python实现
  • C++判断语句(基础速通)ac-wing
  • OpenAI发布GPT-4o mini,3.5从此退出历史舞台?
  • 10.9 网络安全概述
  • watchdog: BUG: soft lockup - CPU#3 stuck for 23s! [swapper/0:1]
  • 微信小程序跳转到另一个微信小程序
  • 2025第十二届广州国际汽车零部件加工技术及汽车模具展览会
  • 替代 Django 默认 User 模型并使用 `django-mysql` 添加数据库备注20240904
  • 国内PFMEA的实施困境与价值探讨
  • 天气预报爬虫
  • kali——wpscan的使用
  • Mac工程动态库配置和加载探究
  • 【论文阅读】CiteTracker: Correlating Image and Text for Visual Tracking
  • RabbitMQ 03 在项目中的实际使用
  • Azure OpenAI Ingesion Job API returns 404 Resource not found
  • 【图论入门】图的存储
  • 【编程底层思考】什么是JVM对象内存分配的空间分配担保,咋担保的?
  • [环境配置]Pycharm手动安装汉化插件
  • Redis缓存预热方案详解:提升应用性能与用户体验
  • ActiViz实战:使用Actor2D画一个二维网格
  • Unity | 内存优化之资源冗余问题
  • python办公自动化:使用`Python-PPTX` 应用动画效果
  • 【Python】数据可视化之核密度
  • 监控MySQL数据恢复策略性能:深入指南
  • 【专题】2024年中国游戏出海洞察报告合集PDF分享(附原数据表)
  • ubuntu20.04(wsl2)测试 arcface 人脸识别(计算特征向量)