views:

624

answers:

5

I've been evaluating a number of JQuery table plugins to handle my paging and sorting needs. I am looking for something that allows be to page and sort my tables with an AJAX call.

The problem that I am having is that all the plugins that I have found expect the Ajax call to return JSON. This is perfect for simple scenarios but falls down when I want to apply complex formatting to my tables, as soon as I want my table to include links or icons or other complex rendering I am faced with reproducing server side code that generates these links or chooses the appropriate icon as client side code to do the same thing.

What I would like to do is return the new table data as an html table and have the plugin replace the existing table with the returned table (either directly or by copying cells, the specifics are not important). Are there any reccomendations for the best way to do this?

+1  A: 

have a look at http://www.datatables.net/ i think this will fill your needs :)

Very good it is too!

as for the formatting thru JSON i am doing this quite hapily... just make sure you encode any funny characters and it should be fine.

Brian
Do you have an example for using an html table as the ajax datasource rather than JSON? I looked at datatables extensively and it didnt seem to be able to do this.
Jack Ryan
DataTables is a JSON solution, which the OP is not interested in. http://www.datatables.net/usage/server-side JSON all the way
butterchicken
FYI, the default usage is just to generate an HTML table then datatables does the rest - no JSON....However to make it more efficient for paging/very parge tables serverside processing is recomended.
Brian
By default datatables uses the tables generated in HTML see the sourcecode behind the basic example - http://www.datatables.net/examples/basic_init/zero_config.html
Brian
I don't think you are understanding the question properly. I want the datatables AJAX functionality but I want my AJAX response to be an HTML table rather than a JSON array. This allows me to use the same code for my initial rendering as for my AJAX responses. And prevents me rewriting my server side formatting code as client side code.
Jack Ryan
Seems like DataTables works with AJAX ; http://www.datatables.net/examples/data_sources/ajax.html
CmdrTallen
A: 

I have since found ingrid and will throw it out as a potential solution. It seems to do what I want though the look and feel seems quite heavy. I'll see how this answer is voted I guess.

Jack Ryan
A: 

Try this, its from YUI, not JQuery, but I think it will do exactly what you need

http://developer.yahoo.com/yui/examples/datatable/dt%5Fdynamicdata.html

Zoidberg
A: 

For jQuery, several grids can modfiy html tables in-place. Two I've used are flexigrid and jqgrid.

If you do this, you could for example, inject the html table using an ajax call (if necessary), then apply flexigrid() or jqgrid() on the resulting html table.

r00fus
A: 

I think your best option will be to create a controller action that returns a partial view which contains your table. The partial view can contain any JavaScript based table/grid you want. This solution has no dependency at all on any specific grid/table implementation.

For instance, let's say you have a strongly typed partial view of type ViewUserControl<IEnumerable<Foo>> named FooList.ascx

You would then define some action in your controller that rendered this partial

// For paging support, you would probably need to modify this action to accept
// a parameter that could be used to tell which records to retrieve.
// This would also require the appropriate route. However, this is beyond
// the scope of this example.
public PartialViewResult List()
{
  // get the collection of stuff that you want to display
  var items = this.itemRepository.GetItems();

  // return the partial
  return PartialView("FooList", items);
}


In your view where this partial is going to be used, you will want to have something like the following...

<!-- this <div> will contain the partial, which we will be able to update via AJAX -->
<div id="FooList">
    <%-- this assumes a collection of "items" is available in Model.Items --%>
    <% Html.RenderPartial("FooList", Model.Items); %>
</div>

<!-- use an AJAX ActionLink to update the table -->
<%=
    Ajax.ActionLink
    (
        "Click Me to Update the Table using AJAX!",
        "List",
        new AjaxOptions()
        {
            HttpMethod = "GET",
            LoadingElementId = "FooList"
        }
    )
%>

All we're doing here is using the ASP.NET MVC AJAX Helper to create a link that will generate an AJAX GET request to the List action in your controller. Also, by defining the LoadingElementId property in the AjaxOptions object, we have told the helper that we want to replace the inner contents of <div id="FooList"> with the results of the AJAX request.

When a user clicks that link, an AJAX request will cause your List action to be invoked. Your list action simply returns the rendered contents of the partial view FooList.ascx. The existing contents of the div with id="FooList" will be replaced by the contents that were retrieved from the AJAX request. In the above example, the controller action will always select the same data, which is not really what you want. In a real scenario, you would have to modify the controller action to retrieve the appropriate data for your partial view. I couldn't suggest an effective way for you to handle that without knowing the details for your implementation.

If you don't want to use a link, then just look at the HTML and JavaScript that is generated by the Ajax.ActionLink helper and adapt it to your own needs. For instance, build your own custom helper, or just manually write out the JavaScript.

Lastly, don't forget to include the MVC AJAX JavaScript source files. I recommend including them in your master page. For example:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
Justin Holzer