Editor Table - New / Edit / Update is not working properly, Not getting AJAX response for this

Editor Table - New / Edit / Update is not working properly, Not getting AJAX response for this

IanBurmanIanBurman Posts: 7Questions: 0Answers: 0

Link to test case: https://dev.bosveldjagters.co.za/branch-list/

Description of problem:
I have integrated the table with the Editor code which was what they mentioned in the Examples. When I submit the table data with changes the data request passed to the AJAX file, but I didn't get a response from the ajax file and that's not affected to my table. I thought the insert and update and delete process will automatically will happen and for that the query will automatically will generate by using the editor. But I think that's not? Here what I need to change it. Please check my codes below,

Debugger code (debug.datatables.net):

$tablename = 'branchList';
$recsource = "wp_mms_branches";
$coldata = "BranchID, Tak, TakNaam, Disabled, BranchPhone, BranchEmail";
$colheads = "BranchID, Tak, TakNaam, Disabled, BranchPhone, BranchEmail";
$colclasses = " , text-start, , tf-bi-check-circle, , " ;
$colfield_toEdit = "BranchID, Tak, TakNaam, Disabled, BranchPhone, BranchEmail, BranchFax, BranchAddress1";
$readonly = "rdly, , , rdly, , , , " ;
 
$edit_capability = "branch_edit";
$list_capability = "branch_list";
 
global $wpdb;
    $records = $wpdb->get_results("SELECT * FROM $recsource");
    $total_records = count($records);
 
    echo    " ". PHP_EOL ;
    echo    "<div id=\"mms_table\" class=\"table-responsive mms_table\">". PHP_EOL ;
    echo    "   <table id=\"mms_dte\" class=\"mms_dte $tablename display\" >". PHP_EOL ;
    echo    "   <thead>". PHP_EOL ;
    echo    "        <tr>". PHP_EOL ;
  
    $colheads = str_ireplace (' ', '', $colheads);  //strip white space
    $colheads_arr = explode (",", $colheads);  //chg to array
    foreach($colheads_arr as $thcol) //loop over column heads array
            {
                echo "<th> ",_e( "$thcol" ) ,"</th>  ". PHP_EOL;
            }
                /*echo "<th></th>  ". PHP_EOL;
                echo "<th></th>  ". PHP_EOL; */
    echo    "    </tr>". PHP_EOL ;
    echo    "</thead>". PHP_EOL ;
    echo    "<tbody>". PHP_EOL ;
 
    $coldata = str_ireplace (' ', '', $coldata);  //strip white space
    $coldata_arr = explode (",", $coldata);  //chg to array
    $colclasses = str_ireplace (' ', '', $colclasses);  //strip white space
    $colclasses_arr = explode (",", $colclasses);  //chg to array
    foreach ( $records as $rec ) {
            echo "<tr>". PHP_EOL;
            foreach($coldata_arr as $key=>$tdcol) //loop over column data array
            {           
                if (substr($colclasses_arr[$key],0,3)=="tf-") {
                    echo "<td class=\"$tablename-$tdcol ", ($rec->$tdcol >0?substr($colclasses_arr[$key],3):'')," \">"," ","</td>  ". PHP_EOL;
                } else {
                    echo "<td class=\"$tablename-$tdcol $colclasses_arr[$key] \">",$rec->$tdcol,"</td>  ". PHP_EOL;
                }
            }
                    /*echo "<td></td>  ". PHP_EOL;
                    echo "<td></td>  ". PHP_EOL; */
            echo "</tr>". PHP_EOL;
        }
    echo    "</tbody>". PHP_EOL ;
    echo    "<tfoot>". PHP_EOL  ;
    echo    "    <tr>". PHP_EOL ;
          
            foreach($colheads_arr as $thcol) //loop over column heads array
            {
                echo "<th> ",_e( "$thcol" ) ,"</th>  ". PHP_EOL;
            }
                /*echo "<th></th>  ". PHP_EOL;
                echo "<th></th>  ". PHP_EOL; */
         
    echo    "    </tr>". PHP_EOL ;
    echo    "</tfoot>". PHP_EOL ;
    echo    "</table>". PHP_EOL ;
 
    /* Making the fields value for editor */
    $colfield_toEdit = str_ireplace (' ', '', $colfield_toEdit);  //strip white space
    $colfield_toEdit_arr = explode (",", $colfield_toEdit);  //chg to array
    $readonly = str_ireplace (' ', '', $readonly);  //strip white space
    $readonly_arr = explode (",", $readonly);  //chg to array
 
    $dte_fields = $dte_columns = $dte_queyCol = array();
    foreach( $coldata_arr as $coldata ){
        $dte_columns[] = '{ data: "'. $coldata .'" }';
    }
 
     foreach( $colfield_toEdit_arr as $coldata ){
        $dte_fields[] = '{ label: "'. $coldata . ':", name: "'. $coldata .'" }';
        $dte_queyCol[] = '`'. $coldata .'`';
    }
 
    $dte_fields = implode (",", $dte_fields);  //make to array
    $dte_columns = implode (",", $dte_columns);  //make to array
    $dte_queyCol = implode (" as ", $dte_queyCol);  //make to array
    ?> 
 
    <script type="text/javascript">
        var editor; // use a global for the submit and return data rendering in the examples
        jQuery(document).ready(function($) {
            editor = new $.fn.dataTable.Editor( {
                "ajax": "<?php echo plugin_dir_url( __DIR__ ).'branches/branch_list_ajax.php'; ?>",
                "table": "#mms_dte",
                "idSrc": "BranchID",
                "fields": [ <?php echo $dte_fields; ?>]
            } );
 
           $('#mms_dte').DataTable( {
                dom: "Bfrtip",
                ajax: '<?php echo plugin_dir_url( __DIR__ )."branches/branch_list_ajax.php"; ?>',
                columns: [ <?php echo $dte_columns; ?> ],
                select: true,
                buttons: [
                    { extend: "create", editor: editor },
                    { extend: "edit",   editor: editor },
                    { extend: "remove", editor: editor },
                    {
                        extend: 'collection',
                        text: 'Export',
                        buttons: [
                            'copy',
                            'excel',
                            'csv',
                            'pdf',
                            'print'
                        ]
                    }
                ]
            } );
        } );
    </script>
 
