Datatable Editor - Add two different unrelated SQL tables on the same page

Datatable Editor - Add two different unrelated SQL tables on the same page

musicawaysmusicaways Posts: 5Questions: 3Answers: 0

Good morning guys,
I am trying to add two different unrelated SQL tables (table1-table2) of the same db (Database1) on the same page.
The first table (table1) works correctly but the second table (table2) is not loaded, the error reported is the following (DataTables warning: Table id = ID-ajax error. For more information about this error, please see HTTP://DATATABLES.NET/TN/7).
I read the link but I can not understand where I'm wrong, (I'm very noob...) Could you help me?

This is my code:

config.php

<?php if (!defined('DATATABLES')) exit(); // Ensure being used in DataTables env.

 // Enable error reporting for debugging (remove for production)
error_reporting(E_ALL);
ini_set('display_errors', '1');

$sql_details = array(
"type" => "Mysql",  // Database type: "Mysql", "Postgres", "Sqlserver", "Sqlite" or "Oracle"
"user" => "root",       // Database user name
"pass" => "admin",       // Database password
"host" => "localhost",       // Database host
"port" => "",       // Database connection port (can be left empty for default)
"db"   => "Database1",       // Database name
"dsn"  => ""        // PHP DSN extra information. Set as `charset=utf8` if you are using MySQL
);

index.html

  Tableone = new $.fn.dataTable.Editor
         $('#table1').DataTable( {
            dom: 'Bfrtip',
            ajax: "../php/staff.php",
                    type: 'POST',
                    scrollX: true,
            pageLength: 12,
            columns: [
                { data: "table1.field1" },
                { data: "table1.field2" },
                { data: "table1.field3", render: $.fn.dataTable.render.number( '.', ',', 0, '€ ' ) },
                { data: "table1.field4" },
                { data: "table1.field5" },
            ],
             columnDefs: [ 
                     {targets: 0, visible: true},
                     {targets: 1, visible: true},
                     {targets: 2, visible: true},
                     {targets: 3, visible: false},
                     {targets: 4, visible: true},
                  ],
                   fixedColumns: true,
                    select: true,
                           buttons: [
                        {extend: 'colvis',
                         text: "Colonne",
                        columns: ':not(.noVis)'},
                        { extend: 'create',
                         text: "Nuovo",
                         editor: editor },
                        { extend: 'edit',
                         text: "Modifica",
                         editor: editor },              
                        { extend: "remove",
                         text: "Rimuovi",
                         editor: editor,
                            formMessage: function ( e, dt ) {
                                var rows = dt.rows( e.modifier() ).data().pluck('pignoramenti.field1');
                                return 'Eliminare il record relativo al soggetto'+rows.join+' ?';}
                            },
                        { extend: 'collection',
                            text: 'Esporta',
                                buttons: [
                                    'excel',
                                    'csv',
                                    'pdf',
                                    'print'
                                            ]
                            }
                        ],
                        } );

    Tabletwo = new $.fn.dataTable.Editor
       $('#table2').DataTable( {
        dom: 'Bfrtip',
           "bProcessing": true,
           "bServerSide": true,
    ajax: "../php/Anag.php",
           "scrollX": true,
    columns: [
        { data: "table2.id" },
        { data: "tabel2.field2" },
        { data: "tabel2.field3" },
        { data: "tabel2.field4" },
                    ],
            fixedColumns: true,
    select: true,
    buttons: [
        { extend: 'create', editor: editor },
        { extend: 'edit',   editor: editor },
        { extend: "remove", editor: editor,
                formMessage: function ( e, dt ) {
                var rows = dt.rows( e.modifier() ).data().pluck('table2.id');
                return 'Eliminare il record relativo al soggetto'+rows.join+' ?';}
         },
         {
            extend: 'collection',
            text: 'Export',
            buttons: [
                'excel',
                'csv',
                'pdf',
                'print'
            ]
        }
    ]
        } );
     } );

staff.php

<?php
include( "../../php/DataTables.php" );

use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;

Editor::inst( $db, 'table1' )
->fields(
    Field::inst( 'table1.field1' ),
    Field::inst( 'table1.field2' )->validator( 'Validate::notEmpty' ),
    Field::inst( 'table1.field3 )
        ->validator( 'Validate::dateFormat_required', 'Y-m-d' )
        ->getFormatter( 'Format::date_sql_to_format', 'Y-m-d' )
        ->setFormatter( 'Format::date_format_to_sql', 'Y-m-d' ),
    Field::inst( 'table1.field4' ),
    Field::inst( 'table1.field5' )
        ->setFormatter( 'Format::fromDecimalChar')->setFormatter( 'Format::ifEmpty', 0 ),   
->process( $_POST )
->json();

Anag.php

    <?php
include( "../../php/DataTables.php" );

use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;

Editor::inst( $db, 'table1' )
->fields(
    Field::inst( 'table2.id' ),
    Field::inst( 'tabel2.field2' )->validator( 'Validate::notEmpty' ),
    Field::inst( 'tabel2.field3 ),
    Field::inst( 'tabel2.field4' )  
->process( $_POST )
->json();

Answers

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    In anag.php you are trying to read the table2 fields from table1. That is not working.

    Editor::inst( $db, 'table1' )
    
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Editor::inst( $db, 'table1' )
    ->fields(
        Field::inst( 'table2.id' ),
        Field::inst( 'tabel2.field2' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'tabel2.field3 ),
        Field::inst( 'tabel2.field4' ) 
    ->process( $_POST )
    ->json();
    

    Don't you mean

    Editor::inst( $db, 'table2' )
    ....
    

    ?

  • musicawaysmusicaways Posts: 5Questions: 3Answers: 0

    Sorry guys,
    I reported the wrong data here but in the original file (Anag.php) The data relative to the table is correct (table2).
    Do you have any ideas?

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    I recommend you follow the instructions to find out what kind of error you have.

    https://datatables.net/manual/tech-notes/7

    In particular I would follow this advice:

    More detailed information can be obtained from the network panel will show all of the requests made by Chrome to load the page.
    Refresh the page to allow Chrome to capture all requests
    Click the XHR option at the bottom of the window to reduce the requests to just the Ajax (XHR) requests.

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin

    You are using editor: editor in both tables, but you don't actually have an editor variable. You've got Tableone and Tabletwo (which should really have a var in front of them unless you want them to be global).

    The editor option of the button types is to tell it what Editor instance it should use - e.g. you might have editor: Tableone in the first table.

    Allan

This discussion has been closed.