Created
November 27, 2018 10:39
-
-
Save GiuMagnani/38521ba7fb0d9780c8666c72b83c6e43 to your computer and use it in GitHub Desktop.
ZiYoungSelectComponent v0.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<el-select :value="value" @input="handleInput($event)" filterable placeholder="Select" :filter-method="filterCustomMethod" | |
default-first-option> | |
<el-option v-for="(item, index) in filteredOptions" :key="item[valueField]" :label="item[labelField]" :value="item[valueField]"> | |
</el-option> | |
</el-select> | |
</template> | |
<script lang="ts"> | |
import Vue from 'vue'; | |
import { Component, Prop } from 'vue-property-decorator'; | |
@Component({}) | |
export default class ZiYoungSelectComponent extends Vue { | |
@Prop({ required: true }) | |
items: any[]; | |
@Prop({}) | |
value: string; | |
@Prop({ | |
default: 'value', | |
}) | |
valueField: string; | |
@Prop({ | |
default: 'label', | |
type: String | |
}) | |
labelField: string; | |
filter = ''; | |
filteredOptions = []; | |
created() { | |
this.filteredOptions = this.items; | |
} | |
// doFilter(filter) { | |
// this.filter = filter; | |
// } | |
handleInput(input) { | |
console.log(input); | |
this.filter = ''; | |
this.$emit('input', input); | |
} | |
filterCustomMethod(query: string) { | |
console.log(query); | |
const re = new RegExp(query, 'i'); | |
const copy = this.items.filter(el => re.test(el.label)); | |
const re1 = new RegExp('^' + query, 'i'); | |
if (query) { | |
copy.sort(function (a, b) { | |
const ares = re1.test(a.label); | |
const bres = re1.test(b.label); | |
if (ares && !bres) return -1; | |
if (!ares && bres) return 1; | |
return 0; | |
}); | |
} | |
console.log(copy); | |
this.filteredOptions = copy; | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment