【问题标题】:jQuery autocomplete _renderItem issue with multiple inputs to trigger autocomplete带有多个输入以触发自动完成的jQuery自动完成_renderItem问题
【发布时间】:2012-07-10 01:40:32
【问题描述】:

快速解释:我有 3 个输入 first_namelast_namecontact_number。它们都有类名autocomplete。例如

<input type="input" name="first_name" id="first_name" class="autocomplete">
<input type="input" name="last_name" id="last_name" class="autocomplete">
<input type="input" name="contact_number" id="contact_number" class="autocomplete">

我使用自动完成类作为启动 jQuery UI 自动完成功能的选择器(请参见下面的代码),这样填充其中任何一个都将导致使用所有 3 个输入进行 ajax 搜索。因为我使用所有 3 个字段进行搜索,所以结果必须位于特定位置(而不是通常情况下在每个输入下),所以我使用带有表格的 div,其中依次显示结果。这可以通过覆盖内部 _renderItem 函数来实现(参见下面的代码)。

这一切都很好,但是,仅适用于表单中的第一个输入,例如名。其他输入都显示在其各自输入下方的下拉列表中。后续输入似乎忽略了 _renderItem 覆盖。我已经尝试交换输入,无论哪个第一个正常工作,其他人都没有。关于如何解决这种行为的任何建议?

    $(document).ready(function() {
        $(".autocomplete").autocomplete({
            search: function(event, ui) {
                $("#autocompleteoutput table tbody").empty();
                $("#autocompleteoutput").css("display", "inline");
            },
            source: function(request, response) {
                jQuery.ajax({
                    url: "'.site_url('reservations/search_customer').'",
                    type: "post",
                    dataType: "json",
                    data: {
                        first_name: $("#first_name").val(),
                        last_name: $("#last_name").val(),
                        contact_number: $("#contact_number").val(),
                        '.$this->security->get_csrf_token_name().' : "'.$this->security->get_csrf_hash().'"
                    },
                    success: function(data) {
                        response(jQuery.map(data, function(item) {
                            return {
                                diner_id: item.diner_id,
                                first_name: item.first_name,
                                last_name: item.last_name,
                                dialing_code: item.dialing_code,
                                country_id: item.country_id,
                                contact_number: item.contact_number,
                                email: item.email
                            }
                        }))
                    }
                })
            }
        })
        .data( "autocomplete" )._renderItem = function( ul, item ) {
            return $( "<tr class=\"customerselect\" data-dinerid=\"" + item.diner_id + "\" data-fname=\"" + item.first_name + "\" data-lname=\"" + item.last_name + "\" data-countryid=\"" + item.country_id + "\" data-num=\"" + item.contact_number + "\" data-email=\"" + item.email + "\"></tr>" )
                .data( "item.autocomplete", item )
                .append( "<td><span class=\"icon-user\">" + item.first_name + " " + item.last_name + "</span></td>")
                .append( "<td><span class=\"icon-phone\">(+" + item.dialing_code + ") " + item.contact_number + "</span></td>" )
                .append( "<td><span class=\"icon-mail\">" + item.email + "</span></td>" )
                .appendTo($("#autocompleteoutput table tbody"));
        };
    });

【问题讨论】:

    标签: jquery autocomplete


    【解决方案1】:

    这里的 .data("autocomplete") 只返回第一个元素的自动完成数据。分配自动完成控件后,请尝试对每个输入分别使用此方法。

    我的意思是这样

     function myRenderFunc(ul,item){
         // code for the _renderItem method
     }
    
     $(".autocomplete").autocomplete({
            //your autocomplete code
     })
    
     $('#first_name').data( "autocomplete" )._renderItem = myRenderFunc;
     $('#last_name').data( "autocomplete" )._renderItem = myRenderFunc;
     $('#contact_number').data( "autocomplete" )._renderItem = myRenderFunc;
    

    我现在就试过了,它对我有用。应该也适合你。

    【讨论】:

      【解决方案2】:

      这也适用于我,特别是如果输入元素是动态生成的:

      $('.autocomplete').each(function() {
          $(this).data('uiAutocomplete')._renderItem = function (ul, item) {
                // render item code
          };
      });
      

      【讨论】:

        【解决方案3】:

        以上两个答案都为我指明了正确的方向,但最终对我来说看起来像这样(包括对 jquery-ui 的一些更新):

        $('.autocomplete').each(function(i, el) {
            $(el).data('ui-autocomplete')._renderItem = function(ul, item) {
                // Do stuff
            };
        });
        

        【讨论】:

        • 这个更整洁+1
        【解决方案4】:

        谢谢你解决了我的问题。我通过'create'函数实现了它

        $.extend(proto, {
            _initSource: function () {
                if (this.options.html && $.isArray(this.options.source)) {
                    this.source = function (request, response) {
                        response(filter(this.options.source, request.term));
                    };
                } else {
                    initSource.call(this);
                }
            },
        
            create: function () {
                $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
                    return $("<li></li>")
                        .data("item.autocomplete", item)
                        .append($("<a></a>")[this.options.html ? "html" : "text"](item.EmployeeHtml))
                        .appendTo(ul);
            };
        }
        

        【讨论】:

          【解决方案5】:
          $('.autocomplete').each(function(i, el) {
              $(el).autocomplete({
                  source: function(request, response) {
                      $.ajax({
                          url: "/Actual/Provision",
                          type: 'POST',
                          dataType: "json",
                          data: {'term': request.term},
                          success: function(data) {
                              response(data);
                          },
                          error: function(data) {
                              alert('failed to load autocomplete source data');
                          }
                      });
                  },
                  minLength: 1,
                  select: function(event, ui) {
                      $(this).parent().find("input[name='submission_provision[]']").val(ui.item.name);
                      $(this).parent().find("input[name='submission_part_age[]']").val(ui.item.part_age);
                      $(this).parent().find("input[name='submission_part_smr[]']").val(ui.item.part_smr);
                      $(this).parent().find("input[name='submission_labour_age[]']").val(ui.item.labour_age);
                      $(this).parent().find("input[name='submission_labour_smr[]']").val(ui.item.labour_smr);
          
                      return false;
                  },
                  change: function(event, ui) {
                      if ($(this).val() == '') {
                          $(this).parent().find(".provision-ref").val("");
                      }
                  }
          
              }).data("ui-autocomplete")._renderItem = function(ul, item) {
                  console.log(ul);
                  return $("<li>")
                      .append("<a>" + item.name + "</a>")
                      .appendTo(ul);
              };
          });
          

          【讨论】:

            猜你喜欢
            • 2018-06-17
            • 2011-01-01
            • 2014-08-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-11
            • 1970-01-01
            相关资源
            最近更新 更多