简体   繁体   English

为什么复选框过滤在VueJS中不起作用?

[英]Why is the checkbox filtering not working in VueJS?

Been trying for 8 hours now to get this to work and still no luck. 现在已经尝试了8个小时才能让它工作,但仍然没有运气。 Ultimately, what I am trying to do is print a loop which shows all items by default. 最终,我要做的是打印一个循环,默认显示所有项目。 Then when a user selects a checkbox it will filter out those items where they are set to false. 然后,当用户选择一个复选框时,它将过滤掉那些设置为false的项目。

I have the following group of checkboxes and a loop: 我有以下一组复选框和一个循环:

  <div class="container" id="clubs">
    <div class="filter">
        <label><input type="checkbox" v-model="selectedCategory" value="All" /> All</label>
        <label><input type="checkbox" v-model="selectedCategory" value="ClubParking" /> Parking</label>
        <label><input type="checkbox" v-model="selectedCategory" value="ClubToilets" /> Toilets</label>
        <label><input type="checkbox" v-model="selectedCategory" value="ClubFloodlights" /> Floodlights</label>
    </div>

    <ul class="clubs-list">
        <li v-for="club in filteredClubs">{{ club.clubName }}</li>
    </ul>
</div>

Then in my application I have the following code 然后在我的应用程序中,我有以下代码

var vm = new Vue({
    el:  "#clubs",
    data: {
        clubs: [
            { clubName: "Club One", clubParking: true, clubToilets: false, clubFloodlights: true },
            { clubName: "Club Two", clubParking: true, clubToilets: false, clubFloodlights: false },
            { clubName: "Club Three", clubParking: false, clubToilets: true, clubFloodlights: true },
        ],
        selectedCategory: "All"
    },
    computed: {
        filteredClubs: function() {
            var vm = this;
            var category = vm.selectedCategory;

            if(category === "All") {
                return vm.clubs;
            } else {

         return vm.clubs.filter(function(club) {
            return selectedFieldNames.every(fname=>club[fname])
             const selectedFieldNames =  selectedCategory.map(category=>{
                  switch(category){
                     case 'Toilets':
                      return 'clubToilets';
                     case 'Parking':
                      return 'clubParking';
                     case 'Floodlights':
                      return 'clubFloodlights';
                  }
            })

        });

            }
        }
    }
});

I have even made a Codepen I am better with a working modal then I can backtrack to see why I have been going wrong. 我甚至做了一个Codepen我用一个工作模式做得更好然后我可以回溯看看为什么我出错了。 Any help welcome as I have'nt left the house all day trying to get it to work. 任何帮助欢迎,因为我没有整天离开房子试图让它工作。

Edited your codepen as discussed on comments above. 编辑您的codepen,如上面的评论所述。 1. moved from "string" selectedCategory to "array" 2. changed filtering to work for array 1.从“string”selectedCategory移动到“array”2.更改过滤以适用于数组

 var vm = new Vue({ el: "#clubs", data: { clubs: [ { clubName: "Club One", clubParking: true, clubToilets: false, clubFloodlights: true }, { clubName: "Club Two", clubParking: true, clubToilets: false, clubFloodlights: false }, { clubName: "Club Three", clubParking: false, clubToilets: true, clubFloodlights: true }, ], selectedCategories: ["All"] }, computed: { filteredClubs: function() { if (this.selectedCategories.includes('All')) { return this.clubs; } else { return this.clubs.filter(club => { return this.selectedCategories.some(category => club[category]) }) } } } }); 
 .container { padding: 20px; width: 90%; max-width: 400px; margin: 0 auto; } label { display: block; line-height: 1.5em; } ul { margin-left: 0; padding-left: 0; list-style: none; } li { padding: 8px 16px; border-bottom: 1px solid #eee; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div class="container" id="clubs"> <div class="filter"> <label><input type="checkbox" v-model="selectedCategories" value="All" /> All</label> <label><input type="checkbox" v-model="selectedCategories" value="clubParking" /> Parking</label> <label><input type="checkbox" v-model="selectedCategories" value="clubToilets" /> Toilets</label> <label><input type="checkbox" v-model="selectedCategories" value="clubFloodlights" /> Floodlights</label> </div> <ul class="clubs-list"> <li v-for="club in filteredClubs">{{ club.clubName }}</li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM