app-quick-group-tab.vue 5.6 KB
Newer Older
1
<template>
2
<div>
3
  <div class="app-quick-group">
4
    <div :class="{'quick-group-tab':true,'app-seleted-item':isSelectedItem(item) || item.childSelected}" v-for="(item,index) in showItems" :key="index">
5 6
      <div
        :style="{color:item.color}"
7
        @click="handleClick(item)"
8 9 10
      >
        <ion-icon v-if=" item.iconcls && !Object.is(item.iconcls, '')" :name="item.iconcls"></ion-icon>
        <img v-else-if="item.icon && !Object.is(item.icon, '')" :src="item.icon" />
11 12 13
        <span v-if="item.selectChildLabel" class="app-quick-item-label">{{item.selectChildLabel}}</span>
        <span v-else class="app-quick-item-label">{{item.label}}</span>
        <ion-icon v-if="item.children" name="caret-down-outline" style="margin-left:4px"></ion-icon>
14
      </div>
15
      <ion-badge class="badge" v-if="isSelectedItem(item) && pageTotal !== 0 && !item.children">{{pageTotal}}</ion-badge>
16
      <ion-badge class="badge" v-if="item.childSelected && pageTotal">{{pageTotal}}</ion-badge>
17 18
    </div>
  </div>
19
  <div ref="child-list" :class="{'child-list':true,'open':subItems.length > 0}">
20
    <div :class="{'child':true,'selected':item.selected}" v-for="(item,index) in subItems" :key="index" @click="handleClick(item)">
21 22 23
      <span>
        <ion-icon v-if=" item.iconcls && !Object.is(item.iconcls, '')" :name="item.iconcls"></ion-icon>
        <img v-else-if="item.icon && !Object.is(item.icon, '')" :src="item.icon" />
24
        <span>{{item.label}}</span>
25
      </span>
26
      <ion-badge class="badge" v-if="pageTotal !== 0 && item.selected">{{pageTotal}}</ion-badge>
27
      <ion-icon size="small" v-if="item.selected" style="margin-left:auto;color:green" name="checkmark-outline"></ion-icon>
28 29
    </div>
  </div>
30
  <ion-backdrop style="height:100vh;z-index:-1" v-show="subItems.length > 0" visible="true" tappable="true" @ionBackdropTap="closeBackdrop"></ion-backdrop>
31
</div>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
</template>

<script lang="ts">
import {
  Vue,
  Component,
  Prop,
  Provide,
  Emit,
  Watch,
} from "vue-property-decorator";
@Component({
  components: {},
})
export default class AppQuickGroupTab extends Vue {
  /**
   * 快速分组代码表
   *
   * @type {any[]}
   * @memberof ViewQuickGroupTab
   */
  @Prop({ default: () => [] }) public items!: Array<any>;

55 56 57 58 59 60 61 62
    /**
   * 快速分组代码表
   *
   * @type {any[]}
   * @memberof ViewQuickGroupTab
   */
  @Prop({ default:0 }) public pageTotal!: number;

63 64 65 66 67 68 69 70
  /**
   * 渲染列表
   *
   * @type {any[]}
   * @memberof AppQuickGroup
   */
  public showItems: any[] = [];

71 72 73 74 75 76 77 78
  /**
   * 子项列表
   *
   * @type {any[]}
   * @memberof AppQuickGroup
   */
  public subItems: any[] = [];

79 80 81 82 83 84 85 86 87 88
  /**
   * 监控代码表变化
   *
   * @memberof AppQuickGroup
   */
  @Watch("items", { immediate: true })
  public itemsWatch(): void {
    if (this.items) {
      this.items.every((item: any) => {
        if (item.default) {
89 90 91
          this.$nextTick(() => {
            this.handleClick(item, true);
          })
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        }
        return !item.default;
      });
      this.showItems = this.handleDataSet(this.items);
    }
  }

  /**
   * UI选中项
   *
   * @type {*}
   * @memberof AppQuickGroup
   */
  public selectedUiItem: any = {};

  /**
   * 是否选中当前项
   *
   * @param item 传入当前项
   * @memberof AppQuickGroup
   */
  public isSelectedItem(item: any): boolean {
    if (this.selectedUiItem && this.selectedUiItem.id === item.id) {
      return true;
    } else {
      return false;
    }
  }

  /**
   * 处理代码表返回数据(树状结构)
   *
   * @param result 返回数组
   * @memberof AppQuickGroup
   */
  public handleDataSet(result: Array<any>): any[] {
    let list: Array<any> = [];
    if (result.length === 0) {
      return list;
    }
    result.forEach((codeItem: any) => {
      if (!codeItem.pvalue) {
        let valueField: string = codeItem.value;
        this.setChildCodeItems(valueField, result, codeItem);
        list.push(codeItem);
      }
    });
    return list;
  }

  /**
   * 处理非根节点数据
   *
   * @param pValue 父值
   * @param result 返回数组
   * @param codeItem 代码项
   * @memberof AppQuickGroup
   */
  public setChildCodeItems(
    pValue: string,
    result: Array<any>,
    codeItem: any
  ): void {
    result.forEach((item: any) => {
      if (item.pvalue == pValue) {
        let valueField: string = item.value;
        this.setChildCodeItems(valueField, result, item);
        if (!codeItem.children) {
          codeItem.children = [];
        }
        codeItem.children.push(item);
      }
    });
  }

  /**
   * 处理点击事件
   *
   * @param $event 值
   * @param isswitch 是否切换UI选中项
   * @memberof AppQuickGroup
   */
  public handleClick($event: any, isswitch: boolean = true): void {
    if (isswitch) {
      this.selectedUiItem = $event;
    }
178 179 180 181 182
    if ($event.children) {
      if (this.subItems.length > 0) {
        this.subItems.length = 0;
      } else {
        this.subItems.push(...$event.children);
183
      }
184 185
    } else {
      this.subItems.length = 0;
186 187
      this.items.forEach((item:any) => {
        item.selected = false;
188
        item.childSelected = false;
189
        item.selectChildLabel = "";
190 191
      })
      $event.selected = true;
192 193 194 195
      if ($event.pvalue) {
        this.items.forEach((item:any) => {
          if (item.value === $event.pvalue) {
            item.childSelected = true;
196
            item.selectChildLabel = $event.label;
197
          }
198
        })
199
      }
200 201 202
      this.$emit("valuechange", $event);
    }
    this.$forceUpdate();
203
  }
204 205 206 207 208 209 210 211 212 213 214

  /**
   * 关闭遮罩层
   *
   * @type {any[]}
   * @memberof AppQuickGroup
   */
  public closeBackdrop () {
    this.subItems.length = 0;
    this.$forceUpdate();
  }
215 216 217 218 219 220
}
</script>

<style lang="less">
@import "./app-quick-group-tab.less";
</style>