【问题标题】:jquery ui autocomplete with categories displayed horizontally, not verticallyjquery ui自动完成,类别水平显示,而不是垂直显示
【发布时间】:2019-03-26 21:47:43
【问题描述】:

是他们现有的插件,或者是可以使类别水平显示的代码,例如表头:

http://jqueryui.com/demos/autocomplete/#categories

还是自己从头开始更容易?

【问题讨论】:

  • 你遇到过这个问题吗?
  • @damd 我开始编写自己的 jquery 插件,但还没有走多远。保持回归的意义。
  • 我现在正在做同样的事情。我想我现在已经有点上路了。如果我想出你可以使用的东西,我会告诉你的。
  • @damd 是你在 github 或类似项目上的项目......我可以帮忙。
  • 恐怕不行,由于种种原因,我真的做不到。

标签: javascript autocomplete jquery-autocomplete jquery-ui-autocomplete


【解决方案1】:

你很可能在某个地方有一个冲突的 css。

我上周遇到了这个问题并解决了。

(对我而言)有冲突的代码是这个:

ul { 列表样式:无; }

这很可能包含在您的 style.css 文件中或网页中的某处。

【讨论】:

    【解决方案2】:

    给你

    https://codepen.io/nitinmendiratta/pen/aMMeOz

    CSS

    #search {
      width: 500px;
    }
    
    .ui-autocomplete {
      display: flex;
      width: auto !important;
    }
    
    .ui-autocomplete-category {
      font-weight: bold;
      padding: .2em .4em;
      margin: .8em 0 .2em;
      line-height: 1.5;
    }
    .ui-autocomplete-category ul{
      padding:0;
    }
    .submenu {
      font-weight: normal;
    }
    
    /* ADDED STYLE */
    .ui-autocomplete>li>div>ul{
      display: block !important;
      position: relative !important;
      border: 0 !important;
    }
    span.ui-menu-icon{
      display:none !important;
    }
    
    /* ADDED for the SO snippet only !! Not needed on CodePen */
    .ui-autocomplete>li{
      display: inline-block !important;
    }
    

    HTML

    <label for="search">Search: </label>
    <input id="search">
    <div class="ui-widget" style="margin-top:2em; font-family:Arial">
      Result:
      <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
    </div>
    

    JS

    $.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, submenuUl;
          if ( item.category != currentCategory ) {
            var elt = $("<li class='ui-autocomplete-category'>" + item.category + "</li>");
            var submenu = $("<div class='submenu "+ item.category +"'><ul></ul></div>");  // Added <ul></ul>
            elt.append(submenu);
            ul.append(elt);
            currentCategory = item.category;
          }
          submenuUl = ul.find("."+item.category+" ul"); // added +" ul"
          li = that._renderItemData(submenuUl, item );
          if ( item.category ) {
            li.attr( "aria-label", item.category + " : " + item.label ).addClass("ui-menu-item"); // Added the addClass()
          }
        });
      }
    });
    
    
    // Actual Code
    $(function() {
      var data = [
        { 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" ).catcomplete({
        delay: 0,
        source: data,
        select: function(item, ui){ // Added item, ui --- Arguments were missing.
          console.log(ui.item.value);
          $( "#search" ).val( ui.item.value );
         $( "<div>" ).text( JSON.stringify(ui.item) ).prependTo( "#log" );
          return false;
        }
      });   
    });
    

    【讨论】:

      猜你喜欢
      • 2022-06-16
      • 2021-01-18
      • 2012-11-10
      • 2020-08-26
      • 2011-12-22
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多