搜索栏(Search Bar)

优质
小牛编辑
135浏览
2023-12-01

描述 (Description)

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

搜索栏参数

S.No参数和描述类型默认
1

searchList

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

字符串或HTML元素-
2

searchIn

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

string'.item-title'
3

found

它使用“found”元素搜索CSS选择器或HTML元素。 此外,如果没有指定元素,它使用.searchbar-found元素。

字符串或HTML元素-
4

notfound

它使用“not-found”元素搜索CSS选择器或HTML元素。 此外,如果没有指定元素,它使用.searchbar-not-found元素。

字符串或HTML元素-
5

overlay

它使用“ .searchbar-overlay overlay”元素搜索CSS选择器或HTML元素,如果没有指定元素,则使用.searchbar-overlay元素。

字符串或HTML元素-
6

ignore

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

string'.searchbar-ignore'
7

customSearch

启用后,搜索栏将不会搜索searchList指定的任何列表块,您将被允许使用自定义搜索功能。

booleanfalse
8

removeDiacritics

搜索元素时,请通过启用此参数来删除变音符号。

booleanfalse
9

hideDividers

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

booleantrue
10

hideGroups

如果在列表视图组中找不到任何项目,则此参数将隐藏组。

booleantrue

搜索栏回调

S.No回调和描述类型默认
1

onSearch

此方法将在执行搜索时触发回调函数。

function (s)-
2

onEnable

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

function (s)-
3

onDisable

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

function (s)-
4

onClear

单击“clear”元素时,此方法将触发回调函数。

function (s)-

搜索栏属性

S.No属性和描述
1

mySearchbar.params

表示使用object传递的初始化参数。

2

mySearchbar.query

搜索当前查询。

3

mySearchbar.searchList

定义搜索列表块。

4

mySearchbar.container

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

5

mySearchbar.input

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

6

mySearchbar.active

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

搜索栏方法

S.No方法和描述
1

mySearchbar.search(query);

此方法搜索传递的查询。

2

mySearchbar.enable();

它启用了搜索栏。

3

mySearchbar.disable();

它会禁用搜索栏。

4

mySearchbar.clear();

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

5

mySearchbar.destroy();

它会破坏搜索栏实例。

搜索栏JavaScript事件

S.No活动和描述目标
1

search

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

2

clearSearch

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

3

enableSearch

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

4

disableSearch

当搜索栏被禁用,用户点击取消按钮或“搜索栏覆盖”元素时,此事件将被触发。

例子 (Example)

以下示例演示了在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>

输出 (Output)

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

  • 将上面给出的HTML代码search_bar.html文件保存在服务器根文件夹中。

  • 以http://localhost/search_bar.html打开此HTML文件,输出显示如下。

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

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