Select.vue 3.16 KB
Newer Older
liuxin committed
1 2
<template>
  <el-select
zhengxiaokui committed
3 4
    :id="id"
    v-model="model"
liushikai committed
5
    v-bind="$attrs"
zhengxiaokui committed
6 7 8 9 10
    v-on="$listeners"
    v-bind:disabled="isDisable"
    :clearable="clearable"
    @click.native="handleClick"
  >
11
    <template v-if="dbCodeList && dbCodeList.length > 0">
12
      <el-option
fukai committed
13
        v-for="item in combinCodes"
14 15 16 17 18 19
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </template>
20
    <template v-else-if="code && code.length > 0">
zhengxiaokui committed
21 22 23 24 25 26 27 28 29
      <el-option
        v-for="item in code"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </template>
    <slot v-else></slot>
liuxin committed
30 31 32 33
  </el-select>
</template>

<script>
潘际乾 committed
34 35
import { getCodetable } from "~/service/business/codeTable"

liuxin committed
36 37 38 39
export default {
  props: {
    value: {
      type: [String, Array, Number],
zhengxiaokui committed
40
      default: undefined,
liuxin committed
41 42 43
    },
    disabled: {
      type: Boolean,
zhengxiaokui committed
44
      default: false,
liuxin committed
45 46 47
    },
    clearable: {
      type: Boolean,
zhengxiaokui committed
48
      default: true,
liuxin committed
49 50 51
    },
    id: {
      type: String,
zhengxiaokui committed
52 53 54 55 56 57 58 59
      default: undefined,
    },
    code: {
      type: Array,
      default: function () {
        return [];
      },
    },
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    dbCode: {
      type: String,
      default: undefined,
    },
  },
  data(){
    return {
      dbCodeList:[],
    }
  },
  watch:{
    //动态从服务器上渲染码表
    dbCode(){
      if(!this.dbCode){
        this.dbCodeList=[]
        //清理数据
        this.model = undefined;
        return 
      }
      getDBCode()
    }
liuxin committed
81 82 83 84
  },
  computed: {
    model: {
      get() {
zhengxiaokui committed
85
        return this.value;
liuxin committed
86 87
      },
      set(newVal) {
zhengxiaokui committed
88 89
        this.$emit("input", newVal);
      },
liuxin committed
90 91
    },
    mode() {
zhengxiaokui committed
92
      return this.$store.state.Status.mode;
liuxin committed
93 94 95
    },
    isDisable: {
      get() {
zhengxiaokui committed
96 97
        return this.mode === "display" || this.disabled;
      },
liuxin committed
98
    },
zhengxiaokui committed
99 100
    highlight() {
      return this.$store.state.Status.highlights.indexOf(this.id) !== -1;
fukai committed
101
    },
zhengxiaokui committed
102 103 104 105 106
    attrs() {
      if (this.mode === "display" || this.disabled) {
        let { placeholder, ...rest } = this.$attrs;
        rest = { placeholder: " ", ...rest };
        return rest;
fukai committed
107
      }
zhengxiaokui committed
108 109
      return this.$attrs;
    },
fukai committed
110 111 112 113 114 115 116 117 118 119 120 121
    combinCodes(){
      //取dbcode,和 setvalues的合集
      if(!this.dbCodeList || this.dbCodeList.length == 0){
        return []
      }
      return this.dbCodeList.filter(item=>{
        if(this.code && this.code.length){
          return this.code.findIndex(item2=>item.value == item2.value)>-1
        }
        return true
      })
    }
zhengxiaokui committed
122
  },
liuxin committed
123 124
  methods: {
    handleClick: function (e) {
zhengxiaokui committed
125 126
      let ev = new Event("click", { bubbles: true });
      let node = e.target;
liuxin committed
127
      if (node.parentElement) {
zhengxiaokui committed
128
        node.parentElement.dispatchEvent(ev);
liuxin committed
129
      }
zhengxiaokui committed
130
    },
131 132 133 134 135
    getDBCode(){
      let args = {tbl:this.dbCode,lang:this.$store.state.I18n.lang.toUpperCase()}
      if(args.lang == "ZH"){
        args.lang = "CN";
      }
潘际乾 committed
136
      getCodetable(args).then(rtnmsg=>{
137 138 139 140 141
        if(rtnmsg.respCode==SUCCESS){
          this.dbCodeList=rtnmsg.data
        }
      })
    }
zhengxiaokui committed
142
  },
143 144 145 146 147
  mounted(){
    if(this.dbCode){
      this.getDBCode()
    }
  }
zhengxiaokui committed
148
};
liuxin committed
149 150 151 152 153 154 155
</script>

<style>
/* .el-select.highlight .el-input .el-input__inner {
  border-color: red;
} */
</style>