Last active
August 31, 2018 05:54
-
-
Save moreta/7a0be8846151fdd6860c262ea0798ab1 to your computer and use it in GitHub Desktop.
vueでの定数とselect optionのためのselectOptionPlugin
This file contains hidden or 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
| /** | |
| * 性別 - 定数 | |
| */ | |
| export const SEX_TYPES = { | |
| MALE: '1', | |
| FEMALE: '2' | |
| } |
This file contains hidden or 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> | |
| <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> |
This file contains hidden or 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
| // vue i18n | |
| export default { | |
| ja: { | |
| model: { | |
| user: { | |
| sex: { | |
| '1': '男性', | |
| '2': '女性' | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| // 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