tags:

views:

982

answers:

3

How to bind jqGrid dynamically?. The columns are not available at design time but will only be available only at runtime.

In the current jqGrid design the colmodels and other properties needs to be pre-populated for the grid to work correctly.

Any input in this direction is greatly appreciated.

A: 

Is it feasible to recreate the grid each time a column is added? You could store the data locally and just Unload / Recreate the grid each time, using a dynamic column model.

You may also want to look at some of the demos that show/hide columns dynamically. Depending upon how many columns you have, you might be able to use the same concept in your application.

Does that help?

Justin Ethier
+3  A: 

Try this in document.ready:

$.ajax(
    {
       type: "POST",
       url: "SomeUrl/GetColumnsAndData",
       data: "",
       dataType: "json",
       success: function(result)
       {
            colD = result.colData;
            colN = result.colNames;
            colM = result.colModel;

            jQuery("#list").jqGrid({
                jsonReader : {
                    cell: "",
                    id: "0"
                },
                url: 'SomeUrl/Getdata',
                datatype: 'jsonstring',
                mtype: 'POST',
                datastr : colD,
                colNames:colN,
                colModel :colM,
                pager: jQuery('#pager'),
                rowNum: 5,
                rowList: [5, 10, 20, 50],
                viewrecords: true
            })
       },
       error: function(x, e)
       {
            alert(x.readyState + " "+ x.status +" "+ e.msg);   
       }
    });
setTimeout(function() {$("#list").jqGrid('setGridParam',{datatype:'json'}); },50);

this work fine for me.

bruno
This is a cool trick and this will definitely work.. The only concern here is you need to have two post request to bind this... one for getting the columns and for getting the grid data.. Thanks for your input..
rajesh pillai
no you will always get one post request...the first request is via the ayax cal and you get the data with your columns, in a jsonstring when you navigate trough the data it will use the "someurl/getdata" to get the data...I had the problem also that in the begin i had 2 requests... but with this solution, to get the data first in a jsonstring and later via the url you'll get always one request.
bruno
A: 

can you share the whole code ? what is the format of result.colData ?

Sagar S