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

标签 javascript jquery css

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

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

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

HTML

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

我需要在列表的最底部添加一个带有永久菜单项的分隔线,即:

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

如果可以添加这个底部元素,那么我可以只滚动没有底部元素的建议列表吗?即:

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

最佳答案

您可以覆盖自动完成的 _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);
};

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

$(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">

关于javascript - 如何在 jQuery 的自动完成列表中添加固定项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32878999/

相关文章:

javascript - onLoad 后 css3 动画并不总是有效

PHP POST 响应

jquery - 在悬停和鼠标输入时显示 div,但当鼠标不在目标或 div 上时隐藏

html - 为什么单击复选框会在 google chrome 51(flexbox)中加宽 HTML div?

javascript - 代码在 jsfiddle 中有效,但在 chrome 中无效。 - 未捕获的类型错误 : Cannot read property 'style' of undefined

javascript - 动态添加和删除字段后对表单输入数组重新编号

javascript - 如何计算数组列表中的项目数

javascript - 在将按键事件应用于 DOM 之前,如何确定在按键事件之后的值是什么?

html - 水平 2 个 div(菜单 + div)

javascript - 如何在 JavaScript 中获取 JSON 数据数组?