app-van-select.vue 3.0 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
<template>
  <van-dropdown-menu class="app-van-select">
    <van-dropdown-item get-container="#app" :lazy-render="false" :title="showTitle" ref="item" >
        <div class="app-van-select__container">
        <div v-for="(item,index) in items" :key="index" class="app-van-select__item" @click="itemClick(item)">
            <div v-show="selectItem == item" class="app-van-select__icon"><van-icon name="success" /></div>
            <div  class="app-van-select__text">{{item.label}}</div>
        </div>
        </div>
      <van-button  class="app-van-select__btn" @click="onReset" >{{$t('reset')}}</van-button>
      <van-button  class="app-van-select__btn" type="info" @click="onConfirm">{{$t('confirm')}}</van-button>
    </van-dropdown-item>
  </van-dropdown-menu>
</template>

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

@Component({
    components: {},
    i18n: {
        messages: {
            'ZH-CN': {
                confirm: '确定',
                reset: '重置',
            },
            'EN-US': {
                confirm: 'Confirm',
                reset: 'Reset',
            }
        }
    }
})
export default class AppVanSelect extends Vue {

    /**
      * 数据集
      *
      * @type {any}
      * @memberof AppVanSelect
      */
    @Prop() public items?: any;

    /**
     * 搜索名
     *
     * @type {any}
     * @memberof AppVanSelect
     */
    @Prop() public name!: string;

    /**
     * 名称
     *
     * @type {any}
     * @memberof AppVanSelect
     */
    @Prop() public title?: any;

    /**
     * 展示信息
     *
     * @type {any}
     * @memberof AppVanSelect
     */
    public showTitle: any = "";

    /**
     * 当前选中数据
     *
     * @type {any}
     * @memberof AppVanSelect
     */
    public selectItem: any = {};

    /**
     * 重置按钮
     *
     * @type {string}
     * @memberof AppVanSelect
     */
    public onReset() {
        this.selectItem = {};
        this.showTitle = this.title;
        this.closeDropdown();
        this.$emit('onConfirm', {tag:'oncategory',value: { [this.name]: this.selectItem.value }})
    }

    /**
     * 数据点击事件
     *
     * @type {any}
     * @memberof AppVanSelect
     */
    public itemClick(item: any) {
        this.selectItem = item;
    }



    /**
     * 收起数据
     *
     * @type {any}
     * @memberof AppVanSelect
     */
    public closeDropdown() {
        let dropdown: any = this.$refs.item;
        if (dropdown.toggle && dropdown.toggle instanceof Function) {
            dropdown.toggle();
        }

    }

    /**
     * 提交事件
     *
     * @type {any}
     * @memberof AppVanSelect
     */
    public onConfirm() {
        if (this.selectItem.label) {
            this.showTitle = this.selectItem.label;
        }
        this.closeDropdown();
        this.$emit('onConfirm',{tag:'oncategory',value: { [this.name]: this.selectItem.value }})
    }

    /**
     * 生命周期
     *
     * @memberof AppVanSelect
     */
    created() {
        this.showTitle = this.title;
    }

}
</script>