阻止自动搜索/添加搜索按钮List.js

时间:2017-08-29 22:43:36

标签: javascript list.js

这应该是一个简单的问题,但我还没有在list.js文档的任何地方找到解决方案。当我在搜索字段中键入并改为添加搜索按钮时,如何防止列表自动搜索?



var options = {
    valueNames: ['material', 'type', 'thickness', 'height', 'category']
};

var featureList = new List('piece-search', options);

.item {
    margin: 0.5em;
    padding: 0.5em;
    width: 150px;
    height: 150px;
    float: left;
    background: #229B55;
    color: #F4F4F4;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);
}

.item p {
    margin: 0;
    padding: 0;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.1.1/list.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
    <div class="page">
        <div id="main">
            <div class="c1">
                <div id="piece-search">
                    <ul class="sort-by">
                        <li>
                            <input class="search" placeholder="Search pieces">
                        </li>
                    </ul>

                    <ul class="list">

                        <li class="item">
                            <p class="sorting-info hide-this">
                                <p class="material">plastic</p>
                                <p class="type">pipe</p>
                                <p class="thickness">3mm</p>
                                <p class="height">15inch+</p>
                                <p class="category">artsy</p>
                        </li>

                        <li class="item">
                            <p class="sorting-info hide-this">
                                <p class="material">glass</p>
                                <p class="type">pipe</p>
                                <p class="thickness">5mm</p>
                                <p class="height">14inch-</p>
                                <p class="category">scientific</p>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

搜索的default类是&#34;搜索&#34;根据文件。因此,您需要从文本输入框中删除/更改classname,以防止自动搜索onkeyup

我为搜索添加了button,将调用函数search(),您将获得搜索文本值并手动搜索。

要手动搜索,请阅读提供的文档here

将在按钮点击时触发的JS功能:

function search() {
  var searchText = document.getElementById("searchText").value;
  //console.log(searchText);

  var options = {
    valueNames: ['material', 'type', 'thickness', 'height', 'category']
  };

  var featureList = new List('piece-search', options);
  // Search manually
  featureList.search(searchText);

  // Search manually on specific columns
  //listObj.search(searchText, [ 'material' ]);
}

&#13;
&#13;
function search() {
  var searchText = document.getElementById("searchText").value;
  //console.log(searchText);

  var options = {
    valueNames: ['material', 'type', 'thickness', 'height', 'category']
  };

  var featureList = new List('piece-search', options);
  // Search manually
  featureList.search(searchText);

  // Search manually on specific columns
  //listObj.search(searchText, [ 'material' ]);
}
&#13;
.item {
  margin: 0.5em;
  padding: 0.5em;
  width: 150px;
  height: 150px;
  float: left;
  background: #229B55;
  color: #F4F4F4;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);
}

.item p {
  margin: 0;
  padding: 0;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.1.1/list.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div class="page">
    <div id="main">
      <div class="c1">
        <div id="piece-search">
          <ul class="sort-by">
            <li>
              <input id="searchText" placeholder="Type text to search">
            </li>
            <li>
              <input type="button" class="searchButton" value="Search pieces" onclick="search()">
            </li>
          </ul>

          <ul class="list">

            <li class="item">
              <p class="sorting-info hide-this">
                <p class="material">plastic</p>
                <p class="type">pipe</p>
                <p class="thickness">3mm</p>
                <p class="height">15inch+</p>
                <p class="category">artsy</p>
            </li>

            <li class="item">
              <p class="sorting-info hide-this">
                <p class="material">glass</p>
                <p class="type">pipe</p>
                <p class="thickness">5mm</p>
                <p class="height">14inch-</p>
                <p class="category">scientific</p>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
&#13;
&#13;
&#13;