【问题标题】:What's the most efficient way to filter on categorie with jquery?使用 jquery 过滤类别的最有效方法是什么?
【发布时间】:2020-09-22 14:27:16
【问题描述】:

我对编程很陌生,现在我有一个网站,可以在前端显示与我的 json 文件不同的烟花。我制作了一个类别菜单,我想实际工作。我想知道使这个菜单工作的最有效方法是什么。我做了一个函数,它从我的 json 文件中返回所有现有的 json 对象,并想知道是否可以使用这个现有的函数来过滤不同的类别,并且只显示具有指定类别的项目。我用来循环所有 de json 对象的脚本是:

$(function(){
$.getJSON("assets/products/sample_products.json", function(response) {
    $.each(response.data, function (i, el) {
        let card = $($('#productCard-template').html());
        card.find('.container > p').html( el.name + '<br> &euro;' +  el.price );
        card.find('.productItem').attr('data-price', el.price)
            .attr('data-article-number', el.article_number)
            .attr('data-id', el.id)
            .attr('data-name', el.name)
            .attr('data-slug', el.slug);
        $('#touchViewProducts').append(card);
    });
});

});

过滤功能:

//filter function
$(".nav-link").click(function() {
    var category = $(this).attr('id');
    if (category != "all") {
        $(".productCard").hide();
        $(".productCard").each(function() {
            if ($(this).find(".productItem").attr('data-slug') == category) {
                $(this).show() //show that div
            }
        })
    } else {
        $(".productCard").show();
    }
})

这是数据在前端显示的模板:

 <script type="text/template" id="productCard-template">
        <div class="col-3 productCard">
            <a href="#" class="productItem">
                <div class="card">
                    <img src="assets/images/Firecracker.jpg" alt="Avatar" style="width: 100%; height: 8vh;">
                    <div class="container">
                        <p>test</p>
                    </div>
                </div>
            </a>
        </div>
    </script>

