简体   繁体   English

修复 vue js 复选框过滤器

[英]Fix vue js checkbox filter

I'm starting with Vue and I was reading some questions like this one to filter with computed() .我从 Vue 开始,我正在阅读一些像这样的问题来过滤computed()

I think I'm on the right path to solve my problem but I could not make it work yet.我认为我正走在解决问题的正确道路上,但我还不能让它发挥作用。

I have a search field in my webpage to search for users according to their city.我的网页中有一个搜索字段,可以根据用户所在的城市搜索用户。 Each user have an array of filters set to true or false .每个用户都有一组过滤器设置为truefalse

The listing process works fine.上市过程工作正常。 Now I want to click on filter buttons and keep in the list only users that have the clicked filter in their filters array.现在我想单击过滤器按钮并仅将在其过滤器数组中具有单击过滤器的用户保留在列表中。

html: html:

<div class="filters-div">
    <label class="btn btn-circle border filters-btn">
        <input type="checkbox" name="vehicle" id="vehicle" value="vehicle" v-model="checkedFilters" />
        <i></i>
        <span>Vehicle</span>
    </label>
    <label class="btn btn-circle border filters-btn">
        <input type="checkbox" name="host" id="host" value="host" v-model="checkedFilters" />
        <i></i>
        <span>Hosting</span>
    </label>
    <label class="btn btn-circle border filters-btn">
        <input type="checkbox" name="transfer" id="transfer" value="transfer" v-model="checkedFilters" />
        <i></i>
        <span>Transfer</span>
    </label>
    <label class="btn btn-circle border filters-btn">
        <input type="checkbox" name="activities" id="activities" value="activities" v-model="checkedFilters" />
        <i></i>
        <span>Night Activities</span>
    </label>
    <label class="btn btn-circle border filters-btn">
        <input type="checkbox" name="adventures" id="adventures" value="adventures" v-model="checkedFilters" />
        <i></i>
        <span>Adventures</span>
    </label>
    <label class="btn btn-circle border filters-btn">
        <input type="checkbox" name="tour" id="tour" value="tour" v-model="checkedFilters" />
        <i></i>
        <span>Tours</span>
    </label>
</div>

<div v-for="(item, index) in filterHoster" v-bind:key="item.id" class="col-md-6 mb-50">
    <div class="result-container">
        <div class="result-content2">
            <div class="result-profile-pic-div2">
                <div class="result-profile-pic2 img-fluid" v-bind:style="{'background-image': 'url(http://localhost/users/'+item.profileIMG + ')' }"></div>
            </div>
            <div class="result-profile-infos-div2">
                <div class="result-name">
                    <h4 class="mb-0">{{ item.name }} {{ item.lastname }}</h4>
                </div>
                <div class="result-rating">
                    <div class="rate-area">
                        <star-rating @rating-selected="setRating(item.id, item.rating)" :rating="item.rating"></star-rating>
                    </div>
                    <div>
                        <p>{{ item.city }} - {{ item.state }}</p>
                    </div>
                </div>
                <hr />
                <div class="tour-options">
                    <div class="tour-options-select">
                        <select :id="'select-suggestions' + item.id" name="tour-options-dropdown" v-model="selected[item.id]" class="tour-options-dropdown" @change="showTour = selected, setTour($event)">
                            <option :value="null">Tour suggestions</option>
                            <option v-for="(tour, key) in item.tours" :key="key" :value="tour.tourID">
                                {{ tour.title }}
                            </option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

vue视图

let app = new Vue({
    el: "#app",
    data: {
        hosters: {},
        filters: {
            vehicle: "true",
            host: "true",
            transfer: "true",
            activities: "true",
            adventures: "true",
            tour: "true",
        },
        checkedFilters: [],
    },
    computed: {
        filterHoster() {
            if (!this.checkedFilters.length) return this.hosters;

            // return this.hosters.filter(f => this.checkedFilters.includes(f.filter));

            return this.hosters.filter((f) => {
                f.filters.filter((j) => {
                    this.checkedFilters.includes(j);
                });
            });
        },
    },
});

This is my database response:这是我的数据库响应:

在此处输入图片说明

if you convert hosters to array you are good to go in my case.如果您将主机转换为数组,那么就我而言,您很高兴。 Because filter will not work in objects.因为过滤器在对象中不起作用。

hosters: [],

You can check it here: https://codepen.io/hasip-timurtas/pen/QWNoNjE你可以在这里查看: https : //codepen.io/hasip-timurtas/pen/QWNoNjE

I had to make some changes in my filters response:我必须对过滤器响应进行一些更改:

filters: ["vehicle", "host", "transfer", "activities", "adventures", "tour"];

and filters in data() are not being used anymore.不再使用data()中的过滤器。

The final computed() is:最终的computed()是:

computed: {
    filterHoster() {
        if (!this.checkedFilters.length) return this.hosters;

        return this.hosters.filter(hoster => {
            return this.checkedFilters.some(
                filter => hoster.filters.includes(filter)
            );
        });
    }
},

This way I can check if every checked filter is included in user filters.这样我就可以检查每个选中的过滤器是否都包含在用户过滤器中。

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

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