Dynamic loading of grid?

Dec 12, 2013 at 11:46 PM
I have been rummaging around your demo apps and have learned a lot about using jqGrid with MVC and I thank you for that. The next thing I want to try to do is tailor the grid to be able to have the columns and their settings dynamically defined. The goal would be to have a Partial View that contains the grid which I could call from any view and pass any model's data and the grid would properly display it. Is this possible to do using your extension, and is there an example of this in the strongly typed application demo? (I looked through the code but just could not figure it out well enough)
Coordinator
Dec 14, 2013 at 9:11 PM
You need to explain your requirement in a little bit more details as current description is a little bit unclear - maybe some design sketch or pseudo-code representation.

From what I understood so far this will probably require resolving generics at runtime through reflection. This is quite a complex corner of C# and .Net Framework so you really need to make sure you want to go there.
Dec 14, 2013 at 10:47 PM
I am usually not good at explaining my thoughts but I will give it ago. I would like to create a single partial view that has both the jqGrid JavaScript and the TABLE/DIV tags so it can be called by any normal view. In the Controller of those views I would like to somehow generate all the needed parameters and values for colNames and colModel via C# code. That could would be passed up to the Partial View that contains the jqGrid JavaScript for display.
Coordinator
Dec 16, 2013 at 9:17 AM
I have an idea on how this might work with the strongly typed helper, but I need to test it.

I will get back to you as soon as I will do that (I'm thinking later today).
Dec 16, 2013 at 3:31 PM
That is great to hear and look forward to what you find but please don't rush just for me. And thank you once again for such great documentation and plug for jqGrid
Coordinator
Dec 17, 2013 at 2:41 PM
So here we go...

First you will need a non-generic representation of JqGridHelper - I suggest following interface:
public interface IJqGridHelperWrapper
{
    MvcHtmlString GetTableHtml();

    MvcHtmlString GetPagerHtml();

    MvcHtmlString GetHtml();

    MvcHtmlString GetJavaScript();
}
Now you need an implementation of this interface. For this purpose I suggest a generic class wrapping JqGridHelper:
public class JqGridHelperWrapper<T> : IJqGridHelperWrapper
{
    private readonly JqGridHelper<T> _wrappedHelper;

    public JqGridHelperWrapper(JqGridHelper<T> wrappedHelper)
    {
        _wrappedHelper = wrappedHelper;
    }

    public MvcHtmlString GetTableHtml()
    {
        return _wrappedHelper.GetTableHtml();
    }

    public MvcHtmlString GetPagerHtml()
    {
        return _wrappedHelper.GetPagerHtml();
    }

    public MvcHtmlString GetHtml()
    {
        return _wrappedHelper.GetHtml();
    }

    public MvcHtmlString GetJavaScript()
    {
        return _wrappedHelper.GetJavaScript();
    }
}
Thanks to that you can now created a strongly typed partial view like this (sample using Razor):
@model IJqGridHelperWrapper
@Model.GetHtml()
<script type="text/javascript">
    @Model.GetJavaScript()
</script>
Which you can use from your controlles like this:
public ActionResult Categories()
{
    JqGridHelper<Product> grid = new JqGridHelper<Product>("products",
        ...
    );

    return PartialView("GridPartial", new JqGridHelperWrapper<Product>(grid));
}
Or form other views, you only need to pass some implementation of IJqGridHelperWrapper (you can for example pass it as part of the model from controller to the view and then use it in the view to call partial view).

If your JqGridHelper initialization code is always the same (or part of it is) you can move its creation to generic method to avoid code repetition. The wrapper parts will no longer be needed after v6.40 release as I'm going to introduce IJqGridHelper interface.

I hope this will solve your problem (or at least will guide in proper direction)
Marked as answer by Eagle_f90 on 12/18/2013 at 3:05 PM
Dec 17, 2013 at 7:18 PM
Thanks so much, I will begin playing with this tonight and see how things work out.
Dec 18, 2013 at 11:48 PM
I was able to get this up and running with a very basic layout with only one change.
<script type="text/javascript">
    $( document ).ready( function ()
    {
        @Model.GetJavaScript()
    } );
</script>
Now I just have to start playing with the parameters for JqGridHelper. Thanks so much, this is going to creating the many, many Grids on my site much easier.
Coordinator
Dec 19, 2013 at 10:49 AM
I'm happy to hear that.
Mar 23, 2014 at 3:22 PM
Hi TPeczek,

I just downloaded v6.4.1 and noticed it has an IJqGridHelper but I did not see anything about it in the ChangeLog. From what I can tell of the included interface it looks to be an enhanced version of what you gave me here. If so can I use this interface in place of the one I created, and can you tell me what the GilterGridID, ID, PagerId, and GetFilterGridHtml variables/function do?
Coordinator
Mar 23, 2014 at 10:29 PM
Hi,

Yes I've managed to squize it into one of past releases and you can safely use it, you can find the documentation for methods and properties here: http://www.nudoq.org/#!/Packages/Lib.Web.Mvc/Lib.Web.Mvc/IJqGridHelper