前提・実現したいこと
Vue.jsを使ってツールの制作を行っています。
https://codepen.io/ebifly/pen/RywWvg
上記のコードペンを見ていただけると分かりやすいと思うのですが
1.「名前を入力」→「追加」で入力した名前がセレクトボックスに追加される
2.リスト化されたものがセレクトボックスにも追加される
3.セレクトボックスで選ばれたものの「Status」が変更される
4.3と同時にセレクトボックスが消えて、選ばれたオブジェクトの内容が「選択されたのは[名前]Status:[ステータス]img:[イメージ番号]」の様に表示される
5.backボタンでセレクトボックスが現れてStatusが「a」に変更される
という動きを実装したいのですが、4,5の動きが上手くいかずアドバイスを頂きたいです。
発生している問題・エラーメッセージ
発生している問題としてはオブジェクトの内容が上手く表示出来ないというのと、backボタンでStatusの変更が行えないということです。
該当のソースコード
js
1Vue.component('name-list',{ 2 template:'\ 3 <div>\ 4 <select v-if="!show" name="name-list" v-model="selected" @change="changeStatus">\ 5 <option value="">該当なし</option>\ 6 <option\ 7 v-for="(name, index) in names"\ 8 :value="name.id"\ 9 >{{ name.title }}\ 10 </option>\ 11 </select>\ 12 <p v-if="show">選択されたのは{{ names.title }}status:{{ names.status }} img:{{ names.img }}<button @click="changeBack">back</button></p>\ 13</div>\ 14 ', 15 props: ['names'], 16 data: function data() { 17 return { 18 selected: '', 19 show: false, 20 }; 21 }, 22 methods: 23 { 24 changeStatus: function changeStatus(e){ 25 this.$emit('change-status', e.target.value); 26 this.show = true; 27 }, 28 changeBack: function changeBack(e){ 29 this.$emit('change-back', e.target.value); 30 this.show = false; 31 } 32 } 33 }); 34 35 var vm = new Vue({ 36 el: '#main', 37 data: { 38 newNameText: '', 39 nextImg: 1, 40 nextId: 0, 41 names: [] 42 }, 43 methods: { 44 addNewName: function () { 45 if (this.newNameText !== '') { 46 if (this.nextImg > 10) { 47 this.nextImg = 1 48 } 49 this.names.push({ 50 img: this.nextImg++, 51 id: this.nextId++, 52 title: this.newNameText, 53 status: 'a' 54 }) 55 } 56 this.newNameText = '' 57 }, 58 updateName: function updateName(id, name){ 59 this.names[id].title = name 60 }, 61 changeB: function changeB(id){ 62 this.names[id].status = 'b' 63 }, 64 changeC: function changeC(id){ 65 this.names[id].status = 'c' 66 }, 67 changeBack: function changeBack(id){ 68 this.names[id].status = 'a' 69 } 70 } 71 }) 72 73vm.$mount('#main');
試したこと
html
1<div> 2 changeB 3 <name-list 4 :names="names" 5 :title="names.title" 6 :img="names.img" 7 :status="names.status" 8 @change-status="changeB" 9 @change-back="changeBack" 10 ></name-list> 11</div>
js
1 Vue.component('name-list',{ 2 template:'\ 3 <div>\ 4 <select v-if="!show" name="name-list" v-model="selected" @change="changeStatus">\ 5 <option value="">該当なし</option>\ 6 <option\ 7 v-for="(name, index) in names"\ 8 :value="name.id"\ 9 >{{ name.title }}\ 10 </option>\ 11 </select>\ 12 <p v-if="show">選択されたのは{{ title }}status:{{ status }} img:{{ img }}<button @click="changeBack">back</button></p>\ 13</div>\ 14 ', 15 props: ['names', 'title', 'status', 'img']
上記の様にprops周りを書き換えても上手くいきませんでした_(:3 」∠)_
ご教授のほどよろしくお願いしますm(_ _)m
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/24 09:17