Grid Rendering when empty.

19 views
Skip to first unread message

Антон

unread,
Jun 30, 2009, 12:21:28 AM6/30/09
to mvccontrib-discuss
Good day.

Unfortunately, I could not reply to the original thread with this
subject, so I had to start a new one.

How can I make the grid render full header with empty tbody if the
model is empty?

Jeremy Skinner

unread,
Jun 30, 2009, 3:20:51 AM6/30/09
to mvccontri...@googlegroups.com
This isn't something that is built in to the grid. You'll need to create a custom GridRenderer to add this behaviour.

Jeremy

2009/6/30 Антон <uks...@gmail.com>

Антон

unread,
Jul 1, 2009, 4:43:06 AM7/1/09
to mvccontrib-discuss
Is it possible to override somehow HtmlTableGridRenderer to achieve
begaviour I need? All I need is render table header even if the
datasource is empty.

Jeremy Skinner

unread,
Jul 1, 2009, 5:14:18 AM7/1/09
to mvccontri...@googlegroups.com
I've just committed a change that should make this a bit easier (you'll need to build from the latest source from svn - revision 958). 

Now, you can do this:
 

public class MyGridRenderer<T> : HtmlTableGridRenderer<T> where T : class

{

protected override void RenderItems() {

if(IsDataSourceEmpty())

{

RenderEmpty();

return;

}

base.RenderItems();

}


protected override void RenderEmpty() {

int numberOfColumns = GridModel.Columns.Count;

RenderText("<tbody>");

RenderText(string.Format("<tr><td colspan='{0}'>{1}</td></tr>", numberOfColumns, GridModel.EmptyText));

RenderText("</tbody>");

}


protected override bool ShouldRenderHeader() {

return true;

}

}



Previously, the check for whether the header should be rendered was inside the RenderHeader method, meaning that if you wanted to change this behaviour then you had to re-implement all of RenderHeader, but i've now moved it into a ShouldRenderHeader method.

Note that you also have to override RenderItems and explicitly check for whether the datasource is empty. If you don't do this, it will just render the header but with no rows. 

Jeremy

2009/7/1 Антон <uks...@gmail.com>

Антон

unread,
Jul 1, 2009, 6:08:40 AM7/1/09
to mvccontrib-discuss
Thanks, Jeremy.

I will try to use it. And I think I will not want to override
RenderItems since I exactly want to have no rows, just a header.
Reply all
Reply to author
Forward
0 new messages