Select.vue 2.77 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 12 13 14 15 16 17 18 19 20
    <template v-if="dbCodeList.length > 0">
      <el-option
        v-for="item in dbCodeList"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </template>
    <template v-else-if="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>
34
import Api from "~/service/Api"
liuxin committed
35 36 37 38
export default {
  props: {
    value: {
      type: [String, Array, Number],
zhengxiaokui committed
39
      default: undefined,
liuxin committed
40 41 42
    },
    disabled: {
      type: Boolean,
zhengxiaokui committed
43
      default: false,
liuxin committed
44 45 46
    },
    clearable: {
      type: Boolean,
zhengxiaokui committed
47
      default: true,
liuxin committed
48 49 50
    },
    id: {
      type: String,
zhengxiaokui committed
51 52 53 54 55 56 57 58
      default: undefined,
    },
    code: {
      type: Array,
      default: function () {
        return [];
      },
    },
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    dbCode: {
      type: String,
      default: undefined,
    },
  },
  data(){
    return {
      dbCodeList:[],
    }
  },
  watch:{
    //动态从服务器上渲染码表
    dbCode(){
      if(!this.dbCode){
        this.dbCodeList=[]
        //清理数据
        this.model = undefined;
        return 
      }
      getDBCode()
    }
liuxin committed
80 81 82 83
  },
  computed: {
    model: {
      get() {
zhengxiaokui committed
84
        return this.value;
liuxin committed
85 86
      },
      set(newVal) {
zhengxiaokui committed
87 88
        this.$emit("input", newVal);
      },
liuxin committed
89 90
    },
    mode() {
zhengxiaokui committed
91
      return this.$store.state.Status.mode;
liuxin committed
92 93 94
    },
    isDisable: {
      get() {
zhengxiaokui committed
95 96
        return this.mode === "display" || this.disabled;
      },
liuxin committed
97
    },
zhengxiaokui committed
98 99
    highlight() {
      return this.$store.state.Status.highlights.indexOf(this.id) !== -1;
fukai committed
100
    },
zhengxiaokui committed
101 102 103 104 105
    attrs() {
      if (this.mode === "display" || this.disabled) {
        let { placeholder, ...rest } = this.$attrs;
        rest = { placeholder: " ", ...rest };
        return rest;
fukai committed
106
      }
zhengxiaokui committed
107 108 109
      return this.$attrs;
    },
  },
liuxin committed
110 111
  methods: {
    handleClick: function (e) {
zhengxiaokui committed
112 113
      let ev = new Event("click", { bubbles: true });
      let node = e.target;
liuxin committed
114
      if (node.parentElement) {
zhengxiaokui committed
115
        node.parentElement.dispatchEvent(ev);
liuxin committed
116
      }
zhengxiaokui committed
117
    },
118 119 120 121 122
    getDBCode(){
      let args = {tbl:this.dbCode,lang:this.$store.state.I18n.lang.toUpperCase()}
      if(args.lang == "ZH"){
        args.lang = "CN";
      }
fukai committed
123
      Api.post("getCodetable",args).then(rtnmsg=>{
124 125 126 127 128
        if(rtnmsg.respCode==SUCCESS){
          this.dbCodeList=rtnmsg.data
        }
      })
    }
zhengxiaokui committed
129
  },
130 131 132 133 134
  mounted(){
    if(this.dbCode){
      this.getDBCode()
    }
  }
zhengxiaokui committed
135
};
liuxin committed
136 137 138 139 140 141 142
</script>

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