Framework7 - 搜索栏



描述

Framework 7 允许使用 searchbar 类搜索元素。

搜索栏参数

序号 参数 & 描述 类型 默认值
1

searchList

搜索列表的 CSS 选择器或 HTML 元素。

字符串或 HTML 元素 -
2

searchIn

您可以搜索 CSS 选择器的列表视图元素,也可以通过传递 .item-title.item-text 类来搜索元素。

字符串 '.item-title'
3

found

使用 "found" 元素搜索 CSS 选择器或 HTML 元素。如果未指定元素,则使用 .searchbar-found 元素。

字符串或 HTML 元素 -
4

notfound

使用 "not-found" 元素搜索 CSS 选择器或 HTML 元素。如果未指定元素,则使用 .searchbar-not-found 元素。

字符串或 HTML 元素 -
5

overlay

使用 "searchbar overlay" 元素搜索 CSS 选择器或 HTML 元素,如果未指定元素,则使用 .searchbar-overlay 元素。

字符串或 HTML 元素 -
6

ignore

您可以使用搜索栏忽略项目的 CSS 选择器。

字符串 '.searchbar-ignore'
7

customSearch

启用此选项后,搜索栏将不会搜索 searchList 指定的任何列表块,您可以使用自定义搜索功能。

布尔值 false
8

removeDiacritics

搜索元素时,启用此参数以删除变音符号。

布尔值 false
9

hideDividers

如果列表中没有项目,此参数将隐藏项目分隔符和分组标题。

布尔值 true
10

hideGroups

如果列表视图分组中没有找到项目,此参数将隐藏分组。

布尔值 true

搜索栏回调函数

序号 回调函数 & 描述 类型 默认值
1

onSearch

搜索期间将触发此回调函数。

function (s) -
2

onEnable

搜索栏变为活动状态时将触发此回调函数。

function (s) -
3

onDisable

搜索栏变为非活动状态时将触发此回调函数。

function (s) -
4

onClear

单击“清除”元素时将触发此回调函数。

function (s) -

搜索栏属性

序号 属性 & 描述
1

mySearchbar.params

表示使用对象传递的已初始化参数。

2

mySearchbar.query

搜索当前查询。

3

mySearchbar.searchList

定义搜索列表块。

4

mySearchbar.container

使用 HTML 元素定义搜索栏容器。

5

mySearchbar.input

使用 HTML 元素定义搜索栏输入。

6

mySearchbar.active

定义搜索栏是启用还是禁用。

搜索栏方法

序号 方法 & 描述
1

mySearchbar.search(query);

此方法搜索传递的查询。

2

mySearchbar.enable();

启用搜索栏。

3

mySearchbar.disable();

禁用搜索栏。

4

mySearchbar.clear();

您可以清除查询和搜索结果。

5

mySearchbar.destroy();

销毁搜索栏实例。

搜索栏 JavaScript 事件

序号 事件 & 描述 目标
1

search

搜索元素时可以触发此事件。

<div class="list-block">
2

clearSearch

用户单击 clearSearch 元素时将触发此事件。

<div class="list-block">
3

enableSearch

搜索栏启用时,将触发此事件。

<div class="list-block">
4

disableSearch

搜索栏禁用,用户单击取消按钮或“search bar-overlay”元素时,将触发此事件。

<div class="list-block">

示例

以下示例演示了在 Framework7 中滚动时使用搜索栏的方法:

<!DOCTYPE html>
<html>

   <head>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, 
         maximum-scale = 1, minimum-scale = 1, user-scalable = no, minimal-ui" />
      <meta name = "apple-mobile-web-app-capable" content = "yes" />
      <meta name = "apple-mobile-web-app-status-bar-style" content = "black" />
      <title>Search Bar Layout</title>
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.min.css" />
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.colors.min.css" />
   </head>

   <body>
      <div class = "views">
         <div class = "view view-main">
            <div class = "pages navbar-fixed">
               <div data-page = "home" class = "page">
               
                  <div class = "navbar">
                     <div class = "navbar-inner">
                        <div class = "center sliding">Search Bar Layout</div>
                     </div>
                  </div>
                  
                  <form data-search-list = ".list-block-search" data-search-in = ".item-title" 
                     class = "searchbar searchbar-init">
                     <div class = "searchbar-input">
                        <input type = "search" placeholder = "Search"><a href = "#" 
                           class = "searchbar-clear"></a>
                     </div>
                     <a href = "#" class = "searchbar-cancel">Cancel</a>
                  </form>
                  
                  <div class = "searchbar-overlay"></div>
                  
                  <div class = "page-content">
                     <div class = "content-block searchbar-not-found">
                        <div class = "content-block-inner">No element found...</div>
                     </div>
                     
                     <div class = "list-block list-block-search searchbar-found">
                        <ul>
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">India</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Argentina</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Belgium</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Brazil</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Canada</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Colombia</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Denmark</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Ecuador</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">France</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Germany</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Greece</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Haiti</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Hong Kong</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Iceland</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Ireland</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Jamaica</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Japan</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Kenya</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Kuwait</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Libya</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Liberia</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Malaysia</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Mauritius</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Mexico</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Namibia</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">New Zealand</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Oman</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Paraguay</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Philippines</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Russia</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Singapore</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">South Africa</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Thailand</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">United Kingdom</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Vatican City</div>
                              </div>
                           </li>
                           
                           <li class = "item-content">
                              <div class = "item-inner">
                                 <div class = "item-title">Zimbabwe</div>
                              </div>
                           </li>
                        </ul>
                     </div>
                     
                  </div>
               </div>
            </div>
         </div>
      </div>
      
      <script type = "text/javascript" 
         src = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/js/framework7.min.js"></script>
      
      <script>
         var myApp = new Framework7();
         var mainView = myApp.addView('.view-main');
      </script>
   </body>

</html>

输出

让我们执行以下步骤来查看上面给出的代码是如何工作的:

  • 将上面给出的 HTML 代码保存为search_bar.html文件到您的服务器根目录。

  • 打开此 HTML 文件,地址为 https://127.0.0.1/search_bar.html,输出将如下所示。

  • 如果在搜索栏中输入列表中包含的元素,则会显示列表中的该特定元素。

  • 如果输入的元素不在列表中包含的元素中,则会显示未找到元素。

广告