Reputation: 674
I know this question has been asked many times and there is so many links in Google. But my bad luck, nothing worked in my case.
I've a Kendo dropdownlist
which I'm populating using Json
data. The DB is sending this details based on Order by Name
. So the Index
in dropdownlist
is random but ordered by names.
E.g.
<select id="ddlNames" data-role="dropdownlist">
<option value="-1">Select Names</option>
<option value="5 ">AAA</option>
<option value="9 ">BBB</option>
<option value="6 ">CCC</option>
<option value="3 ">DDD</option>
<option value="7 ">EEE</option>
<option value="4 ">FFF</option>
<option value="8 ">GGG</option>
<option value="1 ">HHH</option>
<option value="2 ">III</option>
</select>
I've to change the Index
of this dropdownlist
on click of a button
.
$.ajax({
url: "Test.aspx/DDL_List",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
var results = jQuery.parseJSON(data.d);
var appenddata = "<option value = '-1'> Select Names </option>";
$.each(results.Table, function (i, value) {
appenddata += "<option value = '" + value.NameID + " '>" + value.Name + " </option>";
});
$('#ddlNames').html('');
$('#ddlNames').html(appenddata);
$('#ddlNames').kendoDropDownList();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
On click of button, I'm using below code to set the Index
of Kendo dropdownlist
.
Problem : if the result[0].NameID =1
, it is setting the name which is found on top of the list i.e AAA
, but not based on Index
which should be HHH
. I tried both Value
and Select
var ddlNames= $("#ddlNames").data("kendoDropDownList");
if (result[0].NameID != null) {
var iNameID = parseInt(result[0].NameID);
ddlNames.value(iNameID);
//ddlNames.select(iNameID);
}
else {
ddlNames.value(-1);
}
Please guide.
Upvotes: 1
Views: 1580
Reputation: 336
success: function (data) {
var results = jQuery.parseJSON(data.d);
$("#ddlNames").data("kendoDropDownList").dataSource.data(results);
}
try like this...
Upvotes: 1