jQuery Mobile C...login
jQuery Mobile Classic Tutorial
author:php.cn  update time:2022-04-11 13:58:34

jQuery Mobile filtering



Filterable elements

All elements can be filtered if they have one or more child elements.

How to create a search field:

  • #The elements you want to filter must use data-filter="true" attribute.

  • Create <input> element and specify id, add data-type="search" attribute. This creates a basic search field. Place the <input> element inside a form, and the form <form> element uses the "ui-filterable" class - this class will adjust the margins of the search field and filter elements.

  • Then add the data-input attribute to the filtered elements. The value needs to be the id of the <input> element.

Next we create a filterable list:

Instance

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="main" class="ui-content">
    <h2>我的通讯录</h2>
    <form class="ui-filterable">
      <input id="myFilter" data-type="search">
    </form>
    <ul data-role="listview" data-filter="true" data-input="#myFilter" data-autodividers="true" data-inset="true">
      <li><a href="#">Adele</a></li>
      <li><a href="#">Agnes</a></li>
      <li><a href="#">Albert</a></li>
      <li><a href="#">Billy</a></li>
      <li><a href="#">Bob</a></li>
      <li><a href="#">Calvin</a></li>
      <li><a href="#">Cameron</a></li>
      <li><a href="#">Chloe</a></li>
      <li><a href="#">Christina</a></li>
      <li><a href="#">Diana</a></li>
      <li><a href="#">Gabriel</a></li>
      <li><a href="#">Glen</a></li>
      <li><a href="#">Ralph</a></li>
      <li><a href="#">Valarie</a></li>
    </ul>
  </div>
</div>

</body>
</html>

Run the instance »

Click the "Run Instance" button to view the online instance

Tips: You can use the placeholder attribute in the search field to set the tip information:

Instance

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="main" class="ui-content">
    <h2>我的通讯录</h2>
    <form class="ui-filterable">
      <input id="myFilter" data-type="search" placeholder="根据名称搜索..">
    </form>
    <ul data-role="listview" data-filter="true" data-input="#myFilter" data-autodividers="true" data-inset="true">
      <li><a href="#">Adele</a></li>
      <li><a href="#">Agnes</a></li>
      <li><a href="#">Albert</a></li>
      <li><a href="#">Billy</a></li>
      <li><a href="#">Bob</a></li>
      <li><a href="#">Calvin</a></li>
      <li><a href="#">Cameron</a></li>
      <li><a href="#">Chloe</a></li>
      <li><a href="#">Christina</a></li>
      <li><a href="#">Diana</a></li>
      <li><a href="#">Gabriel</a></li>
      <li><a href="#">Glen</a></li>
      <li><a href="#">Ralph</a></li>
      <li><a href="#">Valarie</a></li>
    </ul>
  </div>
</div> 

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance


Custom filtering

Generally, the text inserted into each list item is used as filtered text (such as A corresponding to "Adele" or "B" corresponds to "Billy"). However, if you want to specify custom filtered text, you need to use the data-filtertext attribute on the child element:

Example

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="main" class="ui-content">
    <h2>我的通讯录</h2>
    <p>以下实例中,我们在 "Adele" 中使用了 data-filtertext="fav" 。这意味着你要找到 "Adele" 需要使用以下关键字:f, a, v 或 fav。</p>
    <form class="ui-filterable">
      <input id="myFilter" data-type="search">
    </form>
    <ul data-role="listview" data-filter="true" data-input="#myFilter" data-autodividers="true" data-inset="true">
      <li data-filtertext="fav"><a href="#">Adele</a></li>
      <li><a href="#">Agnes</a></li>
      <li><a href="#">Albert</a></li>
      <li><a href="#">Billy</a></li>
      <li><a href="#">Bob</a></li>
      <li><a href="#">Calvin</a></li>
      <li><a href="#">Cameron</a></li>
      <li><a href="#">Chloe</a></li>
      <li><a href="#">Christina</a></li>
      <li><a href="#">Diana</a></li>
      <li><a href="#">Frank</a></li>
      <li><a href="#">Gabriel</a></li>
      <li><a href="#">Glen</a></li>
      <li><a href="#">Ralph</a></li>
      <li><a href="#">Valarie</a></li>
    </ul>
  </div>
</div>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance


