How to add staterestore parms to what editor php libs return

How to add staterestore parms to what editor php libs return

jfixsenjfixsen Posts: 2Questions: 2Answers: 0
edited September 2022 in Free community support

Hello,

I am using the editor server-side php libraries to return data to the browser. It works great. I also am using "extend: 'savedStates'"
to give the user the ability to name and save different queries. I want to save these savedStates to the db, so am using the ajax parm for that
to call a php script (ss.php) which saves off the states. That also works fine. My question is about loading the savedStates back up. If I don't use
the ajax call to save the states, and let it default to local storage (I see and can change the data in local storage), it automatically gets loaded along
with what the editor php lib returns. But, since i'm using ajax to save the states instead of local, I need to somehow add the stateRestore data
into what the editor instance returns?

If I wasn't using editor php libs in my table's ajax php script, I could do something like this:

$data = file_get_contents('2500.txt');  # equivalent to what editor libs are doing
$json = json_decode($data, true);
$json['stateRestore'] = $stateRestoreInfoFromDB;  # add the stateRestore data to the object being returned

Ie, it would return something like this:

{ 
    "data": [
        [ "1", "Armand", "Warren", "56045", "Taiwan, Province of China" ],
        [ "2", "Xenos", "Salas", "71090", "Liberia" ],
    ],
    "stateRestore": {
        "Scroll and Order 3": {"start":1036,"length":54,"order":[[2,"asc"]],"scroller":{"topRow":1069.1081081081081,"baseScrollTop":39235,"baseRowTop":1060.4054054054054,"scrollTop":39557}}
    }
}

But since I'm using php editor lib to return the data, I'm not sure how to include the stateRestore data.

Here is my editor instance code:

function process_dt_ss($table,$cols_edit,$edit_flag)
{
  global $db;

  // Build our Editor instance and process the data coming from _POST
  $e = Editor::inst( $db, "$table" );

  foreach($cols_edit as $col)
  {
    $e->fields(Field::inst($col));
  }

  $write_parm = false;
  if($edit_flag) { $write_parm = true; }

  $e->debug(true)
    ->write( $write_parm )   # this is needed to use editor libs for free, Ie we are not writing, just reading.
                             # If you want to be able "edit" a table, you will need to pay then remove this.
    ->process( $_POST )
    # its like I need something like this:
    # ->stateRestore($stateRestoreData)
    ->json();

    return(0);

}

Any help is appreciated. Thanks!

-Jason

Here is a link to a live page: https://www.mystockdata.com/dividend-search/
Here is my full js:

var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        "ajax": "{$_SERVER['SCRIPT_URI']}",
        "table": "#stocks_table",
        "fields": [ $cols_fields ]
    } );

    $('#stocks_table').DataTable( {
        dom: 'QlBfrtip',
        ajax: {
            url: "{$_SERVER['SCRIPT_URI']}",
            type: "POST",
            data: {type: 'master', table: '{$aConfig['0']['master_table']}'},
        },
        select: true,
        serverSide: true,
        columns: [ $cols_data ],
        columnDefs: [
          {
            'targets': [{$aConfig[0]['master_sym_col']}],
            'createdCell':  function (td, cellData, rowData, row, col) {
              $(td).attr('class', '');
            }
          },
          {
          'targets': '_all',
          'createdCell':  function (td, cellData, rowData, row, col) {
             $(td).attr('data-bs-toggle', 'modal');
             $(td).attr('data-bs-target', '#stock-modal');
             $(td).attr('data-id', rowData.sym);
             $(td).attr('class', 'sym-class');
          }
          }
       ],
       buttons: [
          $edit
          {
            extend: 'savedStates',
            config:
               {
                    // this writes all savedstate commands to the os.  it is what needs saved and
                    // read from the database for each user to keep users states saved off.
                    ajax: "/ss.php",
                    creationModal: true,
                    preDefined:
                    {
                        "Starting Point": {
                           searchBuilder:
                           {
                                 criteria:[
                                     {$aConfig[0]['master_search']}
                                 ],
                                 logic: 'AND'
                           }
                       }
                    }
               },
          },
          // uncomment his is you want a separate button to save the query, and uncomment the above collection
          { extend: 'createState', text: 'Save' },
          {     extend: 'colvis' , text: 'Hide Cols' },
          {
                extend: 'collection',
                text: 'Export',
                buttons: [
                    'copy',
                    'excel',
                    'csv',
                    'pdf',
                    'print'
                ],
           },
       ],
       language: {
          "zeroRecords": "No data Found",
          "processing": 'Loading',
          "buttons":  { savedStates: { _: 'Saved Queries' } },
       },
       pageLength: 25,
       colReorder: true,
       order: [{$aConfig[0]['master_order']}],
    } );

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    The $e->json(); method is a shorthand for doing:

    $data = $e->data();
    
    echo json_encode($data);
    

    If you use the ->data() method it just returns the assc. array which you can tack items onto as you need.

    Allan

Sign In or Register to comment.