我试图修改实现无限滚动(没有ajax)的自定义适配器,以显示禁用的结果,但它不起作用。基本上我已经将属性'disabled'添加到对象列表中,并尝试在自定义适配器中使用它,但没有运气。
有什么建议吗?
$.fn.select2.amd.define('select2/data/customAdapter', [ 'select2/data/array', 'select2/utils' ], function(ArrayData, Utils) {
function CustomDataAdapter($element, options) {
CustomDataAdapter.__super__.constructor.call(this, $element, options);
}
Utils.Extend(CustomDataAdapter, ArrayData);
CustomDataAdapter.prototype.current = function(callback) {
var found = [], findValue = null, initialValue = this.options.options.initialValue, selectedValue = this.$element.val(), jsonData = this.options.options.jsonData, jsonMap = this.options.options.jsonMap;
if (initialValue !== null) {
findValue = initialValue;
this.options.options.initialValue = null;
} else if (selectedValue !== null) {
findValue = selectedValue;
}
if (!this.$element.prop('multiple')) {
findValue = [ findValue ];
this.$element.html();
}
for (var v = 0; v < findValue.length; v++) {
for (var i = 0, len = jsonData.length; i < len; i++) {
if (findValue[v] == jsonData[i][jsonMap.id]) {
found.push({
id : jsonData[i][jsonMap.id],
text : jsonData[i][jsonMap.text],
disabled : jsonData[i][jsonMap.disabled]
});
if (this.$element.find("option[value='" + findValue[v] + "']").length == 0) {
var $option = $("<option></option>");
$option.val(jsonData[i][jsonMap.id]);
$option.text(jsonData[i][jsonMap.text]);
if (jsonData[i][jsonMap.disabled]) {
$option.attr("disabled", "disabled");
}
this.$element.append($option);
}
break;
}
}
}
this.$element.find("option").prop("selected", false).removeAttr("selected");
for (var v = 0; v < found.length; v++) {
this.$element.find("option[value='" + found[v].id + "']").prop("selected", true).attr("selected", "selected");
}
if (!found.length && !this.$element.prop('multiple')) {
found.push({
id : jsonData[0][jsonMap.id],
text : jsonData[0][jsonMap.text],
disabled : jsonData[0][jsonMap.disabled]
});
this.$element.html(new Option(jsonData[0][jsonMap.text], jsonData[0][jsonMap.id], true, true));
}
callback(found);
};
CustomDataAdapter.prototype.query = function(params, callback) {
if (!("page" in params)) {
params.page = 1;
}
var jsonData = this.options.options.jsonData, pageSize = this.options.options.pageSize, jsonMap = this.options.options.jsonMap;
var results = $.map(jsonData, function(obj) {
if (new RegExp(params.term, "i").test(obj[jsonMap.text])) {
return {
id : obj[jsonMap.id],
text : obj[jsonMap.text],
disabled : obj[jsonMap.disabled]
};
}
});
callback({
results : results.slice((params.page - 1) * pageSize, params.page * pageSize),
pagination : {
more : results.length >= params.page * pageSize
}
});
};
return CustomDataAdapter;
});
var jsonAdapter = $.fn.select2.amd.require('select2/data/customAdapter');