jQWidgets Forums
Forum Replies Created
-
Author
-
April 29, 2014 at 6:37 pm in reply to: dynamically set the datafields on DataTable dynamically set the datafields on DataTable #53743
Thanks, I just did what you said and all worked perfectly, using the info you give me and the example that Dimitar wrote here: http://www.jqwidgets.com/community/topic/need-to-create-grid-columns-dynamically-from-json/ I was able to find a final solution to create dynamic tables.
For those who might be interested, here is what I did:
First, get the query result:
$result = odbc_exec($idconex,$consulta) or die("Query failed(-1)");
Stored it in an Array:
$rawdata = array(); while($row = odbc_fetch_array($result)) { $rawdata[] = $row; }
Get the array keys to dynamically add them, take in count that my keys where on the second level of the array:
$myArray = array_keys($rawdata[0]);
Store the data for the dataFields and colums of the table:
$dFields = array(); $dCols = array(); foreach($myArray as $value){ $dFields[] = array( "name" => $value, "type" => "string"); $dCols[] = array("text" => $value, "dataField" => $value, "width" => 200); }
Encoded the arrays in the “prepare the data” part of the script:
var dfields = <?php echo json_encode($dFields); ?>; var dcols = <?php echo json_encode($dCols); ?>;
The source code:
var source = { localData: data, dataType: "json", dataFields:dfields ]*/ };
And the adapter:
$("#dataTable").jqxDataTable( { width: 2400, pageable: false, source: dataAdapter, altRows: true, sortable: true, columns: dcols });
I am sure that this code can be optimized.
-
AuthorPosts