elementUI el-table组件封装(自定义列)

24 篇文章 1 订阅
<template>
    <div class="loading-table" v-loading="tableLoading">
        <el-table :data="tableData" border stripe>
            <el-table-column type="index" label="序号" align="center" width="80">
                <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"
                    width="200"
                    v-if="item.prop === 'monitoringTime'"
                >
                <!--monitoringTime较长显示不全,字段特殊处理-->
                    <template slot-scope="scope">
                        <span>{{ scope.row[scope.column.property] }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    :key="index"
                    :prop="item.prop"
                    :label="item.label"
                    :align="item.align || 'center'"
                    :show-overflow-tooltip="item.overHidden || true"
                    v-else
                >
                    <template slot-scope="scope">
                        <span>{{ scope.row[scope.column.property] }}</span>
                    </template>
                </el-table-column>
            </template>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            layout="total, prev, pager, next"
            :total="page.total"
            :page-size="page.size"
            @current-change="pageChange"
        >
        </el-pagination>
    </div>
</template>
<script>
export default {
    name: 'TableComponents', //table组件
    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,
                }
            },
        },
    },
    data() {
        return {}
    },
    methods: {
        //分页改变
        pageChange(val) {
            this.$emit('page-change', val)
        },
    },
}
</script>
<style scoped>
.loading-table {
    width: 100%;
}
</style>

以上为组件代码

//父组件
<template>
    <div class="loadingPageDetail" v-if="showDetail">
        <div class="dialog-top">
            <a :title="'关闭弹出层'" @click="close()"></a>
        </div>
        <div class="dialog-content">
            <div class="title">
                {{ title }}
            </div>
            <TableComponents
                :tableLoading="tableLoading"
                :tableData="tableData"
                :page="page"
                :tableOption.sync="tableOption"
                @page-change="getList"
            />
        </div>
    </div>
</template>
<script>
import { getMeasurementData } from '@index/api/specialLayer'
import TableComponents from './TableComponents'
export default {
    name: 'EquipmentDetailDialog', //设备详情弹窗
    props: {
        showDetail: {
            type: Boolean,
            default: false,
        },
        deviceId: {
            type: String,
            default: '',
        },
        title: {
            type: String,
            default: '',
        },
    },
    components: {
        TableComponents,
    },
    data() {
        return {
            tableLoading: false,
            tableOption: [],
            tableData: [],
            page: {
                total: 0,
                current: 1,
                size: 10,
            },
        }
    },
    watch: {
        showDetail(v) {
            if (v) {
                this.page.current = 1
                this.tableData = []
                this.loadingData()
            }
        },
    },
    methods: {
        loadingData() {
            // 获取数据
            let requestUrl = '/iot/getMeasurementData'
            let params = {
                deviceId: this.deviceId,
                pageNo: this.page.current,
                pageSize: this.page.size,
            }
            getMeasurementData({ url: requestUrl, params: params })
                .then(res => {
                    if (res && res.status === 0) {
                        this.tableData = res.result[0].monitoringDataList
                        this.processData(res.result)
                        this.page.total = res.total
                    }
                })
                .catch(() => {})
        },
        //数据处理
        processData(params) {
            if (params) {
                this.tableOption = [{ label: '监测时间', prop: 'monitoringTime' }]
                this.tableData = []
                params.forEach((v, index) => {
                    let obj = {}
                    v.monitoringDataList.forEach((m, index1) => {
                        if (index === 0) this.tableOption.push({ label: m.flagName, prop: `option${index1}` })
                        obj[`option${index1}`] = `${m.measurement}${m.unit}`
                    })
                    obj.monitoringTime = v.monitoringTime
                    this.tableData.push(obj)
                })
            } else {
                return []
            }
        },
        //分页改变,加载分页数据
        getList(v) {
            this.page.current = v
            this.loadingData()
        },
        //关闭弹窗-清空数据
        close() {
            this.tableData = []
            this.$emit('closeDetail', false)
        },
    },
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.loadingPageDetail {
    min-width: 600px;
    height: auto;
    min-height: 200px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 9999;
    background-color: #fff;
    padding: 20px;
    .dialog-top {
        display: block;
        height: 15px;
        a {
            width: 16px;
            height: 16px;
            background-image: url('../../../home/assets/close.png');
            background-size: 100%;
            position: absolute;
            z-index: 9;
            display: block;
            top: 10px;
            right: 10px;
        }
    }
    .dialog-content {
        height: auto;
        margin-top: 10px;
        .title {
            height: 48px;
            line-height: 48px;
            padding-left: 10px;
            border-top: 1px solid #ebeef5;
            border-left: 1px solid #ebeef5;
            border-right: 1px solid #ebeef5;
        }
    }
    /deep/ .el-table td,
    /deep/ .el-table th {
        padding: 9px 0;
    }
}
</style>

表头数据格式:

表格数据格式(表头prop需要和option一一对应):

页面效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

木易66丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值