elementui table字段篩選展示

背景

在crm系統(tǒng)中囊颅,有時form表單字段非常多当悔,而實際需要查詢的條件只有幾個傅瞻,一是占用空間多分飞,二是從非常多的字段中不太好尋找嗽桩,所以,需要做一個字段篩選模版婆廊,只選擇有效的字段展示饼疙。效果如圖:


image.png

在此溺森,給其封裝成一個組件。

如何使用

1窑眯、引入組件

import FilterColumns from "@/components/filterColumns/index

2儿惫、注冊組件

  components: {
    FilterColumns
  },

3、調用組件

<FilterColumns ref="filterColumns" :filterColumns.sync="filterColumns"></FilterColumns>

4伸但、初始化數(shù)據(jù)

filterColumns: {
        tableName: 'pushMultipleTable',
        filtrateVisible: false,
        checkedColumn: [
        'itemId',
        'title',
        'subtitle',
        'icon',
        'pushTimeliness',
        'updatedUser',
        'updatedTime',
        's3Link', 
        'appIds',
        'countries',
        'language',
        'publishTime',
        ],
        // columnList: [],
        defaultShow: true,
        showColumnsKey: 'MEDIA_push_show_columns'
      },

mounted() {
    this.$refs.filterColumns.initTableColumn('pushMultipleTable')
  },

5肾请、模版使用

          <template slot="header">
            <!-- 過濾展示相 -->
            <el-button type="text" class="filtrate-btn" @click="filterColumns.filtrateVisible = !filterColumns.filtrateVisible">
              <svg-icon iconClass="filtrate"></svg-icon>
            </el-button>
          </template>
<el-table-column v-if="filterColumns.defaultShow || filterColumns.checkedColumn.indexOf('feedTimelines') !== -1" prop="feedTimelines" :label="$t('pushEdit.feedTimelines')" width="150"></el-table-column>

組件具體實現(xiàn)

<template>
  <div>
    <el-dialog :title="$t('pushEdit.editInfos')" :visible.sync="filterColumns.filtrateVisible" width="800px" class="filtrate-dialog">
      <el-checkbox :indeterminate="filtrateIsIndeterminate" v-model="filtrateCheckAll" @change="handleCheckAllChange" class="select-all">{{ $t("common.select_all") }}</el-checkbox>
      <el-checkbox-group v-model="filterColumns.checkedColumn" @change="handleCheckedFiltateChange">
        <template v-for="column in columnList">
          <el-checkbox v-if="column.property" :label="column.property" :key="column.id" :title="column.label">{{ column.label }}</el-checkbox>
        </template>
      </el-checkbox-group>
    </el-dialog>
  </div>
</template>
<script>
const ls = localStorage;
export default ({
  props: {
    filterColumns: {
      type: Object
    }
   

  },
  data () {
    return {
      filtrateIsIndeterminate: true,
      filtrateCheckAll: true,
      columnList: []
    }
  },
  methods: {
    /** 初始化表格展示箱 */
    initTableColumn(table) {
      // 獲取table所有可以自定義顯示隱藏的列
      console.log('===', this.$parent)
      this.$parent.$refs[table].columns.forEach((column) => {
        if (column.property) {
          this.columnList.push({
            id: column.id,
            property: column.property,
            label: column.label
          });
        }
      });
      this.$parent.filterColumns.defaultShow = false;
      this.filterColumns.checkedColumn = JSON.parse(ls.getItem(this.filterColumns.showColumnsKey)) || this.filterColumns.checkedColumn;

    },
    /** 表格列顯示控制 */
    handleCheckAllChange(val) {
      this.filterColumns.checkedColumn = val ? this.columnList.map((column) => column.property) : [];
      this.filtrateIsIndeterminate = false;
      ls.setItem(this.filterColumns.showColumnsKey, JSON.stringify(this.filterColumns.checkedColumn));
      this.$nextTick(() => {
        this.$parent.$refs[this.filterColumns.tableName].doLayout();
      });
    },
    /** 表格列顯示項選中變化 */
    handleCheckedFiltateChange(val) {
      const checkedCount = val.length;
      this.filtrateCheckAll = checkedCount === this.columnList.length;
      this.filtrateIsIndeterminate = checkedCount > 0 && checkedCount < this.columnList.length;
      ls.setItem(this.filterColumns.showColumnsKey, JSON.stringify(this.filterColumns.checkedColumn));
      this.$nextTick(() => {
        this.$parent.$refs[this.filterColumns.tableName].doLayout();
      });
    },
  }
})
</script>
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市更胖,隨后出現(xiàn)的幾起案子铛铁,更是在濱河造成了極大的恐慌,老刑警劉巖却妨,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饵逐,死亡現(xiàn)場離奇詭異,居然都是意外死亡彪标,警方通過查閱死者的電腦和手機倍权,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來捞烟,“玉大人薄声,你說我怎么就攤上這事√饣” “怎么了默辨?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長苍息。 經常有香客問我缩幸,道長,這世上最難降的妖魔是什么竞思? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任表谊,我火速辦了婚禮,結果婚禮上盖喷,老公的妹妹穿的比我還像新娘爆办。我一直安慰自己,他們只是感情好传蹈,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布押逼。 她就那樣靜靜地躺著,像睡著了一般惦界。 火紅的嫁衣襯著肌膚如雪挑格。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天沾歪,我揣著相機與錄音漂彤,去河邊找鬼。 笑死灾搏,一個胖子當著我的面吹牛挫望,可吹牛的內容都是我干的。 我是一名探鬼主播狂窑,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼媳板,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了泉哈?” 一聲冷哼從身側響起蛉幸,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎丛晦,沒想到半個月后奕纫,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡烫沙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年匹层,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锌蓄。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡升筏,死狀恐怖,靈堂內的尸體忽然破棺而出瘸爽,到底是詐尸還是另有隱情仰冠,我是刑警寧澤,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布蝶糯,位于F島的核電站洋只,受9級特大地震影響,放射性物質發(fā)生泄漏昼捍。R本人自食惡果不足惜识虚,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望妒茬。 院中可真熱鬧担锤,春花似錦、人聲如沸乍钻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至多糠,卻和暖如春累舷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背夹孔。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工被盈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人搭伤。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓只怎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親怜俐。 傳聞我的和親對象是個殘疾皇子身堡,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內容