为什么动态值不能在自动完成组合框中填充?

8

我参考了很多jQuery和StackOverflow的例子,但没有一个例子涉及将数据库值添加到自动完成组合框中。这就是我在这里提出这个问题的原因。

请问为什么数组值没有填充到自动完成组合框中?这是我的示例代码

   (function($) {
$.widget( "custom.combobox", {
    _create: function() {
      this.wrapper = $( "<span>" )
        .addClass( "custom-combobox" )
        .insertAfter( this.element );

      this.element.hide();
      this._createAutocomplete();
      this._createShowAllButton();
    },

    _createAutocomplete: function() {
      var selected = this.element.children( ":selected" ),
        value = selected.val() ? selected.text() : "";

      this.input = $( "<input>" )
        .appendTo( this.wrapper )
        .val( value )
        .attr( "title", "" )
        .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
        .autocomplete({
          delay: 0,
          minLength: 3,
          source: $.proxy( this, "_source" )
        })
        .tooltip({
          tooltipClass: "ui-state-highlight"
        });

      this._on( this.input, {
        autocompleteselect: function( event, ui ) {
          ui.item.option.selected = true;
          this._trigger( "select", event, {
            item: ui.item.option
          });
        },

        autocompletechange: "_removeIfInvalid"
      });
    },

    _createShowAllButton: function() {
      var input = this.input,
        wasOpen = false;

      $( "<a>" )
        .attr( "tabIndex", -1 )
        .attr( "title", "Show All Items" )
        .tooltip()
        .appendTo( this.wrapper )
        .button({
          icons: {
            primary: "ui-icon-triangle-1-s"
          },
          text: false
        })
        .removeClass( "ui-corner-all" )
        .addClass( "custom-combobox-toggle ui-corner-right" )
        .mousedown(function() {
          wasOpen = input.autocomplete( "widget" ).is( ":visible" );
        })
        .click(function() {
          input.focus();

          // Close if already visible
          if ( wasOpen ) {
            return;
          }

          // Pass empty string as value to search for, displaying all results
          input.autocomplete( "search", "" );
        });
    },

    _source: function( request, response ) {

     var autocompleteList = [];
     autocompleteList=['test1','test2','test3','test4'];
     if(autocompleteList.length>0){
      console.log(autocompleteList) ;
       for(var j=0;j<autocompleteList.length;j++){

        return {
          label:autocompleteList[j],
          value:autocompleteList[j],
          option:this
       }
      }

     }

    },

    _removeIfInvalid: function( event, ui ) {

      // Selected an item, nothing to do
      if ( ui.item ) {
        return;
      }

      // Search for a match (case-insensitive)
      var value = this.input.val(),
        valueLowerCase = value.toLowerCase(),
        valid = false;
      this.element.children( "option" ).each(function() {
        if ( $( this ).text().toLowerCase() === valueLowerCase ) {
          this.selected = valid = true;
          return false;
        }
      });

      // Found a match, nothing to do
      if ( valid ) {
        return;
      }

      // Remove invalid value
      this.input
        .val( "" )
        .attr( "title", value + " didn't match any item" )
        .tooltip( "open" );
      this.element.val( "" );
      this._delay(function() {
        this.input.tooltip( "close" ).attr( "title", "" );
      }, 2500 );
      this.input.data( "ui-autocomplete" ).term = "";
    },

    _destroy: function() {
      this.wrapper.remove();
      this.element.show();
    }
  });
  })(jQuery);

  $(function() {
    $("#combobox").combobox({

    });

    //$("#combobox").closest(".ui-widget").find("input, button").prop("disabled", true);
});

HTML

 <div class="ui-widget">
  <select id="combobox">

</select>
</div>

请提供预期结果数据的示例。autocompleteList会是什么样子? - Twisty
3个回答

4

