【问题标题】:jQuery UI custom AutoComplete - `_renderItem` and `_renderMenu` not workingjQuery UI 自定义自动完成 - `_renderItem` 和 `_renderMenu` 不起作用
【发布时间】:2016-05-05 07:52:04
【问题描述】:

我使用了组合框演示中的一些代码,现在我试图向列表项添加一些类,_renderItem 和 _renderMenu,没有效果。

代码(有一些不相关的行,以确保我没有遗漏任何内容)

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({
        autoFocus: true,
        response: function (event, ui) {
            if (ui.content.length == 0) {
                    ui.content.push({
                        label: "new value: " + $(this).val(),
                        value: $(this).val(),
                        id: 0
                    });
            }
        },
        _renderItem: function (ul, item) {
            return $("<li>")
                .addClass("Please work")
                .attr("data-value", item.value)
                .append(item.label)
                .appendTo(ul);
        },
        _renderMenu: function (ul, items) {
            var that = this;
            $.each(items, function (index, item) {
                that._renderItemData(ul, item);
            });
            $(ul).find("li:odd").addClass("odd");
        },
        delay: 0,
        minLength: 0,
        source: $.proxy(this, "_source")
    })
    .tooltip({
        tooltipClass: "ui-state-highlight"
    });

【问题讨论】:

    标签: jquery jquery-ui jquery-autocomplete


    【解决方案1】:

    我从来没有以这种方式使用过扩展,我也说不出它为什么不起作用(我想应该如此)。

    无论如何,尝试使用标准方式,在 create 回调:

    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({
            autoFocus: true,
            response: function (event, ui) {
                if (ui.content.length == 0) {
                        ui.content.push({
                            label: "new value: " + $(this).val(),
                            value: $(this).val(),
                            id: 0
                        });
                }
            },
            delay: 0,
            minLength: 0,
            source: $.proxy(this, "_source"),
            create: function() {
                $(this).data('ui-autocomplete')._renderItem  = function (ul, item) {
                  return $("<li>")
                    .addClass("Please work")
                    .attr("data-value", item.value)
                    .append(item.label)
                    .appendTo(ul);
                };
    
            }
        })
        .tooltip({
            tooltipClass: "ui-state-highlight"
        });
    

    看到这个FIDDLE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 2017-03-26
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      相关资源
      最近更新 更多