如何为5000个项目优化Kendo UI Combobox的数据源?

时间:2013-05-14 03:45:40

标签: kendo-ui

在我的测试中 - > http://jsfiddle.net/olragon/642c4/12/,KendoUI Combobox无法运行5000项,如何在不调用severside数据源的情况下使其工作,或者这是KendoUI的限制?

HTML

<h3>T-shirt Fabric</h3>
<input id="fabric" placeholder="Select fabric..." />

JS

/**
 * Returns a random integer between min and max
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

$(document).ready(function() {
    var superData = []
    ,   data = [
            { text: "Cotton", value: "1" },
            { text: "Polyester", value: "2" },
            { text: "Cotton/Polyester", value: "3" },
            { text: "Rib Knit", value: "4" }
        ];

    for(var _i=0; _i<5000; _i++) {
        var randomEntry = data[getRandomInt(0,data.length-1)];
        randomEntry.text += '-' + _i;
        randomEntry.value += _i;
        superData.push(randomEntry);
    }

    // create ComboBox from input HTML element
    $("#fabric").kendoComboBox({
        dataTextField: "text",
        dataValueField: "value",
        dataSource: superData,
        filter: "contains",
        suggest: true,
        index: 3
    });
});

更新

1 个答案:

答案 0 :(得分:3)

问题不在于Kendo UI ComboBox,而是在你的循环中。你检查它做了什么(不是你想要它做什么)?我会说有一个错误,因为data[getRandomInt(0,data.length-1)]没有返回一个新元素而是一个引用,所以你将“_i”附加到相同的5个元素,多次构建一个非常长的字符串。

请改为尝试:

for (var _i = 0; _i < 5000; _i++) {
    var randomEntry = data[getRandomInt(0, data.length - 1)];
    var newEntry = {
        text: randomEntry.text + '-' + _i,
        value            : randomEntry.value += _i
    };
    superData.push(newEntry);
}

在此处查看小提琴的修改版本:http://jsfiddle.net/OnaBai/642c4/14/