只更改组合框的选择项目;更改组合框的选择项目会自动更新自动完成

    $('#combobox').empty();
    for (var i = start_index; i < start_index + 4; i++) {
        $('#combobox').append(' <option value=test"' + i + '">test' + i + '</option>');
    }

 $( function() {
   
   var autocompleteList = [];
     autocompleteList=['test1','test2','test3','test4'];
      
   for(var i=0; i<autocompleteList.length; i++){
     
             $('#combobox').append(' <option value="' + autocompleteList[i] + '">' + autocompleteList[i] + '</option>');

   }
   
    $.widget( "custom.combobox", {
      _create: function() {
        this.wrapper = $( "<span>" )
          .addClass( "custom-combobox" )
          .insertAfter( this.element );
 
        this.element.hide();
        this._createAutocomplete();
        this._createShowAllButton();
      },
 
      _createAutocomplete: function() {
        var selected = this.element.children( ":selected" ),
          value = selected.val() ? selected.text() : "";
 
        this.input = $( "<input>" )
          .appendTo( this.wrapper )
          .val( value )
          .attr( "title", "" )
          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
          .autocomplete({
            delay: 0,
            minLength: 0,
            source: $.proxy( this, "_source" )
          })
          .tooltip({
            classes: {
              "ui-tooltip": "ui-state-highlight"
            }
          });
 
        this._on( this.input, {
          autocompleteselect: function( event, ui ) {
            ui.item.option.selected = true;
            this._trigger( "select", event, {
              item: ui.item.option
            });
          },
 
          autocompletechange: "_removeIfInvalid"
        });
      },
 
      _createShowAllButton: function() {
        var input = this.input,
          wasOpen = false;
 
        $( "<a>" )
          .attr( "tabIndex", -1 )
          .attr( "title", "Show All Items" )
          .tooltip()
          .appendTo( this.wrapper )
          .button({
            icons: {
              primary: "ui-icon-triangle-1-s"
            },
            text: false
          })
          .removeClass( "ui-corner-all" )
          .addClass( "custom-combobox-toggle ui-corner-right" )
          .on( "mousedown", function() {
            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
          })
          .on( "click", function() {
            input.trigger( "focus" );
 
            // Close if already visible
            if ( wasOpen ) {
              return;
            }
 
            // Pass empty string as value to search for, displaying all results
            input.autocomplete( "search", "" );
          });
      },
 
      _source: function( request, response ) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
        response( this.element.children( "option" ).map(function() {
          var text = $( this ).text();
          if ( this.value && ( !request.term || matcher.test(text) ) )
            return {
              label: text,
              value: text,
              option: this
            };
        }) );
      },
 
      _removeIfInvalid: function( event, ui ) {
 
        // Selected an item, nothing to do
        if ( ui.item ) {
          return;
        }
 
        // Search for a match (case-insensitive)
        var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
        this.element.children( "option" ).each(function() {
          if ( $( this ).text().toLowerCase() === valueLowerCase ) {
            this.selected = valid = true;
            return false;
          }
        });
 
        // Found a match, nothing to do
        if ( valid ) {
          return;
        }
 
        // Remove invalid value
        this.input
          .val( "" )
          .attr( "title", value + " didn't match any item" )
          .tooltip( "open" );
        this.element.val( "" );
        this._delay(function() {
          this.input.tooltip( "close" ).attr( "title", "" );
        }, 2500 );
        this.input.autocomplete( "instance" ).term = "";
      },
 
      _destroy: function() {
        this.wrapper.remove();
        this.element.show();
      }
    });
 
    $( "#combobox" ).combobox();
      $('.custom-combobox-input').val('');
   var start_index=5;
    $( "#btnUpdate" ).on( "click", function() {
      $('#combobox').empty();
    for (var i = start_index; i < start_index + 4; i++) {
        $('#combobox').append(' <option value=test"' + i + '">test' + i + '</option>');
    }
      
      $('.custom-combobox-input').val('');
      start_index+=5;
    });
  } );
