Serverside & Searching in Editor

Serverside & Searching in Editor

GstgGstg Posts: 66Questions: 4Answers: 0
edited July 2022 in Editor

We think our basic code structure may be incorrect. We are looking to do ServerSide processing and simple Sorting by a reverse key. We can't seem to change the order no mater what we change the $Primarykey to and no matter what we change the "order: [[8,'desc']], to ... I'm thinking maybe we have this in the Editor, when it needs to be in the DataTable function? But when we move the ServerSide function from the Editor to the DataTable function, we get very strange results, like no pagination, all the filters stop working and the Showing # of # (###) records simply comes up with "Showing 0 of 0 (NAN)". We honestly no longer need the editor section, as we are no longer editing, but it seems to be the only way the processing works for us atm. Thoughts?

$(document).ready( function () {
       //$.fn.dataTable.moment( 'HH:mm MMM D, YY' );
       //$.fn.dataTable.moment( 'dddd, MMMM D, YYYY - hh:mm A' );

    var title;
  
    var editor3 = new $.fn.DataTable.Editor({
               "ordering": true,
               "processing": true,
               "serverSide": true,
                order: [[8,'desc']],

        ajax:  "/DataTables/myplace.php",
            table: "#sort_table3",




 
// ----------------------------------------------------------------------------------------------------------
// --------------------- additional code removed to save space -------------------------------
// ----------------------------------------------------------------------------------------------------------

    var table3 = $('#sort_table3')
        .DataTable( {
            ajax: {
                url: '/DataTables/myplace.php',
                data: function (d) {
                    d.anyDay = anyDay;
                    d.MyListings = MyListings;
                },
                                error: function (xhr, error, code)
                                {
                                      console.log(xhr);
                                      console.log(code);
                                }
            },

// -------------------- Probably UNNEEDED -------------------
//                   "ordering": true,
//                   "processing": true,
//                   "serverSide": true,
//                   order: [8,'desc'],
// -------------------- Probably UNNEEDED -------------------
// ---------------------------------------------------------------------------
// ----------------- MyPlace.php - Ajax call code ------------------
// ---------------------------------------------------------------------------

<?php

SESSION_START();

/*
 * Editor server script for DB table 
 * Created by http://editor.datatables.net/generator
 */

// DataTables PHP library and database connection
include( "Editor-PHP-2.0.5/lib/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;


if (isset($_POST['action'])) {
    echo '{}';
    return;
}

// $primaryKey = 'posted_time';
$primaryKey = 'ask_name';

// --------------- SET ENCRYPTION -----------------------
$encr_iv=$_SESSION['encr_iv'];
$encr_key=$_SESSION['encr_key'];
// ---------------------------------------------------------------------------------

// Build our Editor instance and process the data coming from _POST
 Editor::inst( $db, 'questions', $primaryKey)
    ->fields(

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    serverSide is a DataTables option. It will have no effect on the table or the form in the Editor configuration.

    What might be happening is this:

    if (isset($_POST['action'])) {
    

    Since you have that check, and DataTables doesn't send an action parameter by default, then it will just do that echo on line 30.

    I'd suggest:

    1. Removing that (and the Editor client-side block if you no longer need that)
    2. Add serverSide to the DataTables initialisation.
    3. Add method: 'post' to the ajax object for the DataTable so it POSTs its request, since I guess that is what the PHP is looking for.
    4. Add ->write(false) just before the ->process(...) call, so no monkey can "hack" the script and write to the database if they send a POST request.

    Allan

  • GstgGstg Posts: 66Questions: 4Answers: 0

    Thanks so much for the suggestions.

    Any idea why the sorting is not working? Do you see any blatant issues? We've tried both using the ORDER and resetting the $PrimaryKey

    Thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    The primary key should have no effect on the ordering.

    Since you are using the Editor PHP libraries for the server-side processing, add ->debug(true) just before the ->process(...) call. That will dump the executed SQL into the JSON object returned. With that in place, could you then use the debugger to give me a trace please - click the Upload button and then let me know what the debug code is.

    Thanks,
    Allan

Sign In or Register to comment.