I've got a simple thing for populating the datasource into a KendoDropdownList. But I'm doing something wrong and research through all kinds of forums didn't help me.
I want to populate the kendoDropdownList automatically with years. for now 2016,2017 and 2018 and when we are next year, it automatically needs to add 2019. (so not hardcoded)
Here's my code I've got until now:
<script>
$(document).ready(function () {
var startYear=2016;
var currentYear = new Date().getFullYear(), years = [];
var ddl = new kendo.data.DataSource({
while( startYear <= currentYear) {
ddl.dataSource.add({
text: startYear.toString() ,
value: startYear.toString()
});
years.push(startYear++);
}
});
$("#dropdownYear").kendoDropDownList({
dataTextField: "text",
dataValueField: "value",
dataSource: ddl
});
});
</script>
<body>
<div class="demo-section k-content" style="padding-top:10px;padding-bottom:35px;">
<input id="dropdownYear" />
</div>
</body>
Here's also a link to a dojo: https://dojo.telerik.com/ASuWAkuB
Any help is very welcome.
Thanks
You have to build the data and then pass it as datasource. And not add it to the data source and then add the "HTML" to datasource, something like this:
$(document).ready(function () {
var startYear=2016;
var currentYear = new Date().getFullYear();
var data = [];
while(startYear <= currentYear) {
var newItem = {
text: startYear.toString(),
value: startYear.toString()
};
data.push(newItem);
startYear++;
}
$("#dropdownYear").kendoDropDownList({
dataTextField: "text",
dataValueField: "value",
dataSource: data
});
});
Here you can find a working Dojo: Working dojo
Cheers,