Arcan3
Arcan3

Reputation: 89

Trying to add dynamic options to jtable (jQuery)

The basic syntax for adding a RadioButton to the AddRecord option is as follows

active: {
  title: 'Activo',
  width: '5%',
  type: 'radiobutton',
  options: { '0': 'No', '1': 'Si' }
},

Ive been trying to make the "options" come from the db but haven't figured a way yet (PHP).

The plugin works by using a $_REQUEST to "dbactions.php?action=", and returns a JSON Array

$jTableResult = array();
    $jTableResult['Result'] = "OK";
    $jTableResult['TotalRecordCount'] = $recordCount;
    $jTableResult['Records'] = $rows;
    print json_encode($jTableResult);

which i presume goes to the "./js/jtable/jquery.jtable.min.js" script.

As far AFAIK/have read i cannot insert php code inside a js script so i'm pretty lost as to how i could make it dynamic. All relevant examples i've found are for asap.net instead of php.

I actually wanted to use a combobox instead of a radiobutton.

So far i've been using a view to show the data, but when i insert new data i insert directly to the table (which has 3 fields instead of the 4 shown), so i need to show the name field as an option but insert the id (plus 2 dates) into one of the tables that conform the view.

Anyone have any ideas on how to accomplish this?

Upvotes: 1

Views: 4931

Answers (1)

Ante Vrcan
Ante Vrcan

Reputation: 21

In main file

<?php include 'file.php'; ?>

    <script  type="text/javascript">

    active: {
      title: 'Activo',
      width: '5%',
      type: 'radiobutton',
      options: <?php echo $options ; ?> 

    </script>

In php file.php

<?php
    //Open database connection
    include 'Connections/localhost.php';
$result = mysql_query("SELECT
id,
option
FROM
options;");

        //Add all records to an array
        $rows = array();
        while($row = mysql_fetch_array($result))
        {
            $rows[$row['id']] = $row['option'];

        }


        //Return result to jTable
        $options = json_encode( $rows);

    //Close database connection
    mysql_close($localhost);

?>

Upvotes: 2

Related Questions