チェックボックスに変更があっても、キャンセルボタンを押されたら変更される前の状態に戻す。
というスクリプトを書いています。
しかし、保存ボタンがうまく機能しません。
現在、保存ボタンをクリック+チェックボックスの値に変更があった際に
保存メソッドsave()
が発火する状態になっています。
これを保存ボタンがクリックされたら発火する状態にするためにはどうしたら良いでしょうか?
save()
が保存するメソッド
goBackPre
が保存された状態に戻すメソッド
lang
1<!DOCTYPE> 2<html> 3<head> 4 <script src="https://unpkg.com/vue"></script> 5</head> 6<body> 7 <div class="card"> 8 <table v-if='checkedUser.length > 0'> 9 <thead> 10 <tr> 11 <th scope="col" v-for='name in columns' >{{name}}</th> 12 <th scope="col" ></th> 13 </tr> 14 </thead> 15 <tbody> 16 <tr v-for='(item,index) in checkedUser' :key='item'> 17 <td v-for='user in users' :key='user.id' v-if='user.id == item'>{{ user.name }}</td> 18 <td @click='removeCheck(index)'> 19 <div class="text-danger" style="cursor: pointer;">削除</div> 20 </td> 21 </tr> 22 </tbody> 23 </table> 24 <div> 25 <div>ユーザ追加</div> 26 <input type="search" placeholder="Search..." v-model="searchWord"> 27 <div v-for="user in filteredusers" :key='user.id'> 28 <label > 29 <input type="checkbox":value = "user.id" v-model="checkedUser" name="group[user_ids][]"> 30 <span >{{user.name}}</span> 31 </label> 32 </div> 33 <button @click="goBackPre()">キャンセル</button> 34 <button @click="save()">保存</button> 35 </div> 36 </div> 37</body> 38</html>
lang
1 <script> 2 var app = new Vue({ 3 el: '.card', 4 data: { 5 columns: ['名前'], 6 checkedUser:[], 7 users: [ 8 {"id":51,"name":"村人A"}, 9 {"id":52,"name":"村人B"}, 10 {"id":53,"name":"村人C"} 11 ], 12 searchWord: '', 13 current_user:[] 14 }, 15 methods: { 16 removeCheck(index){ 17 this.checkedUser.splice(index, 1); 18 }, 19 save(){ 20 <!-- 保存する --> 21 this.current_user = this.checkedUser 22 }, 23 goBackPre(){ 24 <!-- 保存された状態に戻す --> 25 this.checkedUser.splice(0); 26 this.checkedUser = this.current_user; 27 } 28 29 }, 30 computed:{ 31 filteredusers: function(){ 32 return this.users.filter(function(user){ 33 return user.name.indexOf(this.searchWord) > -1 34 },this) 35 }, 36 }, 37 }); 38 </script>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/11/16 12:56
退会済みユーザー
2017/11/16 14:26