这是我的 json 文件中的代码示例,我想过滤“slug”:{

"data":[
    {
      "id":"1333",
      "article_number":"4016",
      "barcode":"heeftgeenbarcode4",
      "name":"White Albino",
      "stock":null,
      "to_sell":null,
      "price":"50",
      "brand":{
        "id":"26",
        "name":"Fireworks Festival",
        "slug":"grond-en-siervuurwerk",
        "logo_path":"\/uploads\/product-brands\/26\/5d8e3cd1a865f.png"
      },

菜单html:

  <div class="col-3 categoriesSection">
                    <div class="categories">
                        <p style="background-color: white; margin-bottom: 0px" > Categories </p>
                        <a class="nav-link" id="all" href="#">All</a>
                        <a class="nav-link" id="black-thunder" href="#">Black-thunder</a>
                        <a class="nav-link" id="blue-eagle-fireworks" href="#">Blue-eagle-fireworks</a>
                        <a class="nav-link" id="crystal-exclusive" href="#">Crystal-exclusive</a>
                        <a class="nav-link" id="empire-fireworks" href="#">Empire-fireworks</a>
                        <a class="nav-link" id="grondbloemen" href="#">Grondbloemen</a>
                    </div>
                </div>

希望一切都清楚,谢谢!

【问题讨论】:

  • @Andreas 如前所述,我对这些东西很陌生。谢谢:)
  • 您好,您需要如何过滤?点击按钮?你怎么知道要过滤哪个slug?请详细说明..
  • @Swati hanks 回复,我制作了一个包含不同“slugs”的菜单,可以在 json 文件中找到。我希望页面中填充有所选蛞蝓的产品。我将菜单的html添加到编辑中!
  • 你需要过滤a标签的onclick吗?同样在这里.attr('data-slug', el.slug);您将slug的价值存储在产品的a标签中? grond-en-siervuurwerk 你的菜单在哪里?

标签: html jquery json


【解决方案1】:

您可以在这里使用click 事件,当用户单击a 标记时,获取具有不同类别的id,然后遍历所有productCard div 并检查其中是否有 a 标记它的 data-slug 等于用户根据此显示该 div 选择的类别。

演示代码

$(".nav-link").click(function() {
  //get id of the a tag
  var category = $(this).attr('id');
  //check is not "all"
  if (category != "all") {
    //hide product div
    $(".productCard").hide();
    //loop through product div
    $(".productCard").each(function() {
      //check data-slug == category which user has clicked
      if ($(this).find(".productItem").attr('data-slug') == category) {
        $(this).show() //show that div
      }
    })
  } else {

    $(".productCard").show();
  }

})
.productCard {
  border: 1px solid blue
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-3 categoriesSection">
  <div class="categories">
    <p style="background-color: white; margin-bottom: 0px"> Categories </p>
    <a class="nav-link" id="all" href="#">All</a>
    <a class="nav-link" id="black-thunder" href="#">Black-thunder</a>
    <a class="nav-link" id="blue-eagle-fireworks" href="#">Blue-eagle-fireworks</a>
    <a class="nav-link" id="crystal-exclusive" href="#">Crystal-exclusive</a>
    <a class="nav-link" id="empire-fireworks" href="#">Empire-fireworks</a>
    <a class="nav-link" id="grondbloemen" href="#">Grondbloemen</a>
  </div>
</div>

<div class="col-3 productCard">
  <a href="#" class="productItem" data-slug="crystal-exclusive">
    <div class="card">
      <img src="assets/images/Firecracker.jpg" alt="Avatar" style="width: 100%; height: 8vh;">
      <div class="container">
        <p>test wwrbrb crystal-exclusive</p>
      </div>
    </div>
  </a>
</div>
<div class="col-3 productCard">
  <a href="#" class="productItem" data-slug="black-thunder">
    <div class="card">
      <img src="assets/images/Firecracker.jpg" alt="Avatar" style="">
      <div class="container">
        <p>testbrbr black-thunder</p>
      </div>
    </div>
  </a>
</div>
<div class="col-3 productCard">
  <a href="#" class="productItem" data-slug="grondbloemen">
    <div class="card">
      <img src="assets/images/Firecracker.jpg" alt="Avatar" style="">
      <div class="container">
        <p>Soemthing 3 grondbloemen</p>
      </div>
    </div>
  </a>
</div>
<div class="col-3 productCard">
  <a href="#" class="productItem" data-slug="grondbloemen">
    <div class="card">
      <img src="assets/images/Firecracker.jpg" alt="Avatar" style="">
      <div class="container">
        <p>Soemthing 2 grondbloemen</p>
      </div>
    </div>
  </a>
</div>
<div class="col-3 productCard">
  <a href="#" class="productItem" data-slug="empire-fireworks">
    <div class="card">
      <img src="assets/images/Firecracker.jpg" alt="Avatar" style="">
      <div class="container">
        <p>Soemthing 2 empire-fireworks</p>
      </div>
    </div>
  </a>
</div>
<div class="col-3 productCard">
  <a href="#" class="productItem" data-slug="blue-eagle-fireworks">
    <div class="card">
      <img src="assets/images/Firecracker.jpg" alt="Avatar" style="">
      <div class="container">
        <p>Soemthing 2 blue-eagle-fireworks</p>
      </div>
    </div>
  </a>
</div>

【讨论】:

  • 感谢您的好解决方案,我现在很清楚了。 “全部”确实有效,但由于某种原因,对 id 的过滤对我不起作用,当我按下“全部”以外的类别之一时,一切都被隐藏了。
  • 确保iddata-slug 应该具有相同的值.. 同样在您的代码中,只要您有attr('data-name', el.slug); 应该是attr('data-slug', el.slug); 更改它并查看
  • 我做到了,我现在将问题中的代码更新为我最近的代码,所以你应该能够看到它。
  • 抱歉,我忘了说没有,很遗憾。
  • 您是否检查过这些值是否相同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
  • 2011-06-10
  • 1970-01-01
  • 2021-07-07
相关资源
最近更新 更多