drop-down list with the choice

drop-down list with the choice

maxstdmaxstd Posts: 6Questions: 2Answers: 0

Hello friends!
Please help with the solution of problem:
I have a mysql table that has three fields -
id, parent_id, name

|id| parent_id| name
|1 |                | store
|2 |                | cafe
|3 | 1             | store_name1
|4 | 1             | store_name2
|5 | 2             | cafe_name1
|6 | 2             | cafe_name2
(function($){

$(document).ready(function() {
    var editor = new $.fn.dataTable.Editor( {
        ajax: 'php/table.test.php',
        table: '#test',
        fields: 
                [
            {label: "pole1",name: "test.id",type: "select"},
            {label: "pole2",name: "test.parent_id",type: "select"}
        ]
    } );
    var table = $('#test').DataTable( {
        dom: 'Tfrtip',
        ajax: 'php/table.test.php',
        columns:
               [
            {data: "test.id"},
            {data: "test.parent_id"}
        ],
        select: true,
        buttons: [
            { extend: 'create', editor: editor },
            { extend: 'edit',   editor: editor },
            { extend: 'remove', editor: editor }
        ]
    } );
} );
}(jQuery));
include( "lib/DataTables.php" );
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

Editor::inst( $db, 'test'' )
    ->fields(
        Field::inst( 'id' )->options('test','id','name'),
        Field::inst( 'parent_id' )->options('test','parent_id','name')
    )
    ->process( $_POST )
    ->json();

here's my question:

when set in the pole1 (store or cafe), in pole2 put a value equal to its id

Answers

  • maxstdmaxstd Posts: 6Questions: 2Answers: 0

    replay

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    So you want the options in the second list to change based on the value selected int he first list?If so, then the dependent() method is probably the best way to do this. You would need to write a script on the server-side that will get the data for the list based on the value submitted, and then return the options as JSON data so Editor can update the list of options.

    Regards,
    Allan

  • maxstdmaxstd Posts: 6Questions: 2Answers: 0

    Thanks for the quick response!
    Do I understand correctly -

    1 step:

    create get_fields.php file

    <?
    $db = mysql_connect ("localhost","root",""); mysql_select_db ("testbase",$db);
    $sql="SELECT parent_id FROM testtable";
    $result = mysql_query($sql);
    $stack=array();
        while($row = mysql_fetch_array($result))
              { array_push($stack,array($row['parent_id '],$row['parent_id '])); }
    echo json_encode($stack);
    
    <?php
    >
    ```
    step 2:
    ?>
    
    
    add in my js file this code
    ```javascript
    var test= new Array({"label" : "a", "value" : "a"});
     
    function loader(){
    test.splice(0,1);
    $.ajax({
      url: 'php/get_fields.php',
      async: false,
      dataType: 'json',
      success: function (json) {
          for(var a=0;a<json.length;a++){
            obj= { "label" : json[a][0], "value" : json[a][1]};
            test.push(obj);
          }
        }
    });
    return test;
    }
    

    step 3:

    add in my js file this code

            columns: [
                {data: "test.id"},
                {data: "test.parent_id",ipOpts:loader()}
                           ],
    

    or this metod is wrong?

    Sorry Allan, but I can not understand how to relate these fields using dependent. I have tried only to show and hide, but no more.

This discussion has been closed.