app-multiple-select.vue 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
<template>
  <div class="app-multiple-select">
    <van-checkbox-group class="app-multiple-select__group" v-model="curValue">
      <van-checkbox
        shape="square"
        class="app-multiple-select__checkbox"
        :name="item.value"
        v-for="item in options"
        :key="item.id"
      >{{ item.text }}</van-checkbox>
    </van-checkbox-group>
  </div>
</template>

<script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";
import { CodeListService, LogUtil } from "ibiz-core";

@Component({
  components: {}
})
export default class AppMultipleSelect extends Vue {

  /**
   * 编辑器名称
   *
   * @type {string}
   * @memberof AppMultipleSelect
   */
  @Prop() public name?: string;
  
  /**
   * 代码表服务对象
   *
   * @type {CodeListService}
   * @memberof AppMultipleSelect
   */

  public codeListService: CodeListService = new CodeListService();

  /**
   * 代码表标识
   *
   * @type {string}
   * @memberof AppMultipleSelect
   */
  @Prop() public tag!: string;

  /**
   * 代码表类型
   *
   * @type {string}
   * @memberof AppMultipleSelect
   */
  @Prop() public codelistType!: string;

  /**
   * 应用上下文
   *
   * @type {*}
   * @memberof AppMultipleSelect
   */
  @Prop({ default: {} }) protected context?: any;

  /**
   * 代码表项集合
   *
   * @type {Array<any>}
   * @memberof AppMultipleSelect
   */
  public options: Array<any> = [];

  /**
   * 当前选中值
   *
   * @type {string[]}
   * @memberof AppMultipleSelect
   */
  @Prop() public value?: any;

  /**
   * 模式的类型
   *
   * @type {string}
   * @memberof AppMultipleSelect
   */
  @Prop({ default: "str" }) public orMode?: string;

  /**
   * 数据存储分隔符
   *
   * @type {string}
   * @memberof AppMultipleSelect
   */
  @Prop({ default: "," }) public valueSeparator?: string;

  /**
   * 数据显示分隔符
   *
   * @type {string}
   * @memberof AppMultipleSelect
   */
  @Prop({ default: "," }) public textSeparator?: string;

  /**
   * 代码表
   *
   * @type {string}
   * @memberof DropDownList
   */    
  @Prop() public codeList?: any;

  get curValue() {
    if (this.value) {
      this.selectedValues = this.value;
      if (this.orMode === "num") {
        const temp: Array<any> = [];
        this.options.forEach((val: any) => {
          if ((this.value & val.value) == val.value) {
            temp.push(val.value);
          }
        });
        return temp;
      } else {
        return this.value.split(this.valueSeparator);
      }
    } else {
      return [];
    }
  }

  set curValue(val: any) {
    this.getSelectedValues(val);
    this.$emit("change", {name:this.name, value:this.selectedValues, event:{}});
  }

  /**
   * 选中数据值
   *
   * @private
   * @type any
   * @memberof AppMultipleSelect
   */
  public selectedValues: any;

  /**
   * 获取选择的实际值和文本值
   *
   * @param {any[]} arr
   * @memberof AppMultipleSelect
   */
  public getSelectedValues(arr: any[]) {
    let num = 0;
    let str = "";
    arr.forEach(val => {
      const element = this.options.find((item: any) =>
        Object.is(item.value, val)
      );
      if (element) {
        if (this.orMode === "num") {
          num = num | parseInt(val, 10);
        } else {
          if (str) {
            str += this.valueSeparator;
          }
          str += element.value;
        }
      }
    });
    this.selectedValues =
      this.orMode === "num" ? (num !== 0 ? num.toString() : "") : str;
  }

  public created() {
    if (this.tag && this.codelistType) {
      if (Object.is(this.codelistType, "DYNAMIC")) {
        this.codeListService
          .getItems(this.tag)
          .then((res: any) => {
            this.options = res;
          })
          .catch((error: any) => {
            this.options = [];
          });
      } else {
        this.codeListService.getDataItems({ tag: this.tag, type: 'STATIC', data: this.codeList, context:this.context, viewparam:null }).then((codelistItems: Array<any>) => {
            this.options = codelistItems;
        }).catch((error: any) => {
            LogUtil.log(`----${this.tag}----${this.$t('app.commonWords.codeNotExist')}`);
        })        
      }
    }
  }
}
</script>