No update after create/edit a table

No update after create/edit a table

dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

Hi,
i have the problem that the table will not update after i create a new or edit an existing row. I did this a hundred times and it worked Always fine. I have no clue why it won't work now on this specific case. After pressing F5 in the browser all datas appeared as it should be.

I have to use two leftjoins, perhaps this is the error?

The Ajax part:

// 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, TB_MADB_TZUG, C_ZG_ID )
    ->debug( TRUE )
    ->fields(
        Field::inst( TB_MADB_TZUG.'.'.C_ZG_NT,     'ntlogin' ),
        Field::inst( TB_MADB_TZUG.'.'.C_ZG_YEAR,   'jahr' ),
        Field::inst( TB_MADB_TZUG.'.'.C_ZG_DAYS,   'tage' ),
        Field::inst( TB_MADB_TZUG.'.'.C_ZG_TYPE,   'art' ),
        Field::inst( TB_MADB_TZUG.'.'.C_ZG_REMARK, 'remark' ),
        Field::inst( 'A5_LANGUAGE_SET.LS_TRANS',   'ua_key' )
    )
    ->leftJoin ('A5_VACTYPE', 'A5_VACTYPE.UA_ID', '=', TB_MADB_TZUG.'.'.C_ZG_TYPE)
    ->leftJoin ('A5_LANGUAGE_SET', 'A5_LANGUAGE_SET.LS_NAME', '=', 'A5_VACTYPE.UA_KEY')
    ->where(TB_MADB_TZUG.'.'.C_ZG_NT, $nt)
    ->where('A5_LANGUAGE_SET.LS_LG_ID', 1)
    ->process( $_POST )
    ->json();

The js part:
$(document).ready(function() {

    // Editor
    editor = new $.fn.dataTable.Editor( {
        ajax: "ajax/tzug.php",
        table: "#set_tzug",
        fields: [ {
                label: "Jahr",
                name:  "jahr",
                type: "text"
            },{
                label: "Tage",
                name: "tage",
                type: "text"
            },{
                label: "Art",
                name: "art",
                type: "select",
                options: [
                    <?php
                        for ($i = 0; $i < $mypos; $i++)
                        {
                            echo "{ label: '" . $tzugArten[$i]['tz_nam'] . "', "
                                 . "value: " . $tzugArten[$i]['tz_id'] . " },\n";
                        }
                    ?>
                ],
                placeholder: "Bitte T-Zug wählen"
            },{
                label: "Bemerkung",
                name: "remark",
                type: "text"
            },{
                type: "hidden",
                name: "ntlogin",
                default: "<?php echo $row->ntlogin; ?>"
            }
        ]
    } );


    // DTables
    $('#set_tzug').DataTable({
        "dom": '<"top"B>rt<"bottom"ip><"clear">',
        serverSide: false,
        "processing": true,
        "order": [[0, 'asc']],
        "pageLength": 12,
        "ajax": {
            "url": "ajax/tzug.php",
            "type": "POST",
            data: { mant: '<?php echo $row->ntlogin; ?>' }
        },
        select: true,
        columns: [
            { data: "jahr" },
            { data: "tage" },
            { data: "ua_key" },
            { data: "remark" }
        ],
        buttons: [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor }
        ]
    });

} );

