Two Joins Datatables Editor Undefined index

Two Joins Datatables Editor Undefined index

vincmeistervincmeister Posts: 136Questions: 36Answers: 4
edited March 2016 in Free community support

Hello,

I'm trying to adapt this example https://datatables.net/forums/discussion/26095/full-editor-for-multiple-child-rows#Comment_71585 to my datatables.

There's a problem : "Undefined index p2b_detail on line 16 " when I submit edit data on the child table. The MySQL data was updated, but the child datatables won't refresh.

Line 16: $filter = $_POST['data']['p2b_detail']['p2b_nr'];

My code

$filter = isset($_GET['p2b_nr'])?$_GET['p2b_nr']:0;
    if ( isset($_POST['action']) && $_POST['action'] === 'edit' ) { 
        $filter = $_POST['data']['p2b_detail']['p2b_nr'];
    }

Please advise, i'm trying to rewrite all code, but the problem still appear.

Debug: http://debug.datatables.net/ecuzit

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    The error you are seeing suggests that the p2b_detail field(s) is not being submitted to the server. Are you using inline editing here? If so, have you set the submit option of the form-options to allIfChanged? Otherwise, only the changed value will be submitted.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    hi allan,

    i'm not using inline editing. I'm using form.
    If the p2b_detail field(s) is not being submitted to the server, why i've got an update after error. As i wrote on my question above, after i clicked the submit button, error appear, the data was updated (on MySQL). But the child datatables (#p2b_detail) not refreshed.
    When i closed the form and refresh the page, the child datatables (#p2b_detail) shown the updated value

    Please help, thank you

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Are you able to give me a link to the page please? The data format your code uses above is expecting data to be submitted in the legacy format. Is that correct? Have you configured Editor to submit in the legacy format (legacyAjax)?

    If you could link to the page that would be really helpful in being able to resolve this issue.

    Thanks,
    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Sorry Allan, it's on my localhost.

    I'm new in datatables, i just searching on this forum and found that link above to show 2 join datatables in one page. I didn't see any legacyAjax: true on that example link, so i guess that's not legacy.

    Or maybe there is any example for my need? I want to create 2 datatables, master detail on one page

    Please advise, thank you

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Okay - let's take a step back. What is the goal here? Do you want to apply a condition to the data that is loaded? What are you using to submit that data?

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4
    edited March 2016

    Sorry late reply,

    As on my debug link, I want to create a master-detail tables on one page.

    Master tables : Table (#p2b_head)
    Detail tables: Table (#p2b_detail)

    After browsing this forum, i found this link https://datatables.net/forums/discussion/26095/full-editor-for-multiple-child-rows#Comment_71585
    so i tried to adopt to my tables.
    - Both datatables works
    - Submit New Button both datatables works
    - Submit Edit Button master tables works
    - Submit Edit Button detail table error : "Undefined index p2b_detail on line 16 " , datatables not refreshed, but MySQL is updated

    Line 16: $filter = $_POST['data']['p2b_detail']['p2b_nr'];

    $filter = isset($_GET['p2b_nr'])?$_GET['p2b_nr']:0;
        if ( isset($_POST['action']) && $_POST['action'] === 'edit' ) {
            $filter = $_POST['data']['p2b_detail']['p2b_nr'];
        }
    

    The goal is success edit and refresh the datatables. Please advise, thank you

    or maybe, is there any example editor one to many join table on Child Row Datatables like this example http://datatables.net/examples/api/row_details.html

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    If you are not submitting in the legacy format, there will be no $_POST['data']['p2b_detail'] parameter (as the error suggests).

    This page documents the format that data is submitted in. Note that it is an object of data for each row.

    I would suggest reading this blog post which I think (hope!) will address the issues that you are seeing. It details how parent / child editing can be implemented.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4
    edited March 2016

    Hello Allan,
    Parent / Child editing is what i need. Thanks for the link.
    I'm trying to adapt that example to my page, then i found problem on selecting parent table, the child table won't show to linked row

    Please take a look on my code, what's wrong.

    Debug page

    My script

    var siteEditor = new $.fn.dataTable.Editor( {
                    ajax: "function/tes_join.php",
                    table: '#p2b_head',
                    fields: [ {
                                label: "No P2B:",
                                name: "p2b_head.p2b_nr"
                            }, {
                                label: "Date:",
                                name: "p2b_head.p2b_date",
                                type:  'datetime',
                            }, {
                                label: "To:",
                                name: "p2b_head.p2b_to"
                            }, {
                                label: "Category",
                                name: "p2b_head.p2b_category"
                            }
                        ] 
                } );
                
                var siteTable = $('#p2b_head').DataTable( {
                    dom: 'Bfrtip',
                    ajax: "function/tes_join.php",
                    columns: [
                        { data: "p2b_head.p2b_nr" },
                        { data: "p2b_head.p2b_date"},
                        { data: "p2b_head.p2b_to" },
                        { data: "p2b_head.p2b_category" }
                    ],
                    select: {
                        style: 'single'
                    },
                    buttons: [
                        { extend: 'create', editor: siteEditor },
                        { extend: 'edit',   editor: siteEditor },
                        { extend: 'remove', editor: siteEditor }
                    ]
                } );
                
                siteTable.on( 'select', function () {
                    usersTable.ajax.reload();
                    usersEditor
                        .field( 'p2b_detail.p2b_nr' )
                        .def( siteTable.row( { selected: true } ).data().p2b_nr );
                } );
                 
                siteTable.on( 'deselect', function () {
                    usersTable.ajax.reload();
                } );
    
                var usersEditor = new $.fn.dataTable.Editor( {
                    ajax: {
                        url: "function/p2b_detail.php",
                        data: function ( d ) {
                            var selected = siteTable.row( { selected: true } );
                            if ( selected.any() ) {
                                d.p2b_nr = selected.data().p2b_nr;
                            }
                        }
                    },
                    table: '#p2b_detail',
                    fields: [ {
                                label: "Item",
                                name: "p2b_detail.p2b_item"
                            }, {
                                label: "Quantity:",
                                name: "p2b_detail.p2b_quantity",
                            }, {
                                label: "SKU:",
                                name: "p2b_detail.p2b_sku",
                            },{
                                label: "No P2B:",
                                name: "p2b_detail.p2b_nr"
                            }                   
                        ]    
                } );
    
                var usersTable = $('#p2b_detail').DataTable( {
                    dom: 'Bfrtip',
                    ajax: {
                        url: "function/p2b_detail.php",
                        type: 'post',
                        data: function ( d ) {
                            var selected = siteTable.row( { selected: true } );
                            if ( selected.any() ) {
                                d.p2b_nr = selected.data().p2b_nr;
                            }
                        }
                    },
                    columns: [
                        { data: "p2b_detail.id" },
                        { data: "p2b_detail.p2b_item" },
                        { data: "p2b_detail.p2b_quantity" },
                        { data: "p2b_detail.p2b_sku" },
                        { data: "p2b_detail.p2b_nr" }
                    ],
                    select: true,
                    buttons: [
                        { extend: 'create', editor: usersEditor },
                        { extend: 'edit',   editor: usersEditor },
                        { extend: 'remove', editor: usersEditor }
                    ]
                } );
                
                
                 
                siteEditor.on( 'submitSuccess', function () {
                    usersTable.ajax.reload();
                } );
                 
                usersEditor.on( 'submitSuccess', function () {
                    siteTable.ajax.reload();
                } );
            
            } );
    

    My php code

    if ( ! isset($_POST['p2b_nr']) || ! is_numeric($_POST['p2b_nr']) ) {
            echo json_encode( [ "data" => [] ] );
        }
        else {
            Editor::inst( $db, 'p2b_detail' )
                ->fields(
                    Field::inst( 'p2b_detail.id' ),
                    Field::inst( 'p2b_detail.p2b_item' ),
                    Field::inst( 'p2b_detail.p2b_quantity' ),
                    Field::inst( 'p2b_detail.p2b_sku' ),
                    Field::inst( 'p2b_detail.p2b_nr' )
                )
                ->leftJoin( 'p2b_head', 'p2b_head.p2b_nr', '=', 'p2b_detail.p2b_nr' )
                ->where( 'p2b_detail.p2b_nr', $_POST['p2b_nr'] )
                ->process($_POST)
                ->json();
        }
    
  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Can you link to a page showing the issue so I can debug it live please?

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Here's the link Allan, please take a look
    thank you

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin
    Answer ✓

    selected.data().p2b_nr;

    This is returning undefined. Your data object's structure is:

    {
        "DT_RowId": "row_1",
        "p2b_head": {
            "p2b_nr": "1",
            "p2b_date": "2016-03-04",
            "p2b_to": "GRACE",
            "p2b_category": "BOOK"
        }
    }
    

    So you would use:

    selected.data().p2b_head.p2b_nr;
    

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    thanks allan

This discussion has been closed.