Update to remove sorting

Jul 31, 2015 at 1:51 AM
Edited Aug 11, 2015 at 8:19 PM
One of my pet peeves with GridMVC is the inability to remove the sort - I added some simple code to SortColumnHeaderRenderer.cs at line 49

From this:
            var sortArrow = new TagBuilder("span");
            sortArrow.AddCssClass("grid-sort-arrow");
            sortTitle.InnerHtml += sortArrow.ToString();
to this:
            //get our URL w/o sorting
            string url = GetRemoveSortUrl(column.Name);
            //create our A tag
            var sortArrorLink = new TagBuilder("a");
            string urlRemove = GetRemoveSortUrl(column.Name);
            sortArrorLink.Attributes.Add("href", urlRemove);
            sortArrorLink.Attributes.Add("style", "text-decoration: none;");
            //create our span tag
            var sortArrow = new TagBuilder("span");
            sortArrow.AddCssClass("grid-sort-arrow");
            //add span to A
            sortArrorLink.InnerHtml += sortArrow.ToString();
            //Add A and Span
            sortTitle.InnerHtml += sortArrorLink.ToString();

With a helper function:
    private string GetRemoveSortUrl(string columnName)
    {
        //determine current url:
        var builder = new CustomQueryStringBuilder(_settings.Context.Request.QueryString);
        string url = builder.GetQueryStringExcept(new[]
                {
                    GridPager.DefaultPageQueryParameter,
                    _settings.ColumnQueryParameterName,
                    _settings.DirectionQueryParameterName
                });
        if (string.IsNullOrEmpty(url)) {
            url = "?";
        }
        return url;
    }
Now you just click on the arrow to remove the sort - not very intuitive, but it works.