vue的el-table通過(guò)表頭勾選實(shí)現(xiàn)列自定義

近期要實(shí)現(xiàn)一個(gè)el-table的表頭自定義功能,可以根據(jù)復(fù)選框的選中或者不選實(shí)現(xiàn)表格列的顯示隱藏,自定義展示表格列表,通過(guò)網(wǎng)上的一些例子來(lái)實(shí)現(xiàn)了一下。
實(shí)現(xiàn)的效果如下:


WechatIMG15.jpeg

WechatIMG14.jpeg

代碼如下:
1. table表格部分

<el-table
     border
     :data="list"
     stripe
     max-height="460"
   >
    <af-table-column
      type="selection"
      fixed
      width="55">
    </af-table-column>
     <af-table-column
       v-for="(item,index) in tableHeader"
       v-if="colData[index].istrue"
       :key="item.key"
       show-overflow-tooltip
       :label="item.label"
       :header-align = "headFormat"
       :resizable = "resizable"
     >
     <template slot-scope="scope">
       {{scope.row[item.key]}}
     </template>
     </af-table-column>
   </el-table>

2.數(shù)據(jù)部分

data () {
  return {
      list: [
        {
          id: '上海市普陀',
          zbh: '上海市普陀',
          kind: '上海市普',
          qd: '上海市普',
          user: '上海市普',
          id1: '1上海市普',
        }
      ],
      colOptions: ['上海市普', '上海市普22', '核心存款時(shí)點(diǎn)', '所屬機(jī)構(gòu)編號(hào)1'], // 多選框的選擇項(xiàng)
      colSelect: ['上海市普', '上海市普22', '核心存款時(shí)點(diǎn)', '所屬機(jī)構(gòu)編號(hào)1'], // 多選框已選擇的內(nèi)容,即表格中顯示的列
      // istrue屬性存放列的狀態(tài)
      colData: [
        { title: '上海市普', istrue: true },
        { title: '上海市普22', istrue: true },
        { title: '核心存款時(shí)點(diǎn)', istrue: true },
        { title: '所屬機(jī)構(gòu)編號(hào)1', istrue: true }
      ],
      tableHeader: [
        { label: '上海市普', key: 'zbh' },
        { label: '上海市普22', key: 'kind' },
        { label: '核心存款時(shí)點(diǎn)', key: 'qd' },
        { label: '所屬機(jī)構(gòu)編號(hào)1', key: 'id1' }
      ]
  }
}

3.方法

watch: {
    colOptions(newVal, oldVal) {
      if (newVal) { // 如果有值發(fā)生變化,即多選框的已選項(xiàng)變化
        var arr = this.colSelect.filter(i => newVal.indexOf(i) < 0) // 未選中
        this.colData.filter(i => {
          if (arr.indexOf(i.title) !== -1) {
            i.istrue = false
          } else {
            i.istrue = true
          }
        })
      }
    }
  }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容