how to set id on master/detail?

how to set id on master/detail?

muratkasikcioglumuratkasikcioglu Posts: 4Questions: 1Answers: 0

I'm making a form as master/detail. Before open the form, I create a new master_id to use on detail datatables. How can I set related master_id on detail?

Answers

  • muratkasikcioglumuratkasikcioglu Posts: 4Questions: 1Answers: 0

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @muratkasikcioglu ,

    This example here may help, it shows how to use a row ID. If that doesn't get you going, it would if you could post your code too.

    Cheers,

    Colin

  • muratkasikcioglumuratkasikcioglu Posts: 4Questions: 1Answers: 0

    JS


    var editor;

        $(document).ready(function() {
    
            editor = new $.fn.dataTable.Editor( { 
                ajax: "assets/php/proposal_new.php",
                table: "#proposal_item",
                fields: [ 
                    {
                        label: "Id:",
                        name: "proposal_item.proposal_id"
                    }, {
                        label: "Product Name:",
                        name: "proposal_item.product_id",
                        type: "select",
                        placeholder: "Select product"
                    }, {
                        label: "Quantity:",
                        name: "proposal_item.quantity"
                    }, {
                        label: "Price:",
                        name: "proposal_item.price"
                    }
                ],
                i18n: {
                    create: {
                        button: "Add Product",
                        title:  "Add Product",
                        submit: "Add"
                    },
                    edit: {
                        button: "Edit",
                        title:  "Edit Line",
                        submit: "Submit"
                    },
                    remove: {
                        button: "Remove",
                        title:  "Remove Line",
                        submit: "Remove",
                        confirm: {
                            _: "Are you sure to delete selected lines?",
                            1: "Are you sure to delete?"
                        }
                    }
                }
            } );
    
            /*
            editor.
                .create( false );
                .set( 'proposal_item.proposal_id', "<?php echo $_SESSION['proposal_id'] ?>" );
                .submit();
                */
    
            editor.disable( 'proposal_item.proposal_id' );
    
            $('#proposal_item').DataTable( {
                dom: "Bfrtip",
                ajax: "assets/php/proposal_new.php",
                columns: [
                    { data: "proposal_item.proposal_id" },
                    { data: "product.title_tr" },
                    { data: "proposal_item.quantity" },
                    { data: "proposal_item.price" }
                ],
                select: true,
                buttons: [
                    { extend: "create", editor: editor },
                    { extend: "edit",   editor: editor },
                    { extend: "remove", editor: editor }
                ]
            } );
    

    PHP


    <?php

    ob_start();
    session_start();

    /*
    * Example PHP implementation used for the index.html example
    */

    // DataTables PHP library
    include( "editor/php/DataTables.php" );

    // Alias Editor classes so they are easy to use
    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

    // Build our Editor instance and process the data coming from _POST

    Editor::inst( $db, 'proposal_item' )
    ->debug( true )
    ->fields(
    Field::inst( 'proposal_item.proposal_id' ),
    Field::inst( 'proposal_item.product_id' )
    ->options( Options::inst()
    ->table( 'product' )
    ->value( 'id' )
    ->label( 'title_tr' )
    )
    ->validator( Validate::dbValues() ),
    Field::inst( 'product.title_tr' ),

        Field::inst( 'proposal_item.quantity' ),
        Field::inst( 'proposal_item.price' )
    )
    ->leftJoin( 'product', 'product.id', '=', 'proposal_item.product_id' )
    ->leftJoin( 'proposal_header', 'proposal_header.id', '=', 'proposal_item.proposal_id' )
    ->where( 'proposal_item.proposal_id', $_SESSION['proposal_id'] )
    ->process( $_POST )
    ->json();
    
  • muratkasikcioglumuratkasikcioglu Posts: 4Questions: 1Answers: 0
    edited May 2018

    I found a way. Maybe helps some one.

    Please look at "preCreate" on server side:


    Editor::inst( $db, 'proposal_item' )
        ->debug( true )
        ->fields(
            Field::inst( 'proposal_item.proposal_id' ),
            Field::inst( 'proposal_item.product_id' )
                ->options( Options::inst()
                    ->table( 'product' )
                    ->value( 'id' )
                    ->label( 'title_tr' )
                )
                ->validator( Validate::dbValues() ),
            Field::inst( 'product.title_tr' ),
    
            Field::inst( 'proposal_item.quantity' ),
            Field::inst( 'proposal_item.price' )
        )
        ->on( 'preCreate', function ( $editor, $values ) {
            $editor
                ->field( 'proposal_item.proposal_id' )
                ->setValue( $_SESSION['proposal_id'] );
        } )
        ->leftJoin( 'product', 'product.id', '=', 'proposal_item.product_id' )
        ->leftJoin( 'proposal_header', 'proposal_header.id', '=', 'proposal_item.proposal_id' )
        ->where( 'proposal_item.proposal_id', $_SESSION['proposal_id'] )
        ->process( $_POST )
        ->json();
    
This discussion has been closed.