如何在 jQuery 的自动完成列表中添加固定项目?

Vla*_*adP 1 javascript css jquery

我有一个简单的 jQuery 代码来设置自动完成功能:

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

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

超文本标记语言

<input name="myList" id="myList">
Run Code Online (Sandbox Code Playgroud)

我需要使用永久菜单项在列表的最底部添加分隔线,即:

[sugg    ]
   suggestion 1
   suggestion 2
   suggestion 3
   suggestion 4
   ------------
   my custom link
Run Code Online (Sandbox Code Playgroud)

如果可以添加这个底部项目,那么我可以只滚动建议列表而不滚动底部项目吗?IE:

[sugg    ]
        ^
   suggestion 1
   suggestion 2
   suggestion 3
   suggestion 4
        v
   ------------
   my custom link
Run Code Online (Sandbox Code Playgroud)

Buz*_*nas 5

您可以重写自动完成的_renderMenu方法。例如:

/* 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);
};
Run Code Online (Sandbox Code Playgroud)

我给你举了一个例子。开始输入“co”,您将看到 和COBOLColdFusion但您会看到固定的最后一项ES 2015。如果您开始输入“jav”等,也会发生同样的情况。看一下:

/* 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);
};
Run Code Online (Sandbox Code Playgroud)
$(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);
  };
}
Run Code Online (Sandbox Code Playgroud)
.ui-autocomplete {
  max-height: 125px;
  overflow-y: auto;
  overflow-x: hidden;
}
Run Code Online (Sandbox Code Playgroud)

  • @VladP我已经做了一个解决方法,所以,花点时间理解它,然后进行编辑,使其看起来像你想要的那样!如果我的回答对您有用,请将其标记为已接受的答案。谢谢!:) (2认同)