【问题标题】:Javascript filters, how to make each of them function independently?Javascript过滤器,如何使它们各自独立运行?
【发布时间】:2021-04-21 03:21:41
【问题描述】:

https://www.australianathleticscalendar.com.au/

这个网站最初有一个过滤器,但我添加了另一个,希望它们各自独立工作(即按类别(事件)过滤,也按状态过滤)。

它似乎仍然充当一个过滤器,因为我只能在设置的两个过滤器之间选择一个变量。如何调整功能以使每个过滤器独立运行?

    function loadCategories() {
    fetch(projectUrl + '/categories')
    .then((response) => response.json())
    .then(json => {
        this.categories = json.categories;
        drawCategories();
    })
}

function loadStates() {
    fetch(projectUrl + '/states')
    .then((response) => response.json())
    .then(json => {
        this.states = json.states;
        drawStates();
    })
}

function drawProducts(products) {
    var template = Handlebars.compile(document.getElementById("products-template").innerHTML);
    document.getElementById('products-container').innerHTML = template({
        title: this.title,
        titleStates:this.titleStates,
        products: products  
    });
}

function drawCategories() {
    var template = Handlebars.compile(document.getElementById("menu-template").innerHTML);
    console.log('draw ', this.products);
    document.getElementById('menu-container').innerHTML = template(this.categories);
}

function drawStates() {
    var template = Handlebars.compile(document.getElementById("menu-template-2").innerHTML);
    console.log('draw ', this.products);
    document.getElementById('menu-container-states').innerHTML = template(this.states);
}

function showAllProducts() {
    this.title = "All Events";
    drawProducts(this.products);
}

function showAllProducts() {
    this.titleStates = "All States";
    drawProducts(this.products);
}

function showCategory(category) {
    this.title = category;
    let filteredProducts = this.products.filter(product => {
        return product.category.toLowerCase().includes(category.toLowerCase());
    }); 
    drawProducts(filteredProducts);
}


function showState(state) {
    this.titleStates = state;
    let filteredProducts = this.products.filter(product => {
        return product.state.toLowerCase().includes(state.toLowerCase());
    }); 
    drawProducts(filteredProducts);
}

它们在菜单中被调用,这里有两个过滤器:

      <!-- Categories/events filter -->
<div class="container">
    <script id="menu-template" type="text/x-handlebars-template">
            
                <ul class="navbar-nav">
                    {{#each this as |category|}}
                        <li class="nav-item"></li>
                            <a class="nav-link" href="#" onclick="showCategory('{{category.name}}');">{{category.name}}</a>
                        </li>
                    {{/each}}
                    <!-- <a id="all-events" href="#" onclick="showAllProducts();">All Events</a> -->
                    <a class="navbar-brand hover-color" href="#" onclick="showAllProducts();">All Events</a>
                </ul>
                
                <ul class="navbar-nav ml-auto"></ul>
                    <li class="nav-item">
                        
                    </li>
                </ul>

        </div>
    </script>
</div>

<!-- States filter -->
<div class="container">
    <script id="menu-template-2" type="text/x-handlebars-template">
            
                <ul class="navbar-nav">
                    {{#each this as |state|}}
                        <li class="nav-item"></li>
                            <a class="nav-link" href="#" onclick="showState('{{state.name}}');">{{state.name}}</a>
                        </li>
                    {{/each}}
                    <!-- <a id="all-events" href="#" onclick="showAllProducts();">All Events</a> -->
                    <a class="navbar-brand hover-color" href="#" onclick="showAllProducts();">All States</a>
                </ul>
                
                <ul class="navbar-nav ml-auto"></ul>
                    <li class="nav-item">
                        
                    </li>
                </ul>

        </div>
    </script>
</div>

【问题讨论】:

  • Javascript 与 java 无关。请删除 java 标签。
  • 无法打开您的链接。请添加您调用这些函数的部分。
  • @trincot 我在调用它们的地方添加了 html。您应该可以在此处查看该站点:australianathleticscalendar.com.au
  • 你有两倍的函数showAllProducts:第一个将被第二个覆盖。这是复制/粘贴错误吗?如果你用把手标记这个问题,这个问题会得到更具体的关注,因为你的代码依赖于它。
  • 我需要一个重置类别过滤器的 showAllCategories 和一个重置状态过滤器的 showAllStates,但是我不知道如何让它们单独运行

标签: javascript jquery web filtering


【解决方案1】:

首先,您有一个重复的函数showAllProducts,您还链接到了两个不同的按钮。我想你打算有两个不同的函数,一个用于每个“All”按钮,所以添加一个名为 showAllEvents 和另一个 showAllStates 的函数,并将它们作为按钮处理程序引用。

关于问题:您应该将过滤移至一个函数,该函数将考虑两个过滤器。您可以使用this.titlethis.titleState 执行过滤,确保正确处理“全部”大小写。

因此,您绑定到 HTML 的过滤功能应该变成:

function showAllProducts() {
    this.title = "All Events";
    this.titleStates = "All States";
    drawProducts();
}

function showAllEvents() {
    this.title = "All Events";
    drawProducts();
}

function showAllStates() {
    this.titleStates = "All States";
    drawProducts();
}

function showCategory(category) {
    this.title = category;
    drawProducts();
}

function showState(state) {
    this.titleStates = state;
    drawProducts();
}

请注意新函数,因此请确保在 HTML 中引用它们。其次,我从这些函数中删除了过滤,正如我在drawProducts 中建议的那样。现在该函数将不接受任何参数:

function drawProducts() { // remove argument; extend with filtering
    var template = Handlebars.compile(document.getElementById("products-template").innerHTML);
    // Perform filtering here:
    var filteredProducts = this.products.filter(product => {
        return (this.title.slice(0, 4) === "All " || product.category.toLowerCase().includes(this.title.toLowerCase()))
            && (this.titleState.slice(0, 4) === "All " || product.state.toLowerCase().includes(this.titleState.toLowerCase());
    });
    document.getElementById('products-container').innerHTML = template({
        title: this.title,
        titleStates: this.titleStates,
        products: filteredProducts // <--  
    });
}

【讨论】:

  • 我似乎仍然在此功能上遇到错误:function loadProducts() { fetch(projectUrl + '/products') .then((response) =&gt; response.json()) .then(json =&gt; { this.products = json.products; showAllProducts(); }); }
  • 所以想想吧。你明白为什么吗?我为showAllProducts 添加了一个定义,它就像我介绍的其他两个ShowAllXX 的组合。不要忘记将两个“全部”按钮绑定到这两个新功能。
  • 有效!我把它推送给你看:australianathleticscalendar.com.au 老实说,我非常感谢你的帮助。
  • 完成,再次感谢。我很好奇,你为什么要花这么多精力去帮助别人,你有什么收获?
  • 我喜欢编程和挑战。通过这样做,我还从我在其他答案中读到的内容中学到了新东西。这是一种爱好。
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多