【问题标题】:How to set a Headline for JQuery autocomplete如何为 JQuery 自动完成设置标题
【发布时间】:2018-05-10 16:31:52
【问题描述】:

对于我的自动完成,我将结果分为 3 个部分“标题”、“公司”、“标签”。
我想为每个部分设置一个标题。
在实践中,我想我想在 li 元素之前添加<div class='ui-optionHeader'>Headline</div>
我想不通,要么标题在每个元素之上,要么根本不起作用。

我做了一个直观的解释——这就是我的目标:


这是我的代码:

$(document).ready(function() {
 src = "{{ route('searchajax') }}";
 $("#search_text").autocomplete({
    source: function(request, response) {
       $.ajax({
           url: src,
           dataType: "json",
           data: {
               term : request.term,
               searchOption : $("#search_option").val()
           },
           success: function(data) {
               response(data);
           }
       });
     },
   select: function(event, ui) {
    $('#search_text').val(ui.item.value);
  },
  minLength: 1,
  open : function(){
  //  $(".ui-autocomplete:visible").css({top:"+=13"});
  }
 })
  .autocomplete( "instance" )._renderItem = function( ul, item ) {

      if(item.option==="title") {
        return $( "<li>" )
          .append( "<div><i class='fas fa-tags'></i> " + item.label + "<br> title </div>" )
          .appendTo( ul );
      }

      else if(item.option==="company"){
        return $( "<li>" )
          .append( "<div><i class='fas fa-building'></i> " + item.label + "<br> company </div>" )
          .appendTo( ul );
      }

      else if(item.option==="tags"){
        return $( "<li>" )
          .append( "<div><i class='fas fa-utensils'></i> " + item.value + "<br> tags </div>" )
          .appendTo( ul );
      }
  };

});

目前的样子:

提前致谢。

【问题讨论】:

  • 感谢您的提示。但我不知何故不能一起建造它。我的类别值是“item.option”。你能给我一个代码示例吗?
  • 需要一个预期数据的样本。

标签: javascript jquery jquery-ui autocomplete


【解决方案1】:

一个例子:

$(function() {
  $.widget("custom.catcomplete", $.ui.autocomplete, {
    _create: function() {
      this._super();
      this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)");
    },
    _renderMenu: function(ul, items) {
      var that = this,
        currentCategory = "";
      $.each(items, function(index, item) {
        var li;
        if (item.category != currentCategory) {
          ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
          currentCategory = item.category;
        }
        li = that._renderItemData(ul, item);
        if (item.category) {
          li.attr("aria-label", item.category + " : " + item.label);
        }
      });
    }
  });
  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_text").catcomplete({
    delay: 0,
    source: data
  });
});
.ui-autocomplete-category {
  background: #009F84;
  color: #fff;
  font-weight: bold;
  padding: .2em .4em;
  margin: 0;
  line-height: 1.5;
}

form #search {
  border: 1px solid #000;
  line-height: 2em;
  margin: 0;
  padding-left: .5em;
  width: 75%;
}

form button {
  background: #009F84;
  border: 0;
  color: #fff;
  font-weight: bold;
  padding: .65em 2em;
  margin-left: 0;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<form>
  <input id="search_text"> <button type="submit">Find</button>
</form>

【讨论】:

  • 太棒了!非常感谢。现在为我工作。
  • @iTzMeRafa 很高兴它有帮助。希望您将其标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
  • 2018-11-05
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多