How can jqGrid be used when the number of columns is dynamic (i.e. determined by the data set)?
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 ge开发者_如何转开发t 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?
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?
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
...
});
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.
精彩评论