jquery ui autocomplete combobox with categories

后端 未结 4 1967
谎友^
谎友^ 2020-12-10 03:54

I\'m using the jquery ui autocomplete combobox, and it\'s working great but now i\'m getting a bit greedy. I would like to be able to add categories to it. The combobox is

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 04:54

    as you can see in the jQueryUI docs, you have to customize the widget to do that

    _renderMenu: function( ul, items ) {
                var self = this,
                    currentCategory = "";
                $.each( items, function( index, item ) {
                    if ( item.category != currentCategory ) {
                        ul.append( "
  • " + item.category + "
  • " ); currentCategory = item.category; } self._renderItem( ul, item ); }); }

    this its not tested, but should be a good start:

    _renderMenu: function( ul, items ) {
                var self = this,
                    currentCategory = "";
                $.each( items, function( index, item ) {
                    if ( item.parent.attr('label') != currentCategory ) {
                        ul.append( "
  • " + item.parent.attr('label') + "
  • " ); currentCategory = item.parent.attr('label'); } self._renderItem( ul, item ); }); }

    if this doesnt work, maybe you should debug to see whats into the items array that comes as a parameter of _renderMenu.

    a side note: this is called MonkeyPatching, i wouldnt recommend doing this a lot, but since the docs show it, id say do it.

提交回复
热议问题