【问题标题】:How to filter results on checkboxes (array) in Vue如何过滤 Vue 中复选框(数组)的结果
【发布时间】:2023-01-03 01:50:44
【问题描述】:

对于在 Vue 2 中分配了多个标签/复选框的新闻文章,我无法显示结果。如果我选​​中单个标签/复选框,我的结果会正确显示。我可能在这里做错了什么?

var tagData = [
    {
        id: 1,
        title: "Internal"
    },
    {
        id: 2,
        title: "Industry"
    },
    {
        id: 3,
        title: "Company"
    }
];

var articleData = [
    {
        id: 1,
        pagetitle: "Card title that wraps to a new line",
        image: "https://via.placeholder.com/500x250/171717/222222?text=500x250",
        tags: [
            "Internal",
            "Industry",
            "Company",
        ],
        content: "This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
        alias: "/news-article-1",
        published: 1672668835
    },
    {
        id: 2,
        pagetitle: "Card title",
        image: "https://via.placeholder.com/500x250/171717/222222?text=500x250",
        tags: [
            "Company",
        ],
        content: "This card has supporting text below as a natural lead-in to additional content.",
        alias: "/news-article-2",
        published: 1672668835
    },
    {
        id: 3,
        pagetitle: "Card with no image",
        image: "",
        tags: [
            "Internal",
            "Company",
        ],
        content: "This is another card with title and supporting text below. This card has some additional content to make it slightly taller overall.",
        alias: "/news-article-3",
        published: 1672668835
    },
    {
        id: 4,
        pagetitle: "Yet another article",
        image: "",
        tags: [
            "Industry"
        ],
        content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
        alias: "/news-article-4",
        published: 1672668835
    },
];

var vm = new Vue({
    el: "#app",
    data: {
        articles: articleData,
        search: '',
        tags: tagData,
        checkedTags: []
    },
    computed: {
        searchArticles: function searchArticles() {
            var result = this.articles.filter(function (article) {
                // articles can have more than one tag/category assigned
                //if any checkboxes have been checked...
                if(this.checkedTags.length) {
                    return article.tags.some(tag => tag.includes(this.checkedTags)) && (article.pagetitle.toLowerCase().match(this.search.toLowerCase()) || article.content.toLowerCase().match(this.search.toLowerCase()) )
                } else {
                    return article.pagetitle.toLowerCase().match(this.search.toLowerCase()) || article.content.toLowerCase().match(this.search.toLowerCase())   
                }
            }, this);
            return result;
        }
    }
});

checkedTags 包含一个数组。例如:["Internal", "Industry"] 我试过.some.every 的变体,但显然我的逻辑不正确。

【问题讨论】:

    标签: vue.js vuejs2 filtering


    【解决方案1】:

    您必须从此更改标签检查:

    article.tags.some(tag => tag.includes(this.checkedTags))
    

    对此:

    this.checkedTags.every( tag => article.tags.includes(tag))
    

    您必须检查文章是否存在所有选中的标签。

    您的支票 .some(tag => tag.includes 是错误的,因为您将 includes 应用于一个标签项目。应该不是这样。 includes 应该应用于数组。

    tags.includes(tag),但不是tag.includes(tags)

    如果tags.length > 1tag.includes(tags)将永远是false

    这里是工作的操场。

    var tagData = [
        {
            id: 1,
            title: "Internal"
        },
        {
            id: 2,
            title: "Industry"
        },
        {
            id: 3,
            title: "Company"
        }
    ];
    
    var articleData = [
        {
            id: 1,
            pagetitle: "Card title that wraps to a new line",
            image: "https://via.placeholder.com/500x250/171717/222222?text=500x250",
            tags: [
                "Internal",
                "Industry",
                "Company",
            ],
            content: "This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
            alias: "/news-article-1",
            published: 1672668835
        },
        {
            id: 2,
            pagetitle: "Card title",
            image: "https://via.placeholder.com/500x250/171717/222222?text=500x250",
            tags: [
                "Company",
            ],
            content: "This card has supporting text below as a natural lead-in to additional content.",
            alias: "/news-article-2",
            published: 1672668835
        },
        {
            id: 3,
            pagetitle: "Card with no image",
            image: "",
            tags: [
                "Internal",
                "Company",
            ],
            content: "This is another card with title and supporting text below. This card has some additional content to make it slightly taller overall.",
            alias: "/news-article-3",
            published: 1672668835
        },
        {
            id: 4,
            pagetitle: "Yet another article",
            image: "",
            tags: [
                "Industry"
            ],
            content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
            alias: "/news-article-4",
            published: 1672668835
        },
    ];
    
    var vm = new Vue({
        el: "#app",
        productionTip: false,
        data: {
            articles: articleData,
            search: '',
            tags: tagData,
            checkedTags: []
        },
        computed: {
            searchArticles: function searchArticles() {
                var result = this.articles.filter(function (article) {
                    // articles can have more than one tag/category assigned
                    //if any checkboxes have been checked...
                    if(this.checkedTags.length) {
                        return this.checkedTags.every( tag => article.tags.includes(tag)) &&  (article.pagetitle.toLowerCase().match(this.search.toLowerCase()) || article.content.toLowerCase().match(this.search.toLowerCase()) )
                    } else {
                        return article.pagetitle.toLowerCase().match(this.search.toLowerCase()) || article.content.toLowerCase().match(this.search.toLowerCase())   
                    }
                }, this);
                return result;
            }
        }
    });
    .search {
      margin: 8px 4px 8px 4px;
    }
    <div id="app">
    <div class="search">
    <label>Search:</label> <input v-model="search" />&nbsp;
    <label>Tags:</label> 
    <span v-for="tag in tags">
    <input type="checkbox" name="tags" :id="tag.id" :value="tag.title" v-model="checkedTags" /> 
    <label for="tag.id">{{tag.title}}</label>&nbsp;
    </span>
    </div>
    <table border=1>
    <thead><tr><th>Id</th><th>Title</th><th>Tags</th></tr></thead>
    <tbody>
    <tr v-for="item in searchArticles">
      <td>{{item.id}}</td>
      <td>{{item.pagetitle}}</td>
      <td>{{item.tags}}</td>
    </tr>  
    </tbody>
    </table>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 2012-01-14
      • 2013-05-06
      • 2016-08-15
      • 2020-04-06
      • 1970-01-01
      • 2011-06-28
      • 2014-08-27
      • 1970-01-01
      相关资源
      最近更新 更多