List.js:如何添加过滤器

时间:2014-07-20 17:14:02

标签: javascript

我正在尝试调整list.js脚本(http://listjs.com/docs/list-api#filter),但我无法在列表中设置过滤器。

我想创建一个过滤器(通过一个按钮),只显示“1986”的项目

你能帮助我吗?

 <div id="users">
  <input class="search" placeholder="Search" />
  <button class="sort" data-sort="name">
    Sort by name
  </button>

  <ul class="list">
    <li>
      <h3 class="name">Jonny Stromberg</h3>
      <p class="born">1986</p>
    </li>
    <li>
      <h3 class="name">Jonas Arnklint</h3>
      <p class="born">1985</p>
    </li>
    <li>
      <h3 class="name">Martina Elm</h3>
      <p class="born">1986</p>
    </li>
    <li>
      <h3 class="name">Gustaf Lindqvist</h3>
      <p class="born">1983</p>
    </li>
  </ul>

</div>
<script src="http://listjs.com/no-cdn/list.js"></script>
<script>
 var options = {
  valueNames: [ 'name', 'born' ]
 };

 var userList = new List('users', options)
</script>

1 个答案:

答案 0 :(得分:1)

这是如何通过按下按钮过滤1986的一种解决方案。 Javascript使用标签(即文本)进行过滤。您还可以在按钮上添加属性以进行过滤。

$('.filter-1986').on('click',function(){
  var $text = $(this).text();
  if($(this).hasClass( 'selected' )){
    userList.filter();
    $(this).removeClass('selected');
  } else {
    userList.filter(function(item) {
      return (item.values().born == $text);
    });
    $(this).addClass('selected');
  }
});

你可以在这里尝试一下。

&#13;
&#13;
var options = {
  valueNames: [ 'name', 'born' ]
};

var userList = new List('users', options);


$('.filter-1986').on('click',function(){
  var $text = $(this).text();
  if($(this).hasClass( 'selected' )){
    userList.filter();
    $(this).removeClass('selected');
  } else {
    userList.filter(function(item) {
      return (item.values().born == $text);
    });
    $(this).addClass('selected');
  }
});
&#13;
.list {
  font-family:sans-serif;
  margin:0;
  padding:20px 0 0;
}
.list > li {
  display:block;
  background-color: #eee;
  padding:10px;
  box-shadow: inset 0 1px 0 #fff;
}
.avatar {
  max-width: 150px;
}
img {
  max-width: 100%;
}
h3 {
  font-size: 16px;
  margin:0 0 0.3rem;
  font-weight: normal;
  font-weight:bold;
}
p {
  margin:0;
}

input {
  border:solid 1px #ccc;
  border-radius: 5px;
  padding:7px 14px;
  margin-bottom:10px
}
input:focus {
  outline:none;
  border-color:#aaa;
}
button {
  padding:8px 30px;
  border-radius: 6px;
  border:none;
  display:inline-block;
  color:#fff;
  text-decoration: none;
  background-color: #28a8e0;
  height:30px;
}
.selected{
  background-color:#1b8aba;
}
button:hover{
  text-decoration: none;
  background-color:#1b8aba;
}
button:focus {
  outline:none;
}
button:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid transparent;
  content:"";
  position: relative;
  top:-10px;
  right:-5px;
}
.sort.asc:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #fff;
  content:"";
  position: relative;
  top:13px;
  right:-5px;
}
.sort.desc:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #fff;
  content:"";
  position: relative;
  top:-10px;
  right:-5px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="users">
  <input class="search" placeholder="Search" />
  <button class="sort" data-sort="name">
    Sort by name
  </button>
  <button class="filter-1986">1986</button>

  <ul class="list">
    <li>
      <h3 class="name">Jonny Stromberg</h3>
      <p class="born">1986</p>
    </li>
    <li>
      <h3 class="name">Jonas Arnklint</h3>
      <p class="born">1985</p>
    </li>
    <li>
      <h3 class="name">Martina Elm</h3>
      <p class="born">1986</p>
    </li>
    <li>
      <h3 class="name">Gustaf Lindqvist</h3>
      <p class="born">1983</p>
    </li>
  </ul>

</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
&#13;
&#13;
&#13;