【问题标题】:jQuery filter posts by their first letter - AlphabeticallyjQuery 按首字母过滤帖子 - 按字母顺序
【发布时间】:2019-02-08 12:50:34
【问题描述】:

我有一个来自 WooCommerce 的产品列表,在此处作为自定义帖子(使用页面构建器 Elementor)被拉入:http://staging.morningsidepharm.com/products/generic-medicines/

我使用了一个插件来尝试添加一个按字母顺序排列的过滤器,但这对我不起作用(我想是因为我将产品显示为帖子)所以我想我会使用 jquery 来构建一个。

我找到了以下代码:

HTML

<button class="active btn" id="all">Show All</button>
<button class="btn" id="a">Show A</button>
<button class="btn" id="b">Show B</button>
<button class="btn" id="c">Show C</button>
<button class="btn" id="d">Show D</button>

<div class="spacer"></div>

<div id="parent">
  <div class="box a b">A &amp; B</div>
  <div class="box a">A</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
</div>

CSS

* {
  box-sizing: border-box;
}
body {
  padding: 10px;
  background: #ecf0f1;
  font-family: helvetica, sans-serif;
  font-weight: 200;
}
.btn {
  border: none;
  background: linear-gradient(to bottom, #3498db, #2980b9);
  border-radius: 3px;
  font-family: Arial;
  color: #ffffff;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
  margin: 5px;
}

.active {
  background: linear-gradient(to bottom, #3cb0fd, #3498db);
  text-decoration: none;
}
.box {
  background-image: linear-gradient(to bottom, #3498db, #2980b9);
  padding: 10px;
  height: 100px;
  width: calc(25% - 10px);
  float: left;
  margin: 5px;
  text-align: center;
  border-radius: 3px;
  color: #fff;
}
.spacer {
  clear: both;
  height: 20px;
}

JS

var $btns = $('.btn').click(function() {
  if (this.id == 'all') {
    $('#parent > div').fadeIn(450);
  } else {
    var $el = $('.' + this.id).fadeIn(450);
    $('#parent > div').not($el).hide();
  }
  $btns.removeClass('active');
  $(this).addClass('active');
})

可以在此处查看实际操作:https://codepen.io/terf/pen/vGeqC

这似乎可行,因为每个 div 都有一个 a、b、c、d 等类。

但是,在我的例子中,我的每个容器 div 都有相同的类,但会有一个唯一的 h3 标题,例如:

<div class="product"><h3>Acarbose</h3></div>
<div class="product"><h3>Bicalutamide</h3></div>
<div class="product"><h3>Capecitabine</h3></div>
<div class="product"><h3>Dapsone</h3></div>

所以我认为,要完成这项工作,我需要 jQuery 读取每个产品 div 中 h3 的第一个字母,然后仅在按下字母过滤器时显示相应的产品。

或者将产品名称作为类添加到 div 中?

我知道这只是一个起点,可能有更简单的方法可以实现这一目标,因此我们将不胜感激。

【问题讨论】:

  • 我会添加一个参数,例如

    Acarbose

    。然后就可以检索这个参数了。

标签: javascript jquery html woocommerce elementor


【解决方案1】:

您正在从单击按钮的id 中搜索第一个字母。因此,您可以通过正则表达式将 filter() .product 元素确保它们以该字母开头,如下所示:

var $boxes = $('#parent > .box');

var $btns = $('.btn').click(function() {
  var id = this.id;
  if (id == 'all') {
    $boxes.fadeIn(450);
  } else {
    $boxes.fadeOut(450).filter(function() {
      var re = new RegExp('^' + id, 'i');
      return re.test($(this).text().trim());
    }).stop(true).fadeIn(450);
  }
  $btns.removeClass('active');
  $(this).addClass('active');
})
* {
  box-sizing: border-box;
}

body {
  padding: 10px;
  background: #ecf0f1;
  font-family: helvetica, sans-serif;
  font-weight: 200;
}

.btn {
  border: none;
  background: linear-gradient(to bottom, #3498db, #2980b9);
  border-radius: 3px;
  font-family: Arial;
  color: #ffffff;
  padding: 5px 10px 5px 10px;
  text-decoration: none;
  margin: 5px;
}

.active {
  background: linear-gradient(to bottom, #3cb0fd, #3498db);
  text-decoration: none;
}

.box {
  background-image: linear-gradient(to bottom, #3498db, #2980b9);
  padding: 10px;
  height: 100px;
  width: calc(25% - 10px);
  float: left;
  margin: 5px;
  text-align: center;
  border-radius: 3px;
  color: #fff;
}

.spacer {
  clear: both;
  height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="active btn" id="all">Show All</button>
<button class="btn" id="a">Show A</button>
<button class="btn" id="b">Show B</button>
<button class="btn" id="c">Show C</button>
<button class="btn" id="d">Show D</button>

<div class="spacer"></div>

<div id="parent">
  <div class="box">
    <h3>Acarbose</h3>
  </div>
  <div class="box">
    <h3>Bicalutamide</h3>
  </div>
  <div class="box">
    <h3>Capecitabine</h3>
  </div>
  <div class="box">
    <h3>Dapsone</h3>
  </div>
</div>

【讨论】:

  • 太好了——我能够理解并修改它以适应我的用例!非常感谢:)
  • 对不起,是否可以做到不褪色?当我最初过滤产品时,所有产品都会一一淡出 - 我试过了,但它不对 - var $boxes = $('.elementor-posts-container > .elementor-post'); var $btns = $('.alphabet-btn').click(function() { var id = this.id; if (id == 'all') { $boxes.; } else { $boxes.filter(function () { var re = new RegExp('^' + id, 'i'); return re.test($(this).text().trim()); }).stop(true); } $btns .removeClass('alphabet-active'); $(this).addClass('alphabet-active'); })
  • 当然,只需将fadeIn(450) 更改为show() 并将fadeOut(450) 更改为hide()jsfiddle.net/8dh0mnf7
  • 这很好,但是,第一次点击似乎失败了?如果您访问此处并单击 B,例如,staging.morningsidepharm.com/products/generic-medicines 它会显示以 B 开头的药物,然后它会淡入其他一些药物......但是当我浏览它时,它就可以了......它只是看起来的初始点击奇怪吗?
  • 我猜你有另一个事件处理程序绑定到需要删除的按钮单击
猜你喜欢
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
相关资源
最近更新 更多