Master detail in two tables

Master detail in two tables

RichardADRichardAD Posts: 1Questions: 0Answers: 0

I want to present master detail information in two datatables A and B.

master data: master_id, name, date, total_cost, item_count
detail data: detail_id, master_id, item_id, quantity, cost

The table A will show the master information and when a record is selected the detail information is show in table B

Couple of questions:
How can the initialization of table B be prevented (do not want to show any records until one is selected) ?

The master_id is not part of the row().data()
How can I get the master_id value from the selection?
The master_id value sort of shows up in the datatable <tr id="row_{id_value}"> but I don't trust using that.

jQuery ready
   var master_editor = new $.fn.dataTable.Editor ({
        ajax: "data/master_ajax.php",
        table: "#master",
        fields: [
              { name: 'name' }, { name: 'date'}, { name: 'total_cost'}, { name: 'item_count'}
        ]
    });

    var master_table = $("#master") . DataTable({
        ajax: "data/master_ajax.php",
        paging: true,
        columns: [
              { name: 'name' }, { name: 'date'}, { name: 'total_cost'}, { name: 'item_count'}
        ],
        select:'single',
        lengthChange:false
    });

    master_table.on ( 'select', function ( e, dt, type, indexes ) {
        if ( type === 'row' ) {
            var standard_id = master_table.row().data().pluck('master_id');
            alert (master_id);
// do something to apply where master_id = selected master_id to detail table
        }
    });


php handler (master_ajax.php)
$editor = Editor::inst( $db, 'sales.master', 'master_id' )
    -> fields (
         Field::inst('name')
        ,Field::inst('date')
        ,Field::inst('total_cost')
        ,Field::inst('item_count')
    )
    -> process ( $_POST )
    -> json ()
;

Replies

  • ErwinNErwinN Posts: 1Questions: 0Answers: 0

    Hi,

    I am figuring out a similar case.

    1. Your master_ajax.php must have Field::inst('master_id').
    2. Change your select event to:

    master_table.on( 'click', 'tr', function () {
    var myData = master_table.row( this ).data();

            console.log(myData.master_id);
        } );
    

    That is what i figured out sofar.
    Now I am working on the details table to get them updated and redrawn.

    This will get you started.

This discussion has been closed.