I implement jQuery UI autocomplete based on the example categories in the documentation. I would like to add the number of results to the "Categories" heading, so instead of displaying "Products" it displays "Products (3)". I know that the _renderMenu function needs to be changed from the example, but it's hard for me to understand how it can be changed. Any help that started me on the right path would be greatly appreciated.
Here is sample code from jQuery UI Autocomplete Categories demo:
<script> $.widget( "custom.catcomplete", $.ui.autocomplete, { _renderMenu: function( ul, items ) { var self = this, currentCategory = ""; $.each( items, function( index, item ) { if ( item.category != currentCategory ) { ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" ); currentCategory = item.category; } self._renderItem( ul, item ); }); } }); </script> <script> $(function() { var data = [ { label: "anders", category: "" }, { label: "andreas", category: "" }, { label: "antal", category: "" }, { label: "annhhx10", category: "Products" }, { label: "annk K12", category: "Products" }, { label: "annttop C13", category: "Products" }, { label: "anders andersson", category: "People" }, { label: "andreas andersson", category: "People" }, { label: "andreas johnson", category: "People" } ]; $( "#search" ).catcomplete({ delay: 0, source: data }); }); </script> <div class="demo"> <label for="search">Search: </label> <input id="search" /> </div>
lukemcd
source share