app-department-select.vue 5.3 KB
Newer Older
1 2
<template>
  <div class="app-department-select">
3
    <ibiz-select-tree  :NodesData="Nodesdata" v-model="selectTreeValue" :multiple="multiple" @select="onSelect"></ibiz-select-tree>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  </div>
</template>

<script lang="ts">
import { Vue, Component, Watch, Prop, Model } from 'vue-property-decorator';

@Component({
})
export default class AppDepartmentSelect extends Vue {

    /**
     * 接口url
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
tony001's avatar
tony001 committed
20
    @Prop() public url?: any;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

    /**
     * 过滤项
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
    @Prop() public filter?: any;

    /**
     * 过滤项
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
    @Prop() public fillMap?: any;

    /**
     * 是否多选
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
    @Prop({default:false}) public multiple?: any;

    /**
     * 表单数据
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
    @Prop() public data!: any;

    /**
55
     * 上下文变量
56 57 58 59
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
60
    @Prop() public context!: any;
61 62

    /**
63
     * 选中数值
64 65 66 67
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
68
    public selectTreeValue:any = "";
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    /**
     * 树节点数据
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
    public Nodesdata: any[] = [];

    /**
     * 当前树节点数据的url
     *
     * @type {*}
     * @memberof AppDepartmentSelect
     */
    public oldurl: any[] = [];

    /**
     * 获取节点数据
     *
     * @memberof AppDepartmentSelect
     */
91
    public handleFilter(){
92 93
      if(this.filter){
          if(this.data && this.data[this.filter]){
94
            return this.data[this.filter];
95
          }else if(this.context && this.context[this.filter]){
96
            return this.context[this.filter];
97 98
          }
      }else{
99
          return this.context.srforgid;
100
      }
101 102 103 104 105 106 107 108 109 110
    }

    /**
     * 获取节点数据
     *
     * @memberof AppDepartmentSelect
     */
    public searchNodesData(){
      // 处理过滤参数,生成url
      let param = this.handleFilter();
tony001's avatar
tony001 committed
111
      let _url = this.url.replace('${orgid}',param)
112 113 114
      if(this.oldurl === _url){
          return;
      }
115 116 117 118 119 120 121
      this.oldurl = _url;
      // 缓存机制
      const result:any = this.$store.getters.getCopyData(_url);
      if(result){
        this.Nodesdata = result;
        return;
      }
122 123
      this.$http.get(_url).then((response: any) => {
          this.Nodesdata = response.data;
124
          this.$store.commit('addDepData', { srfkey: this.filter, orgData: response.data });
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
      }).catch((response: any) => {
          if (!response || !response.status || !response.data) {
              this.$Notice.error({ title: '错误', desc: '系统异常!' });
              return;
          }
      });
    }

    /**
     * 值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppDepartmentSelect
     */
140
    @Watch('data',{immediate:true,deep:true})
141
    public onValueChange(newVal: any, oldVal: any) {
142 143 144 145 146 147
        if(newVal){
          this.computedSelectedData();
          this.$nextTick(()=>{
            this.searchNodesData();
          });
        }
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 178 179 180 181 182 183 184 185 186 187 188
    /**
     * 计算选中值
     * 
     * @memberof AppOrgSelect
     */
    public computedSelectedData(){
      // 单选
      if(!this.multiple){
        if(this.fillMap && Object.keys(this.fillMap).length >0){
        let templateValue = {};
        Object.keys(this.fillMap).forEach((item:any) =>{
          if(this.data && this.data[this.fillMap[item]]){
            Object.assign(templateValue,{[item]:this.data[this.fillMap[item]]});
          }
        })
        this.selectTreeValue = JSON.stringify([templateValue]);
        }
      }else{
      // 多选
        if(this.fillMap && Object.keys(this.fillMap).length >0){
          let tempArray:Array<any> = [];
          Object.keys(this.fillMap).forEach((item:any) =>{
            if(this.data && this.data[this.fillMap[item]]){
              let tempDataArray:Array<any> = (this.data[this.fillMap[item]]).split(",");
              tempDataArray.forEach((tempData:any,index:number) =>{
                if(tempArray.length < tempDataArray.length){
                  let singleData:any ={[item]:tempData};
                  tempArray.push(singleData);
                }else{
                  Object.assign(tempArray[index],{[item]:tempData});
                }
              })
            }
          })
          this.selectTreeValue = JSON.stringify(tempArray);
          }
      }
    } 

189 190 191 192 193 194 195 196 197 198 199 200 201
    /**
     * select事件处理
     * 
     * @param {*} $event
     * @memberof AppDepartmentSelect
     */
    public onSelect($event:any){
        // 组件自身抛值事件
        let selectArr = JSON.parse($event);
        // fillMap抛值事件
        if(this.fillMap && Object.keys(this.fillMap).length > 0){
            Object.keys(this.fillMap).forEach((attribute:string) => {
                let _name = this.fillMap[attribute];
WodahsOrez's avatar
WodahsOrez committed
202 203 204
                let values = selectArr.map((item:any) => item[attribute]);
                let _value = $event === "[]" ? null : values.join(",");
                this.$emit('select-change',{name: this.fillMap[attribute], value: _value})
205 206 207 208 209 210 211 212 213
            });
        }
    }
}
</script>

<style lang='less'>
@import './app-department-select.less';
</style>