Any idea where i have to look closely?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @dg_datatables ,

    I did this a hundred times and it worked Always fine.

    What's changed since it did work? Did you change the DB, or server-side scripts, or has the client software been updated in any way?

    Cheers,

    Colin

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    Hi Colin,
    no nothing was changed. I've have around 10 projects which use datables. This is an existing project. It's only expanded for a new table. The other tables in this project are working correctly - so all dependencies and scripts are available. I do not get any error message in the console or on the website.

    Once again: Everything works. I can see all datas, i can sort or search the datas. But when i edit an existing row, after i press the "update" button the row disappeared in the table. When i reload the page the edited row appears with the correct (edited) datas.
    The same after i insert a new row. After the insert the new row does not appeared on the screen. After a refresh of the website, the new row is showed.

    Thanks for your help
    Christian

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    edited March 2019

    Hi @dg_datatables ,

    If it's appearing after a refresh, it's likely to be something to do with the response from the server. Can you post the message the server sends back in respond to the create please. And you might as well post the message to the server as well for completeness!

    Cheers,

    Colin

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    This is send to the server:

        action: create
        data[0][art]:   32
        data[0][jahr]:  2019
        data[0][ntlogin]:   mn82rt
        data[0][remark]:    
        data[0][tage]:  8
    

    and this is the response:

    JSON    
    data    []
    debug   […]
    0   {…}
    query   INSERT INTO MADB_TZUG ( ZG_NT, ZG_YEAR, ZG_DAYS, ZG_TYPE, ZG_REM ) VALUES ( :ZG_NT, :ZG_YEAR, :ZG_DAYS, :ZG_TYPE, :ZG_REM ) RETURNING MADB_TZUG.ZG_ID INTO :editor_pkey_value
    bindings    […]
    0   
    name    :ZG_NT
    value   mn82rt
    type    null
    1   
    name    :ZG_YEAR
    value   2019
    type    null
    2   
    name    :ZG_DAYS
    value   8
    type    null
    3   
    name    :ZG_TYPE
    value   32
    type    null
    4   {…}
    name    :ZG_REM
    value   
    type    null
    1   {…}
    query   SELECT MADB_TZUG.ZG_ID as "MADB_TZUG.ZG_ID", MADB_TZUG.ZG_NT as "MADB_TZUG.ZG_NT", MADB_TZUG.ZG_YEAR as "MADB_TZUG.ZG_YEAR", MADB_TZUG.ZG_DAYS as "MADB_TZUG.ZG_DAYS", MADB_TZUG.ZG_TYPE as "MADB_TZUG.ZG_TYPE", MADB_TZUG.ZG_REM as "MADB_TZUG.ZG_REM", A5_LANGUAGE_SET.LS_TRANS as "A5_LANGUAGE_SET.LS_TRANS" FROM MADB_TZUG LEFT JOIN A5_VACTYPE ON A5_VACTYPE.UA_ID = MADB_TZUG.ZG_TYPE LEFT JOIN A5_LANGUAGE_SET ON A5_LANGUAGE_SET.LS_NAME = A5_VACTYPE.UA_KEY WHERE MADB_TZUG.ZG_NT IS NULL AND A5_LANGUAGE_SET.LS_LG_ID = :where_1 AND MADB_TZUG.ZG_ID = :where_2
    bindings    […]
    0   
    name    :where_1
    value   1
    type    null
    1   
    name    :where_2
    value   9
    type    null
    

    The data appears only after a refresh.

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    Answer ✓

    data is an empty array. It should contain the data for the newly added row.

    It looks like you have a WHERE condition. Does the newly added row match that? my guess is not.

    Allan

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    I will doublecheck this. But why do i see it after a refresh?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Hi @dg_datatables ,

    It's because the response determines how the table is redrawn after the submission. The submission succeeds, the data is there, but if the response isn't as expected then Editor doesn't redraw the table with the new data. The refresh works because it gets all data afresh.

    Cheers,

    Colin

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    @allan
    You were absolutly right - like always ;-)

    After an edit/new action the variable $nt is empty and the where-condition could not be passed. I put the $nt into a $_Session and everything works fine.

    Just for me to know:
    How can i read the value of the hidden field from the editor in the ajax part?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @dg_datatables ,

    If it's hidden, you can still access the column with row().data(). Hope that helps,

    Cheers,

    Colin

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    @colin
    No, i mean a hidden field from the editor pop up.

    If i have a editor like in the example (see below). How can i get access to the hidden field "secret" im my ajax? I want to manipulate the where statement with the hidden field from editor.

    // Editor
        editor = new $.fn.dataTable.Editor( {
            ajax: "ajax/tzug.php",
            table: "#set_tzug",
            fields: [ {
                    label: "Bemerkung",
                    name: "remark",
                    type: "text"
                },{
                    type: "hidden",
                    name: "secret",
                    default: "nobody see this"
                }
            ]
        } );
    
  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    Answer ✓

    I'd actually suggest you do this:

    ajax: {
      url: 'ajax/tzug.php',
      data: function ( d ) {
        d.secret = 'whatever';
      }
    }
    

    Then you can just do $_POST['secret'] to get the value. That assumes that you don't want to do the value of a per row basis though.

    If you do want to do it on a per row basis, then keep your hidden field and use a server-side event. The data for the row to be processed is passed into the event handler. That has the benefit of working with multi-row editing as well.

    Allan

  • dg_datatablesdg_datatables Posts: 53Questions: 10Answers: 1

    Great! Thanks a lot.

This discussion has been closed.