I should add sorting order icons to Ajax WebGrid. The following approach works:
grid.Column("Name", string.Format("Name {0}", grid.SortColumn == "Name" ? grid.SortDirection == SortDirection.Ascending ? "▼" : "▲" : "")
But should be applied to every column.
Is there any other approach to add sorting indicators only to sorted column in one place? For example, modifying the grid after grid.GetHtml().
You have already found the only way using the WebGrid API. However, you can apply sorting arrows after the fact using jQuery. You can use hidden fields to store the WebGrid.SortDirection and WebGrid.SortColumn values. Then you can use the ajaxUpdateCallback parameter to specify a function that is called after the grid has been updated asynchronously, and within that method, set the arrows based on the hidden field values:
function setArrows() {
var dir = $('#dir').val(); //hidden field value
var col = $('#col').val(); //hidden field value
var header = $('th a[href*=' + col + ']');
if (dir == 'Ascending') {
header.text(header.text() + ' ▲');
}
if (dir == 'Descending') {
header.text(header.text() + ' ▼');
}
};
I have just written an article about this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With