include( plugin_dir_path( __DIR__ ) . '/dte/lib/DataTables.php' );
 
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, 'wp_mms_branches', 'BranchID' )
    ->fields(
        Field::inst( 'BranchID' )->set(false),  // ID is automatically set by the database on create
        Field::inst( 'Tak' )
            ->validator( Validate::notEmpty( ValidateOptions::inst()
                ->message( 'A Tak is required' ) 
            ) ),
        Field::inst( 'TakNaam' )
            ->validator( Validate::notEmpty( ValidateOptions::inst()
                ->message( 'A Tak is required' ) 
            ) ),
        Field::inst( 'Disabled' ),
        Field::inst( 'BranchPhone' )
             ->validator( Validate::notEmpty( ValidateOptions::inst()
                ->message( 'A BranchPhone is required' ) 
            ) ),
        Field::inst( 'BranchEmail' )
            ->validator( Validate::email( ValidateOptions::inst()
                ->message( 'Please enter an e-mail address' )  
            ) ),
        Field::inst( 'BranchFax' )
             ->validator( Validate::notEmpty( ValidateOptions::inst()
                ->message( 'A BranchFax is required' ) 
            ) ),
        Field::inst( 'BranchAddress1' )
    )
    ->debug(true)
    ->process( $_POST )
    ->json();

**AJAX file Data: **

require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb;
$records = $wpdb->get_results("SELECT * FROM `wp_mms_branches`");
$data_arr = array();
foreach ( $records as $rec ) {
$data_arr[] = array(
  "BranchID" => $rec->BranchID,
  "Tak" => $rec->Tak,
  "TakNaam" => $rec->TakNaam,
  "Disabled" => $rec->Disabled,
  "BranchPhone" => $rec->BranchPhone,
  "BranchEmail" => $rec->BranchEmail,
  "BranchFax" => $rec->BranchFax,
  "BranchAddress1" => $rec->BranchAddress1,
 
);
}
$pages_array = array(
  "data" => $data_arr,
  "options" => array(),
  "files" => array(),
  "debug" => array(
    "query" => "SELECT  `BranchID` as 'BranchID', `Tak` as 'Tak', `TakNaam` as 'TakNaam', `Disabled` as 'Disabled', `BranchPhone` as 'BranchPhone', `BranchEmail` as 'BranchEmail', `BranchFax` as 'BranchFax', `BranchAddress1` as 'BranchAddress1' FROM  `wp_mms_branches` ",
      "bindings"=> []
  )
);
echo json_encode($pages_array);

Kindly check it and let us know. Thanks in Advance...

Replies

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    Editor is a CRUD application (create, read, update and delete). That means you do not need an additional SQL statement to read the data. Editor does all of this automatically for you including the handling of the ajax-data sent to and from the server.

    Just take a closer look at the examples and I am sure you'll figure it out. Good luck :smile:

  • allanallan Posts: 63,531Questions: 1Answers: 10,474 Site admin

    Hi,

    Just to add to that, is branches/branch_list_ajax.php the PHP block of code starting at line 123 in your first block there? That is a separate file isn't it and the formatting has just gone a bit funny on this page?

    Perhaps you could give me a link to your page and I'll dig into the error?

    Allan

Sign In or Register to comment.