Vueでチェックボックスで絞り込みができる機能を実装しました。
さらに「全てチェックボタン」をつけたのですが、上記の機能とうまく連動しません。
尚、絞り込みを実装しない段階で「全てチェックボタン」のみ動かした際はうまく動作をしておりました。
2つの機能の連動に際し、もし不具合になりそうな箇所がありましたら教えて頂きたいです。
<div id="app"> <table class="table"> <thead class="thead-dark"> <tr> <th> <input type="checkbox" v-on:click="clickAllChecked($event)" v-model="AllChecked" value="1"> </th> <th>全てチェック</th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" class="facilitychk" value="1" v-on:click="clickChecked($event)" v-model="show1"> </td> <td>東京</td> </tr> <tr> <td> <input type="checkbox" class="facilitychk" value="1" v-on:click="clickChecked($event)" v-model="show2"> </td> <td>大阪</td> </tr> <tr> <td> <input type="checkbox" class="facilitychk" value="1" v-on:click="clickChecked($event)" v-model="show3"> </td> <td>名古屋</td> </tr> <p>{{this.filteredList.length}}件<p> </div> </tbody> </table> </div> <script src="https://unpkg.com/vue@2.5.17"></script> <script> var app = new Vue({ el: '#app', data: { dataList : [ { id: 121, city: "Osaka" }, { id: 123, city: "Fukuoka" }, { id: 425, city: "Osaka" }, { id: 645, city: "Tokyo" }, { id: 326, city: "Osaka" }, { id: 876, city: "Tokyo" }, { id: 456, city: "Osaka" }, { id: 335, city: "Osaka" }, { id: 471, city: "Tokyo" }, { id: 345, city: "Osaka" }, { id: 857, city: "Fukuoka" }, { id: 455, city: "Osaka" }, { id: 997, city: "Osaka" }, { id: 121, city: "Tokyo" }, { id: 115, city: "Osaka" }, { id: 668, city: "Fukuoka" } ], AllChecked: 0, show1: false, show2: false, show3: false, }, methods: { // 全てチェック機能 clickAllChecked: function (event) { var facilitychk = document.getElementsByClassName('facilitychk'); var actionFlg; if (event.target.checked == true) { actionFlg = true; } else { actionFlg = false; } for (var i = 0; facilitychk.length > i; i++) { facilitychk[i].checked = actionFlg; } }, clickChecked: function(event) { if(event.target.checked == false){ this.AllChecked = 0; } } }, computed: { filteredList: function() { // 絞り込み機能 var newList = []; for (var i=0; i<this.dataList.length; i++) { var isShow = true; if (!this.show1 && this.dataList[i].city == "Tokyo") { isShow = false; } if (!this.show2 && this.dataList[i].city == "Osaka") { isShow = false; } if (!this.show3 && this.dataList[i].city == "Fukuoka") { isShow = false; } if (isShow) { newList.push(this.dataList[i]); } } return newList; } } }); </script>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/12 14:58
2020/03/12 15:10
2020/03/12 21:19 編集
2020/03/13 01:13