I’m using the Telerik MVC Grid component to render a groupable grid populated via ajax. The grid renders fine, sorting and paging works, ajax refresh works, but when I try to do grouping, the rendering gets all screwed up. Attached are screen caps of the grid pre- and post-grouping.
The grid definition is pretty straight-forward:
<div id="tabAccounts" class="tab_content">
@(Html.Telerik().Grid<SharedSimpleAccountListViewModel>()
.Name("AcctGrid")
.Columns(columns =>
{
columns.Bound(x => x.Number)
.HeaderHtmlAttributes(new { @style = "text-align: center;" })
.HtmlAttributes(new { @style = "text-align: center;" });
columns.Bound(x => x.ProviderOrganizationFriendlyName)
.Title("Provider");
columns.Bound(x => x.Name)
.Title("Account Name");
columns.Bound(x => x.BillingLocationName)
.Title("Location");
})
.Groupable()
.DataBinding(db => db.Ajax().Select("CustomerAccounts", "Customers", new { id = Model.Id }))
.Pageable(pager => pager.PageSize(50))
.Sortable()
)
</div>
The controller action is also straight-forward (I won’t paste since it’s just a retrieval from the repository). I’m using the Telerik default theme, so there’s no custom CSS and I’ve confirmed that the required scripts are included in the page.
Inspecting the HTML after the grouping, it appears that there are changes made to the table, but it’s not adding the table row element for the group. Here’s the HTML that exists after a grouping attempt:
<table cellspacing="0">
<colgroup>
<col class="t-group-col">
<col><col><col><col>
</colgroup>
<thead class="t-grid-header">
<tr>
<th class="t-group-cell t-header"> </th>
<th style="text-align: center;" scope="col" class="t-header">
<a href="/Customers/Details/408?AcctGrid-orderBy=Number-asc" class="t-link">Number</a>
</th>
<th scope="col" class="t-header">
<a href="/Customers/Details/408?AcctGrid-orderBy=ProviderOrganizationFriendlyName-asc" class="t-link">Provider</a>
</th>
<th scope="col" class="t-header">
<a href="/Customers/Details/408?AcctGrid-orderBy=Name-asc" class="t-link">Account Name</a>
</th>
<th scope="col" class="t-header t-last-header">
<a href="/Customers/Details/408?AcctGrid-orderBy=BillingLocationName-asc" class="t-link">Location</a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">00002</td>
<td>Acme</td>
<td>Test account 2 </td>
<td class="t-last">Location 2</td>
</tr>
<tr class="t-alt">
<td style="text-align: center;">00001</td>
<td>3M</td>
<td>Test account 1</td>
<td class="t-last">Location 1</td>
</tr>
</tbody>
</table>
Any ideas what might be going on here?
The crux of the issue here is that I was doing AJAX binding but wanted to do grouping and sorting. What that required was a manual sorting process that sorted by the grouped column first, then the other sorted columns. The grid then takes care of setting up the group UI. This was a bit of a challenge for me because my project uses NHibernate as the ORM with a fairly robust service layer that handles querying. I ended up getting the grid to work with a helper class that looks like this:
Note that I’m using ViewModels that have flattened property names, so if my domain object has a property of type
Address, the ViewModel might have a property name ofAddressStreetandAddressCity. MyIPropertyTranslatorinterface specifies a translation process where I can go from the string sort member names in theGridCommandobject to what my domain expects.The
Orderclass in the second-to-last method is an NHibernateOrder. This method is used to generate the list ofOrderobjects that I pass to my service layer when I retrieve the results. TheViewOrderis a utility class I use in the UI. I still need to refactor those last two methods since they are repetitive.Here’s an example of how I use that helper class to pull the
GridModelfor the grid:There’s a bit in there that’s irrelevant to the whole question of grouping, but it’s a real world example, so maybe it will help.