自定义列(Ant Design Vue)

1、效果图(有不懂的可以私信)

<template>
  <a-popover title="自定义列" trigger="click" placement="leftBottom">
    <template slot="content">
      <a-checkbox-group @change="onShowedColsChange" v-model="showedCols" style="width: 400px;">
        <a-row>
          <template v-for="(column, index) in columnDefs_">
            <a-col :span="12">
              <a-checkbox :value="column.dataIndex || column.key" :disabled="isIgnored(column)">{{
                column.title
              }}</a-checkbox>
            </a-col>
          </template>
        </a-row>
      </a-checkbox-group>
    </template>
    <a-button type="link" icon="setting">自定义列</a-button>
  </a-popover>
</template>
<script>
import Vue from 'vue'
export default {
  name: 'TableColumnsSetter',

  model: {
    prop: 'columnDefs',
    event: 'change'
  },
  props: {
//传递过来的columns值
    columnDefs: {
      type: Array,
      required: true
    },
    ignoredCols: {
      //column[i].key字符串'key1,key2'
      type: String,
      default: 'rowIndex,action'
    },
//传递出来的需要显示的字段
    round: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      columnDefs_: [], //保存列定义
      showedCols: [] //columnDefs[i].key字符串数组
    }
  },
  mounted() {
    this.columnDefs_.push(...this.columnDefs)
    this.init()
  },
  methods: {
    init() {
      //权限过滤(列权限控制时打开,修改第二个参数为授权码前缀)
      //this.columnDefs_ = colAuthFilter(this.columnDefs_,'testdemo:');
//转化为数组
      this.showedCols = this.round.mainColumns.split(',')
      let columns = []
      if (!this.showedCols) {
        this.showedCols = []
        this.columnDefs_.forEach((column, i, array) => this.showedCols.push(column.dataIndex || column.key))
        columns = this.columnDefs_
      } else {
        columns = this.columnDefs_.filter(column => {
          if (this.isIgnored(column)) return true
          if (this.showedCols.includes(column.dataIndex || column.key)) return true
          return false
        })
      }
      this.$emit('change', columns)
    },
    //列设置更改事件
    onShowedColsChange(checkedValues) {
//本地存储选中的列
      let that = this
      let USETT = Vue.ls.get('USETTINGS')
      let coles = this.showedCols
      USETT.forEach(item => {
        if (item.mainTitle == that.round.mainTitle) {
          item.mainColumns = coles.toString()
        }
      })
      Vue.ls.set('USETTINGS', USETT, 7 * 24 * 60 * 60 * 1000)
      const columns = this.columnDefs_.filter(column => {
        if (this.isIgnored(column)) return true
        return this.showedCols.includes(column.dataIndex || column.key)
      })
      this.$emit('change', columns)
    },
    isIgnored(column) {
      return this.ignoredCols.indexOf(column.dataIndex || column.key) >= 0
    }
  }
}
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

My&Liu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值