jQuery Mobile - Filter Text Selectmenu



Description

Set data-filtertext attribute on any of the child(<option> element), which will be considered for filtering.

Example

Following example demonstrates the use of filter text selectmenu in the 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.11.3.min.js"></script>
      <script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
      
      <script>
         $.mobile.document
         .on( "selectmenucreate", ".filterable-select", function( event ) {
            var input,
            selectmenu = $( event.target ),
            list = $( "#" + selectmenu.attr( "id" ) + "-menu" ),
            form = list.jqmData( "filter-form" );
            
            if ( !form ) {
               input = $( "<input data-type='search'></input>" );
               form = $( "<form></form>" ).append( input );
               input.textinput();
               list
               .before( form )
               .jqmData( "filter-form", form ) ;
               form.jqmData( "listview", list );
            }
            
            selectmenu
            .filterable({
               input: input,
               children: "> option[value]"
            })
            
            .on( "filterablefilter", function() {
               selectmenu.selectmenu( "refresh" );
            });
         })
      </script>
   </head>
   
   <body>
      <h2>Filter Text Selectmenu Example</h2>
      <form>
         <div class = "ui-field-contain">
            <label for = "filter">Basic</label>
            
            <select id = "filter" data-native-menu = "false" class = "filterable-select">
               <option value = "de" data-filtertext = "Delhi">Delhi</option>
               <option value = "pu" data-filtertext = "Punjab">Chandigarh</option>
               <option value = "ch" data-filtertext = "TamilNadu">Chennai</option>
               <option value = "mu" data-filtertext = "Maharastra">Mumbai</option>
            </select>
            
         </div>
      </form>
   </body>
</html>

Output

Let's carry out the following steps to see how the above code works −

  • Save the above html code as selectmenu_filter_text.html file in your server root folder.

  • Open this HTML file as http://localhost/selectmenu_filter_text.html and the following output will be displayed.

jquery_mobile_widgets.htm
Advertisements