Created
June 20, 2012 01:12
-
-
Save samandmoore/2957517 to your computer and use it in GitHub Desktop.
Html Helper for creating a link that enables sorting for the column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Helpers; | |
using System.Collections.Specialized; | |
namespace cflat | |
{ | |
public class SortOptions | |
{ | |
public SortOptions() | |
{ | |
this.Column = ""; | |
this.Direction = SortDirection.Ascending; | |
} | |
public string Column { get; set; } | |
public SortDirection Direction { get; set; } | |
} | |
public static class SortLinkExtensions | |
{ | |
public static MvcHtmlString SortLink(this HtmlHelper helper, string linkText, SortOptions sort = null, string column = null, object htmlAttributes = null) | |
{ | |
return SortLink(helper, linkText, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), sort, column); | |
} | |
public static MvcHtmlString SortLink(this HtmlHelper helper, string linkText, IDictionary<string, object> htmlAttributes, SortOptions sort = null, string column = null) | |
{ | |
if (sort == null) | |
{ | |
sort = LoadSortOptionsFromViewData(helper.ViewData); | |
} | |
string formattedColumnName = FormatColumnName(column ?? linkText); // use linkText if there is no column | |
string updatedQs = BuildQueryString(helper.ViewContext.RequestContext.HttpContext.Request.QueryString, formattedColumnName, sort); | |
TagBuilder anchor = new TagBuilder("a"); | |
anchor.MergeAttributes(htmlAttributes, false); | |
anchor.MergeAttribute("href", "?" + updatedQs); | |
anchor.SetInnerText(linkText); | |
if (formattedColumnName == sort.Column) | |
{ | |
anchor.MergeAttribute("class", "active" + " " + sort.Direction.ToString().ToLower()); | |
} | |
return MvcHtmlString.Create(anchor.ToString()); | |
} | |
private static SortOptions LoadSortOptionsFromViewData(ViewDataDictionary viewData) | |
{ | |
object sortObj = null; | |
if (!viewData.TryGetValue("Sort", out sortObj)) | |
{ | |
throw new InvalidOperationException("You must provide SortOptions, either via ViewData or param."); | |
} | |
return (SortOptions)sortObj; | |
} | |
static string FormatColumnName(string column) | |
{ | |
if (string.IsNullOrWhiteSpace(column)) | |
{ | |
throw new InvalidOperationException("Column must have a value"); | |
} | |
column = column.Substring(0, 1).ToUpper() + column.Substring(1); | |
return column; | |
} | |
static string BuildQueryString(NameValueCollection queryString, string column, SortOptions sort) | |
{ | |
System.Collections.Specialized.NameValueCollection qs = HttpUtility.ParseQueryString(queryString.ToString()); | |
qs.Remove("Column"); | |
qs.Add("Column", column); | |
qs.Remove("Direction"); | |
if (sort.Column == column) | |
{ | |
SortDirection dir = sort.Direction; | |
if (dir == SortDirection.Ascending) | |
{ | |
dir = SortDirection.Descending; | |
} | |
else | |
{ | |
dir = SortDirection.Ascending; | |
} | |
qs.Add("Direction", dir.ToString()); | |
} | |
else | |
{ | |
qs.Add("Direction", sort.Direction.ToString()); | |
} | |
return qs.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment