This is a migrated thread and some comments may be shown as answers.
[BUG Q1-2011] GridHeaderCellBuilder should not overwrite "t-header" with css class from HtmlHeaderAttributes (fix included)
2 Answers 21 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Parker
Top achievements
Rank 1
Parker asked on 31 Mar 2011, 07:33 PM
There is currently a bug in the GridheaderCellBuilder (Q1-2011) that overwrites the "t-header" class if you specify a custom CSS class in the HtmlHeaderAttributes in GridColumnBase.

The fix is very straightforward and involves calling Attributes(...) before you add the "t-header" class.
A unit test / fix is below:

Test:
public class GridHeaderCellBuilderTests
{
    [Fact]
    public void Should_apply_extra_css_classes()
    {
        const string key = "class";
        const string value = "bar";
 
        var headerAttributes = new Dictionary<string, object> { { key, value } };
        var cell = new GridHeaderCellBuilder(headerAttributes, delegate { }).CreateCell();
        cell.Attribute("class").ShouldEqual("bar t-header");
    }
}

FIX: The current Q1-2011 version first adds the cssclass and then calls Attributes(...)
public class GridHeaderCellBuilder : IGridCellBuilder
{
    /*........ code ommitted ......*/
 
    protected IHtmlNode CreateContainer()
    {
        // call Attributes(...) first so that the subsequent calls to AddClass and Attribute do not override UIPrimitives.Header / the "scope" properties
        var th = new HtmlElement("th").Attributes(htmlAttributes)
                                   .AddClass(UIPrimitives.Header)
                                  .Attribute("scope", "col");
         
        return th;
    }
}

A similar problem exists when rendering the Data Grid cells via javascript. If you add a css-class to the last cell in the table via HtmlAttributes in GridColumnBase.  In this case the "t-last" class is not rendered and is overwritten with the custom css class.
The current implementation adds the class via javascript:
// File: telerik.grid.js
bindData: function (data, html, groups) {
 
    /* ........ code omitted ......... */
    html.cat('<td')
        .cat(column.attr)
        .catIf(' class="t-last"', i == len - 1)
        .cat('>')
        .cat(s);
    /* ........ code omitted ......... */
     
}

There are several ways of fixing this, I chose to modify the GridColumnSerializer class so that it'd add the "t-last" class to the serialized attributes.  It wasn't obvious as to how to update the GridColumnSerializerTests to address this:
class GridColumnSerializer : IGridColumnSerializer
{
   /* ....... code omitted ....... */
    public virtual IDictionary<string, object> Serialize()
    {
        // e need to add back the "t-last" class to the Last columns so that if the HtmlAttributes includes class, it does not overwrite "t-last"
        this.AddLastClassToHtmlAttributes(column);
        /* ....... code omitted ....... */
    }
 
    private void AddLastClassToHtmlAttributes(IGridColumn gridColumn)
    {
        if (gridColumn.IsLast)
        {
            object oPreviousClass = null;
            string sLastClass = null;
            if (gridColumn.HtmlAttributes.TryGetValue("class", out oPreviousClass))
            {
                sLastClass = String.Format("{0} {1}", HttpUtility.HtmlAttributeEncode(oPreviousClass != null ? oPreviousClass.ToString() : String.Empty), UIPrimitives.Last);
            }
            else
            {
                sLastClass = UIPrimitives.Last;
            }
            gridColumn.HtmlAttributes.Merge(new { @class = sLastClass }, true);
        }
    }
}

Cheers
Parker

2 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 01 Apr 2011, 11:38 AM
Hello Parker,

Thank you for your feedback and suggestions. Actually, we have fixed this problem on March 17 and the resolution is available in the internal builds uploaded later on.

Kind regards,
Dimo
the Telerik team
0
Parker
Top achievements
Rank 1
answered on 01 Apr 2011, 02:54 PM
Thanks Dimo,

Looking forward to the SP1 release!
Tags
Grid
Asked by
Parker
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Parker
Top achievements
Rank 1
Share this question
or