Listview 列表视图远程自动完成
描述
filterablebeforefilter 事件用于远程自动完成数据源。 当用户输入搜索查询时,会动态显示一个列表。
示例
以下示例演示了在 jQuery Mobile 中使用远程自动完成列表视图。
<!DOCTYPE html> <html> <head> <meta name = "viewport" content = "width = device-width, initial-scale = 1"> <link rel = "stylesheet" href = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src = "https://code.jquery.com/jquery-1.10.2.min.js"></script> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <script> $( document ).on( "pagecreate", "#remote", function() { $("#flip").on("change", function() { if (this.checked) { var inp = $( "#autocomplete" ).filterable( "option", "input" ); $(inp).val("Belgaum").trigger("change"); } }); $( "#autocomplete" ).on( "filterablebeforefilter", function ( e, data ) { var $ul = $( this ), $input = $( data.input ), value = $input.val(), html = ""; $ul.html( "" ); if ( value && value.length > 2 ) { $ul.html( "<li> <div class = 'ui-loader'> <span class = 'ui-icon ui-icon-loading'></span> </div> </li>" ); $ul.listview( "refresh" ); $.ajax ({ url: "http://gd.geobytes.com/AutoCompleteCity", dataType: "jsonp", crossDomain: true, data: { q: $input.val() } }) .then( function ( response ) { $.each( response, function ( i, val ) { html + = "<li>" + val + "</li>"; }); $ul.html( html ); $ul.listview( "refresh" ); $ul.trigger( "updatelayout"); }); } }); }); </script> </head> <body> <div data-role = "page" id = "remote"> <h2>Example of remote autocomplete</h2> <form> <label for = "flip"></label> <input type = "checkbox" data-role = "flipswitch" id = "flip" /> </form> <form class = "ui-filterable"> <input id = "filter-input" data-type = "search" /> </form> <ul id = "autocomplete" data-role = "listview" data-inset = "true" data-filter = "true" data-input = "#filter-input"></ul> <div> </body> </html>
输出
让我们执行以下步骤,看看上面的代码是如何工作的 −
将上述 html 代码保存为服务器根文件夹中的 listview_remote_autocomplete.html 文件。
将此 HTML 文件打开为 http://localhost/listview_remote_autocomplete.html,将显示以下输出。