1

I have a generalized php routine which generates xml results from different mySQL queries. The actual structure of the xml will vary depending on which query is used. (Actually, the number of columns will vary.)

I've just started to explore using jqGrid, and I'd like to write a generalized display routine where I do an ajax call to my php script to get the relevant xml, and then, based on the xml I get back, dynamically create the colModel for jqGrid (i.e. number of columns, column names, etc.).

Is this possible, and if so, how do I approach it?

jalperin
  • 2,664
  • 9
  • 30
  • 32

3 Answers3

1

Assuming that your columns are mapped to the JSON object's properties/attributes, yes, it's possible to do it with just one call to the server. But you have to:

  • have control over the JSON (grid data) coming over from the server
  • construct the column model by looking at the JSON element's attributes
  • re-create the grid using the newly created column model

Pseudo code looks like this:

   // row - the JSON object whose data represent a row
   var colNames = new Array();
   var colModel = new Array();
   for ( var i in row ) {
       colNames.push(i);
       colModel.push( { name: i, index: i, width: 60, sorttype: sorttype, formatter: formatter, formatoptions: formatoptions, align: align} );
    }
    $('#grid').jqGrid({
        datatype : 'local',
        colNames: colNames,
        colModel: colModel
...
    });
Faustas
  • 350
  • 1
  • 2
  • 10
1

You could do this by making a separate AJAX request to retrieve the dynamic columns. Once you have that data, you would need to dynamically generate the colmodel and colnames options, and create the jqGrid using them. At this point, you could pass it a URL to retrieve data from, as long as the URL result set is guaranteed to contain all of the dynamic columns.

Does that help?

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • This is helpful and similar to another Stack Overflow response I found. See http://stackoverflow.com/questions/2277962/jqgrid-and-dynamic-column-binding. I was hoping not to have to do 2 queries to the data source, because it responds relatively slowly and overall performance will be negatively affected. – jalperin Aug 01 '10 at 14:22
  • Unfortunately you will need to make two requests, unless you can somehow do the dynamic column calculations on the server, before you render the initial page... – Justin Ethier Aug 01 '10 at 15:21
0

You could merge the column model and add it along with data and use one request to get everything, possibly multiple similar jqGrids in a page using ajax and json.

keyser
  • 18,829
  • 16
  • 59
  • 101