If you are in the element The data-filtertext attribute is used in the element, and the source text content of the element will be ignored during filtering. If you still want to find the list item "Adele", the keywords you need to use are: f, a, v or fav.
实例

More examples

Filtering the collapsed list

Examples

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>过滤折叠列表</h1>
  </div>

  <div data-role="main" class="ui-content">
    <form>
      <input data-type="search" id="filterCollapsibles">
    </form>
    <div data-role="collapsibleset" data-filter="true" data-inset="true" id="myFilter" data-input="#filterCollapsibles">
      <div data-role="collapsible" data-filtertext="Cities">
        <h3>城市</h3>
        <ul data-role="listview" data-inset="false">
          <li>Copenhagen</li>
          <li>Mexico City</li>
          <li>Oslo</li>
          <li>Paris</li>
        </ul>
        </div>
        <div data-role="collapsible" data-filtertext="Countries">
          <h3>国家</h3>
          <ul data-role="listview" data-inset="false">
            <li>Denmark</li>
            <li>France</li>
            <li>Mexico</li>
            <li>Norway</li>
          </ul>
        </div>
        <div data-role="collapsible" data-filtertext="Days">
          <h3>周</h3>
          <ul data-role="listview" data-inset="false">
            <li>Monday</li>
            <li>Tuesday</li>
            <li>Wednesday</li>
          </ul>
        </div>
        <div data-role="collapsible" data-filtertext="Months">
          <h3>月</h3>
          <ul data-role="listview" data-inset="false">
            <li>January</li>
            <li>February</li>
            <li>March</li>
          </ul>
        </div>
    </div>
  </div>

  <div data-role="footer">
    <h1>文本底部</h1>
  </div>
</div>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

How to filter the collapsed list.

Filter form

Instance

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<style>
th
{
border-bottom: 1px solid #d6d6d6;
}
tr:nth-child(even)
{
background:#e9e9e9;
}
</style>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>可过滤的表格</h1>
  </div>
  
  <div data-role="main" class="ui-content">
    <form>
      <input id="filterTable-input" data-type="search" placeholder="Search For Customers...">
    </form>
    <table data-role="table" data-mode="columntoggle" class="ui-responsive ui-shadow" id="myTable" data-filter="true" data-input="#filterTable-input">
      <thead>
        <tr>
          <th data-priority="6">CustomerID</th>
          <th>CustomerName</th>
          <th data-priority="1">ContactName</th>
          <th data-priority="2">Address</th>
          <th data-priority="3">City</th>
          <th data-priority="4">PostalCode</th>
          <th data-priority="5">Country</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Alfreds Futterkiste</td>
          <td>Maria Anders</td>
          <td>Obere Str. 57</td>
          <td>Berlin</td>
          <td>12209</td>
          <td>Germany</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Antonio Moreno Taquer</td>
          <td>Antonio Moreno</td>
          <td>Mataderos 2312</td>
          <td>Mico D.F.</td>
          <td>05023</td>
          <td>Mexico</td>
        </tr>
        <tr>
          <td>3</td>
          <td>Around the Horn</td>
          <td>Thomas Hardy</td>
          <td>120 Hanover Sq.</td>
          <td>London</td>
          <td>WA1 1DP</td>
          <td>UK</td>
        </tr>
        <tr>
          <td>4</td>
          <td>Berglunds snabbk</td>
          <td>Christina Berglund</td>
          <td>Berguvsven 8</td>
          <td>Lule</td>
          <td>S-958 22</td>
          <td>Sweden</td>
        </tr>

      </tbody>
    </table>
  </div>

  <div data-role="footer">
    <h1>底部文本</h1>
  </div>
</div>

</body>
</html>

Run instance»

Click "Run instance" " button to view online examples

How to filter table content.

Filter<div> element

Instance

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>过滤所有</h1>
  </div>

  <div data-role="main" class="ui-content">
    <form>
      <input data-type="search" id="divOfPs-input">
    </form>
    <div class="elements" data-filter="true" data-input="#divOfPs-input">
      <p><strong>These</strong> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam</p>
      <p><strong>p elements</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</p>
      <p><strong>are</strong> et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est</p>
      <p><strong>filterable</strong> Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur</p>
    </div>
  </div>

  <div data-role="footer">
    <h1>底部文本</h1>
  </div>
</div>

</body>
</html>

Run instance»

Click "Run" Example" button to view online examples

How to filter the child <p> elements contained in the <div> element.

php.cn