am working with jqxgrid to display data in a grid in an html page.
For loading data from a local json data, jqxgrid says the following code will work.
var source ={
datatype: "json",
datafields: [{ name: 'name' },{ name: 'type' },
{ name: 'calories', type: 'int' },
{ name: 'totalfat' },{ name: 'protein' },],
localdata: jsonData
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxgrid").jqxGrid(
{
width: 670,
source: dataAdapter,
theme: theme,
columnsresize: true,
columns: [
{ text: 'Name', datafield: 'name', width: 250 },
{ text: 'Beverage Type', datafield: 'type', width: 250 },
{ text: 'Calories', datafield: 'calories', width: 180 },
{ text: 'Total Fat', datafield: 'totalfat', width: 120 },
{ text: 'Protein', datafield: 'protein', minwidth: 120 }
]
});
});
And this will work. But my problem is suppose i need to dynamically generate this datafields & columns values. I generate the json string's for both of these and stored that in 2 variables like
jsonStr = '[{ name: 'name' },{ name: 'type' },{ name: 'calories', type: 'int' },
{ name: 'totalfat' },{ name: 'protein' },]'
and
jsonColsStr='[{ text: 'Name', datafield: 'name', width: 250 },
{ text: 'Beverage Type', datafield: 'type', width: 250 },
{ text: 'Calories', datafield: 'calories', width: 180 },
{ text: 'Total Fat', datafield: 'totalfat', width: 120 },
{ text: 'Protein', datafield: 'protein', minwidth: 120 }
]'
and jqxgrid loading code will look like this.
var source ={datatype: "json",
datafields: jsonStr,
localdata: jsonData
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxgrid").jqxGrid(
{
width: 670,
source: dataAdapter,
theme: theme,
columnsresize: true,
columns: jsonColsStr
});
});
But this is not working for me..??Can any one help me to solve this issue.?