iView组件动态控制表格列的显示与隐藏

需求:

  1. 根据当前登录用户角色动态控制是否展示业务经理查看状态列和大区经理查看状态列
  2. 列表中为金额的列,其值如果全为0则该列不展示

实现:

<!-- 此处的Tablepage为重新封装过后的表格组件 -->
<template>
	<div>
		<Tablepage ref="table" :data="listData" :total="total" :columns="newColumns" :pagination="true" border @change-page="onChangePage" @reset="onReset" @on-current-change="onCurrentChange" @on-selection-change="onSelectionChange" show-btn :init-data="false">
			<template slot="searchform">
				<!-- ......此处省略查询条件 -->
			</template>
		</Tablepage>
	</div>
</template>

<script>
	import { rowChangeMixin } from '@/mixins/table'
	import { numFormat } from '@/libs/util'
	import { getListData } from '@/api/reports'
	import { getUserInfo } from '@/api/user'
	
	export default {
        name: 'HelloWorld',
        mixins: [rowChangeMixin],
        data() {
            return {
            	queryParams: {
            		empNo: '',
            		deptName: '',
            		// ...
            	},
            	userType: null, // 1-业务经理  2-大区经理  0-其他
                newColumns: [],
                columns: [
                    {
                        type: 'selection',
                        width: 50,
                        align: 'center'
                    },
                    {
                        type: 'index',
                        width: 50,
                        align: 'center'
                    },
                    {
                        title: '工号',
                        key: 'empNo',
                        width: 80,
                        align: 'center'
                    },
                    {
                        title: '姓名',
                        key: 'empName',
                        width: 100
                    },
                    {
                        title: '部门',
                        key: 'deptName',
                        minWidth: 180
                    },
                    {
                        title: '金额1',
                        key: 'money1',
                        width: 120,
                        align: 'right',
                        isMoney: true,
                        render(h, params) {
                            return h('span', null, numFormat(params.row.money1))
                        }
                    },
                    {
                        title: '金额2',
                        key: 'money2',
                        width: 120,
                        align: 'right',
                        isMoney: true,
                        render(h, params) {
                            return h('span', null, numFormat(params.row.money2))
                        }
                    },
                    {
                        title: '金额3',
                        key: 'money3',
                        width: 120,
                        align: 'right',
                        isMoney: true,
                        render(h, params) {
                            return h('span', null, numFormat(params.row.money3))
                        }
                    },
                    {
                        title: '金额4',
                        key: 'money4',
                        width: 120,
                        align: 'right',
                        isMoney: true,
                        render(h, params) {
                            return h('span', null, numFormat(params.row.money4))
                        }
                    },
                    {
                        title: '金额5',
                        key: 'money5',
                        width: 120,
                        align: 'right',
                        isMoney: true,
                        render(h, params) {
                            return h('span', null, numFormat(params.row.money5))
                        }
                    }
                ],
                list: (queryParams) => {
                	return getListData(Object.assign({ ...this.queryParams }, queryParams))
                }
            }
        },
        mounted() {
        	this.getUserInfo()
        },
        methods: {
        	// 获取当前登录用户角色
            getUserInfo() {
                getUserInfo().then(res => {
                    let data = res.data.data
                    this.userType = data.roleName == '业务经理' ? 1 : data.roleName == '大区经理' ? 2 : 0
                    // 设置当前登录用户为业务经理或大区经理时不展示业务经理查看状态和大区经理查看状态列
                    if (this.userType != 1 && this.userType != 2) {
                        let arr = [{
                            title: '业务经理查看状态',
                            key: 'ifManagerView',
                            align: "center",
                            width: 70,
                            render(h, { row }) {
                                return h("span", {
                                    style: {
                                        color: row.ifManagerView == '关闭' ? '#F56C6C' : '#67c23a'
                                    }
                                }, row.ifManagerView)
                            }
                        },
                        {
                            title: '大区经理查看状态',
                            key: 'ifBigManagerView',
                            width: 70,
                            align: "center",
                            render(h, { row }) {
                                return h("span", {
                                    style: {
                                        color: row.ifBigManagerView == '关闭' ? '#F56C6C' : '#67c23a'
                                    }
                                }, row.ifBigManagerView)
                            }
                        }]
                        this.columns.push(...arr)
                    }
                    this.loadData(true)
                })
            },
            // 获取列表数据
            getList() {
            	this.setColumnsStatus(res.data.data.list)
            	this.listData = res.data.data.list
            	this.total = res.data.data.total
            },
            // 重置
            onReset() {
            	let data = this.$options.data().queryParams
            	Object.assign(this.queryParams, data)
            	this.onRefresh()
            },
            // 设置列表总金额全为0的列隐藏
            setColumnsStatus(data) {
                let newColumns = []
                for (let colItem of this.columns) {
                    let colLabel = colItem.key;
                    if (colLabel && colItem.isMoney) {
                        for (let item of data) {
                            if (item[colLabel] && item[colLabel] !== '0') {
                                newColumns.push({ ...colItem })
                                break
                            }
                        }
                    } else {
                        newColumns.push({ ...colItem })
                    }
                }
                this.newColumns = newColumns
            },
           // ......  此处省略其他代码
        }
	}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Komorebi゛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值