1

我想弄清楚为什么这个使用 json 数据源的经过大量修改的 json jQueryUI 组合框不会像我预期的那样触发 _renderMenu。总体目标?- 我只是想在页脚中提供一个“计数”,因为我认为目前的显示给出了许多项目不存在的“错觉”

$.widget("ui.combobox", {
    options:{
            dataSource: "",
            dataType: "",
            updateId: "custom_combobox_realvalue"
        },
    _create: function() {
        var self = this,
            select = this.element.hide(),
            selected = select.children(":selected"),
            value = selected.val() ? selected.text() : "";
        var input = this.input = $("<input>").insertAfter(select).val(value).autocomplete({
            delay: 0,
            minLength: 0,
            source: function(request, response) {
                $.ajax({
                    url: self.options.dataSource,
                    type: "POST",
                    dataType: self.options.dataType,
                    data: {
                        maxRows: 12,
                        term: request.term
                    },
                    success: function(data) {
                       response($.map(data.params, function(item) {                         
                            return {
                                label: item.name,
                                name:  item.name,
                                id:    item.id
                            }
                        }));
                    },
                    error: function(jqXHR, exception) {
                        if (jqXHR.status === 0) {
                            alert('Not connect.\n Verify Network.');
                        } else if (jqXHR.status == 404) {
                            alert('Requested page not found. [404]');
                        } else if (jqXHR.status == 500) {
                            alert('Internal Server Error [500].');
                        } else if (exception === 'parsererror') {
                            alert('Requested JSON parse failed.');
                        } else if (exception === 'timeout') {
                            alert('Time out error.');
                        } else if (exception === 'abort') {
                            alert('Ajax request aborted.');
                        } else {
                            alert('Uncaught Error.\n' + jqXHR.responseText);
                        }
                    }
                })
            },
            //selected index
            select: function(event, ui) {
                //debugger;
                $('#' + self.options.updateId).val(ui.item.id);
            },

        }).addClass("ui-widget ui-widget-content ui-corner-left");

        input.data("autocomplete")._renderItem = function(ul, item) {
            return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.name + "</a>").appendTo(ul);
        };

        this.button = $("<button type='button'>&nbsp;</button>").attr("tabIndex", -1).attr("title", "Show All Items").insertAfter(input).button({
            icons: {
                primary: "ui-icon-triangle-1-s"
            },
            text: false
        }).removeClass("ui-corner-all").addClass("ui-corner-right ui-button-icon").click(function() {
            // close if already visible
            if (input.autocomplete("widget").is(":visible")) {
                input.autocomplete("close");
                return;
            }

            // work around a bug (likely same cause as #5265)
            $(this).blur();

            // pass empty string as value to search for, displaying all results
            console.log($(this));
            console.log(input);
            //debugger;
            input.autocomplete("search", input.val());
            input.focus();
        });
    },
    _renderMenu: function( ul, items ) {
            ///this bit never shows
        alert("this _renderMenu call alter box never fires");
        var self = this;
        $.each( items, function( index, item ) {
            if (index == 0)
                ul.append( '<li><input type="checkbox"> I\'m at the top! Choose me!</li>' );
            self._renderItem( ul, item );
            if(index == items.length - 1)
                ul.append( '<li><input type="checkbox"> I\'m at the bottom! Choose me!</li>' );
        });
    },

    destroy: function() {
        this.input.remove();
        this.button.remove();
        this.element.show();
        $.Widget.prototype.destroy.call(this);
    }
});

目前我正在逐步完成它,但我无法弄清楚为什么 _renderMenu 不能在组合框上工作,但它确实适用于香草自动完成。

4

0 回答 0