我想您可以确定数据源本身的长度,然后调整组合框的大小。这适用于演示项目,但我想取决于样式和填充,您可能需要对尺寸技术进行更多改进。
这决定了元素的大小(同样,如果您在数组中使用对象而不是字符串,您可能需要对其进行一些改进(如果您愿意,可以传递 displayMember)
function determineWidth(ds) {
var l = ds.length, selement = document.createElement('span'), maxwidth = 0, curwidth = 0;
selement.style = 'position: fixed; left: -500px; top: -500px;';
document.body.appendChild(selement);
for (var i = 0; i < l; i++) {
$(selement).html(ds[i]);
curwidth = $(selement).width();
if (curwidth < maxwidth) {
continue;
}
maxwidth = curwidth;
}
document.body.removeChild(selement);
return curwidth + 24;
}
在组合框内,您可以绑定到 dataBound 事件,确定元素的大小,并更新容器和父项以匹配实际大小
$("#combobox").kendoComboBox({
index: 0,
dataSource: {
data: ["One", "Two", "Hundred"]
},
dataBound: function() {
var width = determineWidth(this.dataSource.data());
$('#combobox-container').find('span>.k-dropdown-wrap').width(width).parent().width(width);
}
});
var comboBox = $("#combobox").data("kendoComboBox");
小提琴你可以在这里找到:http: //jsfiddle.net/Icepickle/gLbLtjhf/