How to add a fixed item in the jQuery's autocomplete list?

拜拜、爱过 提交于 2021-01-29 05:02:31

问题


I have a simple jQuery code to set autocomplete function:

$(document).ready(function() {
  fInitApp();
});

function fInitApp(){
  $("#myList").autocomplete({
    minLength: 2,
    /*.....,*/
    dataType : "json"
  });
}

HTML

<input name="myList" id="myList">

I need to add a separator line to the very bottom of the list with a permanent menu item, i.e.:

[sugg    ]
   suggestion 1
   suggestion 2
   suggestion 3
   suggestion 4
   ------------
   my custom link

If this bottom item can be added, then can I scroll just suggestion list without bottom item? i.e.:

[sugg    ]
        ^
   suggestion 1
   suggestion 2
   suggestion 3
   suggestion 4
        v
   ------------
   my custom link

回答1:


You can override the autocomplete's _renderMenu method. E.g:

/* saves the current behavior of _renderMenu */
var render = $('#myList').autocomplete('instance')._renderMenu;

/* overrides the default method */
$('#myList').autocomplete('instance')._renderMenu = function(ul, items) {
  /* adds your fixed item */
  items.push({ label: 'my custom link', value: 'my custom link' });
  /* calls the default behavior again */
  render.call(this, ul, items);
};

I've made an example for you. Start typing 'co', and you'll see both COBOL and ColdFusion, but you'll see a fixed last item ES 2015. The same will happen, if you start typing 'jav', etc. Take a look:

$(document).ready(function() {
  fInitApp();
});

function fInitApp() {
  $('#myList').autocomplete({
    minLength: 1,
    source: [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ],
    dataType: "json"
  });

  var render = $('#myList').autocomplete('instance')._renderMenu;

  $('#myList').autocomplete('instance')._renderMenu = function(ul, items) {
    items.push({
      label: 'ES 2015',
      value: 'ES 2015',
      last: true
    });

    render.call(this, ul, items);
  };

  var renderItem = $('#myList').autocomplete('instance')._renderItem;
  $('#myList').autocomplete('instance')._renderItem = function(ul, item) {    
    if (item.last) {
      setTimeout(function() {
        ul.find('li:last-child').css({
          position: 'fixed',
          top: ul.offset().top + (ul[0].scrollHeight === ul[0].clientHeight ? ul.offset().height : ul[0].clientHeight),
          left: ul[0].style.left,
          width: 'calc(145px - 1.4em)',
          border: '1px solid #CCC',
          borderTop: '2px solid #999',
          backgroundColor: '#FFEFFE'
        });
      }, 0);
    }

    return renderItem.call(this, ul, item);
  };
}
.ui-autocomplete {
  max-height: 125px;
  overflow-y: auto;
  overflow-x: hidden;
}
<link href="https://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input name="myList" id="myList">



回答2:


You can use "response" event. For more info see https://api.jqueryui.com/autocomplete/#event-response

//add option in autosuggest list, that would be shown on every search even if search result is null.
$("#myList").autocomplete({
    response: function (event, ui) {
        ui.content.push({ label: "my custom link", value: "my custom link"});
    }
});


来源:https://stackoverflow.com/questions/32878999/how-to-add-a-fixed-item-in-the-jquerys-autocomplete-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!