最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue使用el-table篩選tree樹形結(jié)構(gòu)的數(shù)據(jù)問題

 更新時間:2024年07月25日 10:02:40   作者:蜂巢糖FCT  
這篇文章主要介紹了vue使用el-table篩選tree樹形結(jié)構(gòu)的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

實現(xiàn)難點

1.對于普通列數(shù)據(jù)el-table表格可做到多條件篩選,但是對于帶tree樹形結(jié)構(gòu)類型的數(shù)據(jù)只能篩選到最上層節(jié)點,子節(jié)點不會篩選

2.考慮到缺陷,因此查看文檔只能通過文檔提供的filter-change事件手動篩選數(shù)據(jù)。

思路

1.通過filter-change事件使用filterObj對象保存點擊的篩選狀態(tài)    

2.先將當(dāng)前樹形數(shù)據(jù)轉(zhuǎn)變成普通列數(shù)據(jù),再進(jìn)行手動過濾,過濾后以 普通列數(shù)據(jù)展示   

3.當(dāng)無篩選條件時再以樹形結(jié)構(gòu)展示 

圖例

實現(xiàn)步驟

1.數(shù)據(jù)初始化

data:{
    return{
         targetNode:{},//總數(shù)據(jù),保持tree數(shù)據(jù)類型
         tableData:[],//展示在table上的數(shù)據(jù)
         filterObj:{isEnable:[],filterTypeAttr:[],filterTypeCondition:[]},//過濾條件,由于表        格組件filterchange方法只會提示當(dāng)前的篩選項,
            //所以使用filterObj來保存所有篩選過的選項   
    }
}

2.表格

<el-table
 :data="tableData"
 row-key="id"
 border
 default-expand-all
 v-loading="loading"
 @filter-change="filterChange"
 ref="filterTable"
 :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
 <el-table-column prop="index"  label="順序"  width="180">
    <template slot-scope="scope">{{(scope.$index + 1)}}</template>
 </el-table-column>
 <el-table-column prop="filterName" label="篩選項" width="180">
    <template slot-scope="scope"><span :class="        
      [scope.row.filterTypeCondition==1?'filter-name':'']">{{ scope.row.filterName }} 
     </span><i v-if="scope.row.filterTypeAttr!==2" class="el-icon-plus" style="color: 
     #03a9f4;font-size: 12px;margin-left: 10px;cursor: pointer;" 
     @click="addCondition(scope.row)"></i>
   </template>
</el-table-column>
<el-table-column
  prop="filterTypeAttr" 
  column-key="filterTypeAttr"
  :filters="[{ text: '自定義', value: '1' }, { text: '機(jī)構(gòu)名稱', value: '2'},{ text: '無', 
  value: ''}]"
  label="篩選類型屬性">
    <template slot-scope="scope">{{ scope.row.filterTypeAttr==1?'自定義':      
      scope.row.filterTypeAttr==2?'機(jī)構(gòu)名稱':''}}</template>
    <template slot="header" slot-scope="scope">
       <!-- {{filterObj.filterTypeAttr.length==0?'篩選類型屬 
    性':filterObj.filterTypeCondition.length==1 && 
     filterObj.filterTypeCondition[0]==1?'1-篩選類型'
                        :filterObj.filterTypeCondition.length==1 && 
       filterObj.filterTypeCondition[0]==2?'2-篩選條件':'篩選類型屬性-全部'}} -->
                        {{filterObj.filterTypeAttr.length==3?'篩選類型屬性-全部':'篩選類型 
      屬性'+(filterObj.filterTypeAttr.includes(1)?'-自定義':'')+ 
      (filterObj.filterTypeAttr.includes(2)?'-機(jī)構(gòu)名稱':'')+ 
       (filterObj.filterTypeAttr.includes(0)?'-無':'')}}
     </template>
</el-table-column>
<el-table-column 
    prop="filterTypeCondition"
    column-key="filterTypeCondition"
    :filters="[{ text: '1-篩選類型', value: '1' }, { text: '2-篩選條件', value: '2'}]"
    label="標(biāo)識">
    <template slot-scope="scope">{{ scope.row.filterTypeCondition==1?'1-篩選類型':'2-篩選條件'}}</template>
     <template slot="header" slot-scope="scope">
       {{filterObj.filterTypeCondition.length==0?'標(biāo)識':filterObj.filterTypeCondition.length==1 && filterObj.filterTypeCondition[0]==1?'標(biāo)識-1-篩選類型':filterObj.filterTypeCondition.length==1 && filterObj.filterTypeCondition[0]==2?'標(biāo)識-2-篩選條件':'標(biāo)識-全部'}}
    </template>
</el-table-column>
<el-table-column prop="defaultSelected" label="是否屬于默認(rèn)展示項">
 <template slot-scope="scope">
   {{scope.row.defaultSelected==1?'是':scope.row.defaultSelected==0?'否':''}}
 </template>
