Skip to content

Instantly share code, notes, and snippets.

@moreta
Last active August 31, 2018 05:54
Show Gist options
  • Select an option

  • Save moreta/7a0be8846151fdd6860c262ea0798ab1 to your computer and use it in GitHub Desktop.

Select an option

Save moreta/7a0be8846151fdd6860c262ea0798ab1 to your computer and use it in GitHub Desktop.
vueでの定数とselect optionのためのselectOptionPlugin
/**
* 性別 - 定数
*/
export const SEX_TYPES = {
MALE: '1',
FEMALE: '2'
}
<template>
<model-select :options="SEX_OPTIONS"
v-model="query.sex"
placeholder="select item">
</model-select>
</template>
<script>
import { SEX_TYPES } from '@/config/constants'
export default {
data () {
return {
// SEX_OPTIONS: this.$toOptions(SEX_TYPES, (key) => this.$t(`model.user.sex.${key}`), true), // 検索用
SEX_OPTIONS: this.$toOptions(SEX_TYPES, (key) => this.$t(`model.user.sex.${key}`)), // 入力用
}
}
}
</script>
// vue i18n
export default {
ja: {
model: {
user: {
sex: {
'1': '男性',
'2': '女性'
}
}
}
}
}
// plugin登録コードは省略
const VueSelectOption = {
install (Vue, options) {
Vue.prototype.$toOptions = (constant, i18nHandler, includeBlank) => {
options = []
if (i18nHandler) {
options = Object.entries(constant).map(entry => {
return { value: entry[1], text: i18nHandler(entry[1]) }
})
} else {
options = Object.entries(constant).map(entry => {
return { value: entry[1], text: entry[0] }
})
}
if (includeBlank) {
return [{ value: '', text: '選択無し' }].concat(options)
} else {
return options
}
}
}
}
export default VueSelectOption
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment