element的el-table-column循环渲染和自定义列

本文介绍了一个基于Vue的自定义表格组件实现方法,包括动态渲染列、自定义列内容、分页器等功能,并展示了如何使用插槽来实现灵活的数据展示。

// 自定义组件
<template>
    <div class="x__table>
        <el-table
            v-loading="tableLoading"
            :data="tableData"
            border
            stripe>
            <el-table-column
                v-if="index"
                type="index"
                label="序号"
                align="center"
                width="50">
                <template slot-scope="scope">
                    <span>{{ scope.$index + (page.current - 1) * page.size + 1 }}</span>
                </template>
            </el-table-column>
            <template v-for="(item, index) in tableOption">
                <el-table-column
                    :key="index"
                    :prop="item.prop"
                    :label="item.label"
                    :align="item.align || 'center'"
                    :show-overflow-tooltip="item.overHidden || true">
                    <template slot-scope="scope">
                        // 这里通过插槽实现自定义列
                        <slot
                            v-if="item.slot"
                            :name="scope.column.property"
                            :row="scope.row"
                            :$index="scope.$index"
                        />
                        <span v-else>{{ scope.row[scope.column.property] }}</span> // 这里的property自己打印出来看看就明白了
                    </template>
                </el-table-column>
            </template>
            <el-table-column
                label="操作"
                align="center">
                <template slot-scope="scope">
                    <slot
                        name="menu"
                        :row="scope.row"
                        :$index="scope.$index"
                    />
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 --> // 这里的分页器也是封装的一个组件,主要是和序号关联起来
        <Pagination
            v-show="page.total>0"
            :total="page.total"
            :page.sync="page.current"
            :limit.sync="page.size"
            @pagination="pageChange"
        />
    </div>
</template>

<script>
import Pagination from '@/components/Pagination/index' // 有需要的下次再贴代码
export default {
  name: 'XTable',
  components: {
    Pagination
  },
  props: {
    index: {
      type: Boolean,
      default: function() {
        return true
      }
    },
    tableLoading: {
      type: Boolean,
      default: function() {
        return false
      }
    },
    tableData: {
      type: Array,
      default: function() {
        return []
      }
    },
    tableOption: {
      type: Array,
      default: function() {
        return []
      }
    },
    page: {
      type: Object,
      default: function() {
        return {
          total: 0,
          current: 1,
          page: 10
        }
      }
    }
  },
  methods: {
    pageChange() {
      this.$emit('page-change')
    }
  }
}
</script>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值