</el-table-column>
<el-table-column prop="action" label="操作" width="250">
  <template slot-scope="scope">
    <span v-if="scope.row.filterTypeCondition==2" class="scope-span" :class=" 
   [scope.row.rule && scope.row.rule!=''?'filter-span':'']" 
    @click="showRegular(scope.row)">規(guī)則式</span>
    <span class="scope-span" @click="updateFilter(scope.row)">編輯</span>
        <el-popconfirm
           title="是否確定刪除?"
           @confirm="delFilter(scope.row)"
          >
             <span class="scope-span" slot="reference" >刪除</span>
       </el-popconfirm>
   </template>
</el-table-column>
<el-table-column 
    prop="isEnable"
    label="狀態(tài)" 
    column-key="isEnable"
    :filters="[{ text: '啟用', value: '1' }, { text: '禁用', value: '0' }]"
                    >
   <template slot-scope="scope">{{ scope.row.isEnable==1?'啟用':'禁用'}}</template>
   <template slot="header" slot-scope="scope">
       {{filterObj.isEnable.length==0?'狀態(tài)'
        :filterObj.isEnable.length==1 && filterObj.isEnable[0]==1?'狀態(tài)-啟用'
        :filterObj.isEnable.length==1 && filterObj.isEnable[0]==0?'狀態(tài)-禁用':'狀態(tài)-全部'}}
   </template>
</el-table-column>
</el-table>

要點:

  • default-expand-all:默認(rèn)展開全部子節(jié)點
  • filterChange:獲取點擊的篩選條件狀態(tài),對應(yīng)列表頭上設(shè)置:filters屬性的列

<template slot="header" slot-scope="scope">設(shè)置篩選后用來替換表頭的label內(nèi)容

3.filterChange函數(shù)

filterChange(filters){//觸發(fā)過濾事件時過濾數(shù)據(jù)
        console.log('filters',filters);
        if(filters['isEnable']){
            this.filterObj['isEnable'] = filters['isEnable'].map(Number);
        }
        if(filters['filterTypeAttr']){
            this.filterObj['filterTypeAttr'] = filters['filterTypeAttr'].map(Number);
        }
        if(filters['filterTypeCondition']){
            this.filterObj['filterTypeCondition'] = filters['filterTypeCondition'].map(Number);
        }
 
        if(this.filterObj['isEnable'].length==0 && this.filterObj['filterTypeAttr'].length==0 && this.filterObj['filterTypeCondition'].length==0){
            this.tableData = this.targetNode.filterList;
        }else{
            let dataList = [];
            this.targetNode.filterList.forEach(item=>{//層級數(shù)據(jù)轉(zhuǎn)變成普通列數(shù)據(jù)
                let obj = {
                    filterName:item.filterName,
                    filterTypeAttr:item.filterTypeAttr,
                    sort:item.sort,
                    isEnable:item.isEnable,
                    columnId:item.columnId,
                    id:item.id,
                    filterTypeCondition:1,
                    index:item.index
                }
                dataList.push(obj);
                if(item.children){
                    dataList = dataList.concat(this.getLineData(item.children))
                }
               
            }) 
            console.log('線性數(shù)據(jù)',dataList);
            if(this.filterObj['isEnable'].length>0){//狀態(tài)
                dataList = dataList.filter(item=>{
                    return this.filterObj['isEnable'].includes(item.isEnable)
                }) 
            }
            if(this.filterObj['filterTypeCondition'].length>0){//標(biāo)識
                dataList = dataList.filter(item=>{
                    return this.filterObj['filterTypeCondition'].includes(item.filterTypeCondition)
                }) 
            }
            if(this.filterObj['filterTypeAttr'].length>0){//屬性
                console.log('11',this.filterObj['filterTypeAttr']);
                dataList = dataList.filter(item=>{
                    return this.filterObj['filterTypeAttr'].includes(item.filterTypeAttr?item.filterTypeAttr:0)
                }) 
            }
           
            console.log('過濾數(shù)據(jù)',dataList);
            this.tableData = dataList;
        }
        
      },
      getLineData(data){//層級數(shù)據(jù)變成行數(shù)據(jù)
        let list = [];
        data.forEach(item=>{
            let obj = {};
            if(item.filterTypeCondition==1){//篩選類型
                obj = {
                    filterName:item.filterName,
                    filterTypeAttr:item.filterTypeAttr,
                    sort:item.sort,
                    isEnable:item.isEnable,
                    parentId:item.parentId,
                    id:item.id,
                    filterTypeCondition:1,
                    index:item.index
                }
            }else{
                obj = {
                    filterName:item.filterName,
                    sort:item.sort,
                    isEnable:item.isEnable,
                    parentId:item.parentId,
                    id:item.id,
                    defaultSelected:item.defaultSelected,
                    filterTypeCondition:2,
                    index:item.index
                }
            }
            list.push(obj);
            if(item.children){
                list = list.concat(this.getLineData(item.children))
            }
        })
        return list;
      },