.custom-combobox-toggle {
     padding: 13px!important;
    margin-top: -2px!important;

}
.wrapper {
margin:30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css"><link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<div class="wrapper">
<div class="ui-widget">
  <select id="combobox">

</select>
</div>
<br>
<input type="button" id="btnUpdate" class="btn btn-default" value="update" >
<div>


谢谢!数值正在加载中,但组合框选项丢失,就像输入框一样。 - user2848031
它的功能类似于https://jqueryui.com/autocomplete/#combobox。您能解释一下“组合框选项”吗?当在其中输入内容时,它会进行过滤,单击箭头时会显示所有值。您看过https://codepen.io/peker-ercan/pen/LgxRjv吗? - Ercan Peker
你的代码完美运行!但是我在我的代码中实现相同的功能时,下拉菜单没有显示。虽然我在HTML中使用了select标签,但在加载数据时下拉菜单没有显示。 - user2848031
你能否添加 https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css CSS 文件? - Ercan Peker
是的,我已经包含了以下模块,但是我遇到了错误 " [显示/隐藏消息详情。] TypeError: Bootstrap tooltips require Popper.js (https://popper.js.org)" /jquery-3.3.1.min.js , /jquery-ui.min.js, /bootstrap.min.js ,/popper.min.js" type="module"> - user2848031
你应该按照以下顺序添加jQuery js库、bootstrap js库、popper.js库、jquery-ui js库和jQuery-ui css。 - Ercan Peker

3

我建议使用以下方式:

$(function() {
  $("#combobox").combobox({
    source: function(req, resp) {
      var autocompleteList = [];
      var results = [];
      session.run('MATCH (n) RETURN n.name').then(function(result) {
        $.each(result.records, function(k, r) {
          autocompleteList.push(r._fields[0]);
        });
      });
      if (req.term.length) {
        results = $.ui.autocomplete.filter(autocompleteList, req.term);
      } else {
        results = autocompleteList;
      }
      resp(results);
    },
    select: function(event, ui) {
      $('#log').text('selected ' + $("#combobox").val());
    }
  });
});

这将覆盖source回调并执行必要的活动以收集完整列表,如果用户已输入任何内容,则将列表缩小为与所键入内容匹配的项目。
由于此组合框是自定义的,我认为没有selected回调。 自动完成使用select回调。 因此,我建议首先尝试这个。否则,您可以在代码中创建一个selected回调。
否则,请在初始化ComboBox之前收集列表。希望这有所帮助。

我在初始化ComboBox之前收集了我的列表,但是列表仍然没有出现在ComboBox中。 https://jsfiddle.net/stackoverflow/1sogae3d/23/ - user2848031
@user2848031,你能提供一份数据示例吗?你是如何收集它的? - Twisty

2

这里有一个关于如何实现的例子,我创建了一个数组来进行循环,因为我无法访问会话对象。

var autocompleteList = [];

$(document).ready(function() {

 var records = ['test', 'test 2', 'test 3']

    records.forEach(function(record) {
      autocompleteList.push(record);
    });
    
    $( "#tags" ).autocomplete({
      source: autocompleteList
    });
    
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script type="text/javascript" src="//code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


谢谢Adam。抱歉,我正在寻找带有组合框的自动完成功能 https://jqueryui.com/resources/demos/autocomplete/combobox.html - user2848031
你需要重写 _source 函数,并让它返回你的 autocompleteList 对象。 - Adam H
添加了所有库之后,我遇到了一个错误:TypeError: $(...).combobox不是一个函数。 - user2848031
这通常意味着jQueryUI包含有误,请仔细检查这些引用。 - Adam H
这是我的代码:https://jsfiddle.net/stackoverflow/1sogae3d/23/ 请告诉我这里缺少了什么。 - user2848031
是的,在那里你没有引用jQuery或jQueryUi的任何参考。 - Adam H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接