Hi,
I've come across a few issues when using client-side sorting. I'm managing to work around them all, but just wanted to highlight them, as it would have been ideal if it "just worked like it does server-side" (although perhaps wishful thinking!). I'm working with a grid that has already been in use using server-side binding/sorting, and the query behind the datasource is not 'simple', so for lots of the columns we were using the SortExpression property of columns in markup to get around the fact that it wasn't correct to sort by simply the bound field.
Examples:
-Datafield "ID"; SortExpression "Table.ID" (because of many joins in the actual query)
-DataField "NTextField"; SortExpression "cast(NTextField as nvarchar(3999))" (can't order by an ntext field)
-DataField "Sum"; SortExpression "isnull(number1,0) + isnull(number2,0)"
The issues I've noticed are:
1. The client-side javascript for sorting seems to assume the first space in a sort expression follows a single fieldname to sort on, and that directly afterwards you get the direction if one is present. This isn't the case with some of our sort expressions.
2. It assumes that the fieldname (or maybe it's the columnname) is the same as the first part of the expression up to the space (similar to point 1) which means that when it looks for the sorting buttons it doesn't find them correctly. So using my first example the button might have an id of "gridID__ID__sortAsc" but the sorting functionality looks for "gridID__Table.ID__sortAsc".
3. If there are single quotes in the sort expression (used by sql to indicate strings) then you get javascript errors when the grid first loads, and clicking on the header to sort will cause a postback as it doesn't then call the oncommand event which would normally cancel it in my scenario. Escaping the single quotes (\') did fix the load errors, but then the sortexpression that gets passed to the select is wrong.
4. AllowNaturalSort setting server-side does not carry over to client-side and there doesn't appear to be an equivalent available to set.
5. '+' seems to get removed from the expression altogether (e.g. you might have a field returned that is the sum of 2 underlying numbers).
It would also perhaps be nice to have an OnSort event available?
I've worked around these by removing all our sort expressions from the grid markup and then in the select method swapping out the datafields for the desired sort expression, and hooking into the client-side oncommand event as follows to mimic allownaturalsorting (using telerik RadGridScripts.js as a basis):
Of course, I may just be using it in an unexpected way.
Kind regards,
Zoƫ
I've come across a few issues when using client-side sorting. I'm managing to work around them all, but just wanted to highlight them, as it would have been ideal if it "just worked like it does server-side" (although perhaps wishful thinking!). I'm working with a grid that has already been in use using server-side binding/sorting, and the query behind the datasource is not 'simple', so for lots of the columns we were using the SortExpression property of columns in markup to get around the fact that it wasn't correct to sort by simply the bound field.
Examples:
-Datafield "ID"; SortExpression "Table.ID" (because of many joins in the actual query)
-DataField "NTextField"; SortExpression "cast(NTextField as nvarchar(3999))" (can't order by an ntext field)
-DataField "Sum"; SortExpression "isnull(number1,0) + isnull(number2,0)"
The issues I've noticed are:
1. The client-side javascript for sorting seems to assume the first space in a sort expression follows a single fieldname to sort on, and that directly afterwards you get the direction if one is present. This isn't the case with some of our sort expressions.
2. It assumes that the fieldname (or maybe it's the columnname) is the same as the first part of the expression up to the space (similar to point 1) which means that when it looks for the sorting buttons it doesn't find them correctly. So using my first example the button might have an id of "gridID__ID__sortAsc" but the sorting functionality looks for "gridID__Table.ID__sortAsc".
3. If there are single quotes in the sort expression (used by sql to indicate strings) then you get javascript errors when the grid first loads, and clicking on the header to sort will cause a postback as it doesn't then call the oncommand event which would normally cancel it in my scenario. Escaping the single quotes (\') did fix the load errors, but then the sortexpression that gets passed to the select is wrong.
4. AllowNaturalSort setting server-side does not carry over to client-side and there doesn't appear to be an equivalent available to set.
5. '+' seems to get removed from the expression altogether (e.g. you might have a field returned that is the sum of 2 underlying numbers).
It would also perhaps be nice to have an OnSort event available?
I've worked around these by removing all our sort expressions from the grid markup and then in the select method swapping out the datafields for the desired sort expression, and hooking into the client-side oncommand event as follows to mimic allownaturalsorting (using telerik RadGridScripts.js as a basis):
switch (args.get_commandName()) { |
case 'Sort': // this is to replicate 'allownaturalsorting=false' |
if (sender.get_masterTableView().get_sortExpressions().toString() == '' && args.get_commandArgument() != '') { |
var exp = new Telerik.Web.UI.GridSortExpression(); |
exp.set_sortOrder(Telerik.Web.UI.GridSortOrder.Ascending); |
exp.set_fieldName(args.get_commandArgument()); |
var id1 = String.format("{0}__{1}__SortAsc", sender.get_masterTableView().get_id(), args.get_commandArgument()); |
var id2 = String.format("{0}__{1}__SortDesc", sender.get_masterTableView().get_id(), args.get_commandArgument()); |
if ($get(id1)) { $get(id1).style.display = ""; } |
if ($get(id2)) { $get(id2).style.display = "none"; } |
sender.get_masterTableView().get_sortExpressions().add(exp); |
} |
break; |
} |
bindSearchData(sender); |
Of course, I may just be using it in an unexpected way.
Kind regards,
Zoƫ