步驟

  • 1.fiters參數(shù)為當(dāng)前點擊的篩選項,數(shù)據(jù)結(jié)構(gòu)如下:

  • 2.使用filterObj保存當(dāng)前篩選狀態(tài),篩選重置時為[]空數(shù)組
  • 3.保存后判斷filterObj各項是否都是空數(shù)組,是則直接賦值
  • 4.否則將樹形結(jié)構(gòu)轉(zhuǎn)換成普通列數(shù)據(jù),注意考慮淺克隆的問題,getLineData函數(shù)作用是將子節(jié)點children也插入到普通列中
  • 5.數(shù)據(jù)轉(zhuǎn)換成普通列數(shù)據(jù)后進(jìn)行過濾

實現(xiàn)效果

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • 深入詳解Vue3 ref底層實現(xiàn)原理

    深入詳解Vue3 ref底層實現(xiàn)原理

    隨著現(xiàn)在vue3越來越普及,相應(yīng)的面試題也多了起來。說到vue3的面試題,有一個最經(jīng)典的就是對于實現(xiàn)ref和reactive這兩個方法的底層原理,本文就來和大家簡單講講吧
    2023-04-04
  • vue使用import.meta.glob動態(tài)導(dǎo)入多個模塊方式

    vue使用import.meta.glob動態(tài)導(dǎo)入多個模塊方式

    文章介紹了Vite提供的`import.meta.glob`功能,用于動態(tài)導(dǎo)入多個模塊,它通過glob模式匹配文件,返回一個對象,鍵為文件路徑,值為返回Promise的動態(tài)導(dǎo)入函數(shù),支持eager模式直接導(dǎo)入和as選項指定導(dǎo)入方式,如原始字符串或文件URL,適用于Vite或Rollup項目
    2025-10-10
  • Vue實現(xiàn)開關(guān)按鈕拖拽效果

    Vue實現(xiàn)開關(guān)按鈕拖拽效果

    這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)開關(guān)按鈕拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • uniapp-ios開發(fā)之App端與webview端相互通信的方法以及注意事項

    uniapp-ios開發(fā)之App端與webview端相互通信的方法以及注意事項

    在uni-app與Webview之間進(jìn)行數(shù)據(jù)交互是非常常見的需求,下面這篇文章主要給大家介紹了關(guān)于uniapp-ios開發(fā)之App端與webview端相互通信的方法以及注意事項的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • el-table多選toggleRowSelection不生效解決方案

    el-table多選toggleRowSelection不生效解決方案

    這篇文章主要給大家介紹了關(guān)于el-table多選toggleRowSelection不生效的解決方案,文中通過圖文以及代碼將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Vue3中其他的Composition?API詳解

    Vue3中其他的Composition?API詳解

    這篇文章主要介紹了Vue3中其他的Composition?API,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Vuex的四個常用核心概念解讀

    Vuex的四個常用核心概念解讀

    本文詳細(xì)解析了Vuex中的四個核心概念及其區(qū)別,包括State用于存儲狀態(tài),Getters用于計算屬性并緩存,Mutations是唯一更改State的同步方法,Actions則用于提交Mutations且支持異步操作
    2024-11-11
  • 如何解決vue項目打包后文件過大問題

    如何解決vue項目打包后文件過大問題

    這篇文章主要介紹了如何解決vue項目打包后文件過大問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 使用v-memo緩存模板子樹提高應(yīng)用性能詳解

    使用v-memo緩存模板子樹提高應(yīng)用性能詳解

    這篇文章主要為大家介紹了使用v-memo緩存模板子樹提高應(yīng)用性能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Vue使用new Image()實現(xiàn)圖片預(yù)加載功能

    Vue使用new Image()實現(xiàn)圖片預(yù)加載功能

    這篇文章主要介紹了如何在 Vue 中實現(xiàn)圖片預(yù)加載的一個簡單小demo以及優(yōu)化方案,文中通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-11-11

最新評論

乌鲁木齐县| 湘潭县| 民和| 平阳县| 新和县| 神池县| 礼泉县| 宝应县| 开阳县| 乌恰县| 大连市| 浦江县| 商水县| 钦州市| 平远县| 九寨沟县| 东丽区| 玛多县| 余庆县| 莱州市| 同江市| 兴隆县| 长阳| 铁力市| 衡水市| 察隅县| 新余市| 垣曲县| 甘洛县| 北宁市| 榆中县| 洪雅县| 珲春市| 边坝县| 东安县| 防城港市| 祁阳县| 葵青区| 济阳县| 宜宾市| 固原市|