二次封装 - 表格el-table

         感觉表格的二次封装相对来说,考虑的问题没有那么多,最多就是在表格里面展示除了文字以外、其他特殊的(像按钮,图片等之类的可能比较麻烦一点),但是这种比较麻烦一点的展示,其实可以通过插槽去解决……

<!-- stripe 标识表格开启斑马纹效果 header-cell-style可自定义表头样式 -->
<el-table
  fit
  stripe
  :header-cell-style="{
    background: '#394D94',
    color: '#ffffff',
  }"
  :data="pageData"
  style="width: 100%"
  max-height="500px"
  height="250"
  center
>
  <!-- 表格是否又可选 -->
  <el-table-column
    v-if="isSelect"
    width="40"
    type="selection"
  ></el-table-column>
  <!-- 表格是否有自定义的序号 -->
  <el-table-column
    v-if="isIndex"
    label="序号"
    type="index"
    :index="indexMethod"
    width="70"
    fixed
  ></el-table-column>
  <!-- 循环渲染表格数据数据 -->
  <el-table-column
    v-for="item in tableColumns"
    :key="item.key"
    :prop="item.key"
    v-bind="item"
    :fixed="item.fixed"
    align="center"
  >
    <!-- 如果表格的头部是两层,则可以这样显示 -->
    <el-table-column
      v-for="itemChildren in item.children"
      :key="itemChildren.key"
      :prop="itemChildren.key"
      v-bind="itemChildren"
      align="center"
    >
      <template slot-scope="scope">
        <!-- 一般通过custom来标识是否走插槽展示一些特殊的字段 -->
        <slot
          v-if="itemChildren.custom"
          :data="scope"
          :itemChildren="itemChildren"
        ></slot>
        <template v-if="!itemChildren.isImg && !itemChildren.custom">{{
          scope.row[itemChildren.key]
        }}</template>
      </template>
    </el-table-column>

    <!-- 展示table表格的单元格的内容 -->
    <template slot-scope="scope">
      <slot v-if="item.custom" :data="scope" :item="item"></slot>
      <template v-if="!item.isImg && !item.custom">{{
        scope.row[item.key]
      }}</template>
    </template>
  </el-table-column>
</el-table>
<!-- 分页 -->
<div class="pagination">
  <el-pagination
    style="margin-top: 10px; margin-right: 0"
    layout="total, sizes, prev, pager, next, jumper"
    :total="paginationData.total"
    :current-page.sync="paginationData.pageIndex"
    :page-size.sync="paginationData.pageSize"
    :page-sizes="[5, 10, 15, 20]"
  >
  </el-pagination>
</div>

// 模板中的相关逻辑
<script>
export default {
  data() {
    return {};
  },
  props: {
    // 分页数据
    paginationData: {
      type: Object,
      default: () => {},
    },
    // 表格数据
    tableData: {
      type: Array,
      default: () => [],
    },
    // 表格展示数据
    tableColumns: {
      type: Array,
      default: () => [],
    },
    // 是否展示可选框
    isSelect: {
      type: Boolean,
      default: () => false,
    },
    // 是否展示序号
    isIndex: {
      type: Boolean,
      default: () => false,
    },
  },
  computed: {
    pageData() {
      return this.tableData.slice(
        (this.paginationData.pageIndex - 1) * this.paginationData.pageSize,
        this.paginationData.pageIndex * this.paginationData.pageSize
      );
    },
  },
  methods: {
    // 改变选中行的回调
    changeSelection(val) {
      this.$emit("changeSelection", val);
    },
    // 改变页码或者数量
    changeIndexOrPage() {
      console.log(val);
    },
    // 自定义索引(序号)
    indexMethod(index) {
      return (
        index +
        this.paginationData.pageSize * (this.paginationData.pageIndex - 1) +
        1
      );
    },
  },
};
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BlackStar-Coder

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

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

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

打赏作者

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

抵扣说明:

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

余额充值