jQuery Mobile - 自定義過濾器回撥



說明

就像列表檢視的擴充套件一樣,可以在 filterablebeforefilter 事件提供回撥函式來對過濾器進行過濾甚至完全覆蓋過濾器。在過濾器實際觸發之前,它會延遲 250 毫秒。這避免了在使用者鍵入時過濾函式多次執行。

要為所有可過濾部件建立新的預設值,請在 mobileinit 訊號處理程式中設定自定義過濾函式,覆蓋部件原型中的 filtercallback 選項,如下面的程式碼所示。

$( document ).one( "mobileinit", function() {
   $.mobile.filterable.prototype.options.filterCallback = function( index, searchValue ) {
      // The this keyword in this function refers to the element
      // for the code which decide whether or not to filter.
      // The true return value shows that the element referred
      // to by this keyword is to be filtered.
      // If returns false specifies that the item is to be displayed.
      //
      // Custom filtering logic goes here.
   });
});

可以透過設定 filterCallback 選項,為單個可過濾部件設定自定義過濾函式,如下面的程式碼所示。

$.mobile.document.one( "filterablecreate", "#myFilterable", function() {
   $( "#myFilterable" ).filterable( "option", "filterCallback", 
      function( index, searchValue ) {
      // The above example explains the callback function's signature.
      //
      // Custom filtering logic goes here.
   });
});  

要完全覆蓋過濾器(例如,在從伺服器端或 localStorage 載入資料時),請繫結到 filterablebeforfilter 事件,如下面的程式碼所示。

$( ".selector" ).on( "filterablebeforefilter", function( e, data ) {
   var value;

   e.preventDefault();
   value = data.input.value;
   // trigger own request to database
});
jquery_mobile_widgets.htm
廣告
© . All rights reserved.