jQuery 按首字母过滤帖子 - 按字母顺序

jQuery filter posts by their first letter - Alphabetically

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

我曾使用插件尝试添加字母过滤器,但这对我不起作用(我想是因为我将产品显示为 posts)所以我想我会使用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 都有一个 class a、b、c、d 等

然而,在我的例子中,我的每个容器 divs 都会有相同的 class,但会有一个独特的 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 读取每个产品 divs 中 h3 的第一个字母,然后只在出现时显示相应的产品已按下字母过滤器。

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

我知道这只是一个起点,可能有更简单的方法来实现这一点,因此我们将不胜感激任何帮助。

您正在从单击按钮的 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>