0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.jsのselectでプリミティブな値ではなくオブジェクトを返すサンプル。

Last updated at Posted at 2019-01-19

Vue.jsのselectでプリミティブな値ではなくオブジェクトを返すサンプルです。

selectのオプションに相当する変数をvalueとtextの辞書配列に合わせると、素のvueだけではなくbootstrap-vueなどにも応用が簡単です。

SelectSample.vue
<template>
  <div>
    <select v-model="country" @change="change_country">
      <option v-for="(c , i) in country_list" :key="i" :value="c.value">{{ c.text }}</option>
    </select>
    <center v-if="country">
      <table>
        <thead>
          <th>key</th>
          <th>value</th>
        </thead>
        <tbody>
          <tr v-for="(k , i) in Object.keys(country)" :key="i">
            <td>{{ k }}</td>
            <td>{{ country[k] }}</td>
          </tr>
        </tbody>
      </table>
    </center>
  </div>
</template>

<script>
export default {
  name: "SelectSample",
  data: function() {
    return {
      country: null
    };
  },
  computed: {
    country_list: () => {
      return [
        { value: null, text: "国を選択してください。" },
        {
          value: {
            name: "中華人民共和国",
            alpha: "CHN",
            population: 1349335152
          },
          text: "中華人民共和国"
        },
        {
          value: { name: "インド", alpha: "IND", population: 1224514327 },
          text: "インド"
        },
        {
          value: {
            name: "アメリカ合衆国",
            alpha: "USA",
            population: 310383948
          },
          text: "アメリカ合衆国"
        }
      ];
    }
  },
  watch: {
    /*
    country: selected_country => {
      console.log(JSON.stringify(selected_country));
    }
    */
  },
  methods: {
    change_country: function() {
      console.log(JSON.stringify(this.country));
    }
  }
};
</script>

<style scoped>
table {
  margin: 0 auto;
  border-collapse: collapse;
}
table th,
table td {
  padding: 0.2rem 2rem;
  border: solid 1px #333;
}
</style>

Codepenの動作サンプル
(※Codepenってpenじゃなくprojectは埋め込めないのかな??)

https://github.com/qoAopx/vue-select-sample

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?