I'm trying to create a drop down list with grouping where I custom sort the data by group in a non-alphabetical way. I've created a sort function that I run on the data before I pass it to the Kendo DataSource and if I output the array to the console at that point the data is in the correct order. Once I create the Kendo DropDownList though it seems to break the sort and puts back to alphabetical order. I further tested things by turning grouping off on the drop down and in that case the drop down displays the data in the correct order as sorted by my sort function. Any ideas?
The data is an array of objects that look like:
{
Value:
"value"
,
Group:
"group"
}
The sort function is
newData.sort(
function
(a, b) {
var
av, bv;
switch
(a.Group.toLowerCase()) {
case
"group 1"
:
av = 1;
break
;
case
"group 2"
:
av=2;
break
;
default
:
av=3;
break
;
}
switch
(b.Group.toLowerCase()) {
case
"group 1"
:
bv = 1;
break
;
case
"group 2"
:
bv=2;
break
;
default
:
bv=3;
break
;
}
return
av-bv;
});
The function to build the drop down is
$.fn.sampleDD =
function
(options) {
var
ds =
new
kendo.data.DataSource({
transport: {
read:
function
(opts) {
$.ajax({
type:
"GET"
,
url:
"<SharePoint REST Service URL>"
,
dataType:
"json"
,
success:
function
(results) {
//converts the data a bit to account for SharePoint's weird data returns
//runs the sort function referenced above
opts.success(sortedResults);
//sortedResults is created from the conversion and sorting
},
error:
function
(results) {
opts.error(results);
}
});
}
}
});
//listed separately because there's an if statement here that doesn't always group it
ds.group({
field:
"Group"
})
return
this
.kendoDropDownList({
dataTextField:
"Value"
,
dataValueField:
"Value"
,
dataSource: ds,
optionLabel:
"Make a selection"
,
}).data(
"kendoDropDownList"
);
});
};
which is then called by
$(
"#dropDown"
).sampleDD();