jQWidgets Forums

jQuery UI Widgets Forums DataTable dynamically set the datafields on DataTable

This topic contains 2 replies, has 2 voices, and was last updated by  christianjavan 11 years ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author

  • christianjavan
    Participant

    I want to use the datatable to dynamically set the data fields depending on the array keys of my query, in other words, how can DataTable be reusable? Right now i can only fill the dataField manually like this:

    var source =
    {
    localdata: data,
    datatype: “array”,
    datafields:
    [
    { name: ‘firstname’, type: ‘string’ },
    { name: ‘lastname’, type: ‘string’ },
    { name: ‘productname’, type: ‘string’ },
    { name: ‘available’, type: ‘bool’ },
    { name: ‘date’, type: ‘date’ },
    { name: ‘quantity’, type: ‘number’ },
    { name: ‘price’, type: ‘number’ }
    ]

    And i want something more like:

    get the query result into an array:
    $rawdata = array();
    while($row = odbc_fetch_array($result))
    {
    $rawdata[] = $row;
    }

    Get the array keys and it’s total:
    $myArray = array_keys($rawdata[0]);
    $keyCount = sizeof($miArray);

    Set them programatically in here:

    datafields:
    [
    //Fields
    ]
    Any ideas?


    Peter Stoev
    Keymaster

    Hi christianjavan,

    You will have to prepare the datafields array of the source object in some way before creating the widget. If you don’t know your datafields and you get them from a server, I suppose that after your server returns the data, you will be able to loop through that data, fill the datafields array with the appropriate values and then prepare a new dataAdapter instance and bind jqxDataTable to it.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/


    christianjavan
    Participant

    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.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.