Editor:Passing a parameter to the where clause

Editor:Passing a parameter to the where clause

Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10
edited April 2017 in Free community support

I'm sure I'm overlooking this somewhere, but it seems like ajax data is meant to pass along a parameter to the Editor instance in the PHP file so it can act on it with the ->where() function.

I see the examples on how to use ajax data, but how do I refer to what is passed along in the PHP file?

For example, suppose I am trying to get a list of schools, filtered by the city "Athens, Georgia".

Here's my JS:

<script type="text/javascript" language="javascript" class="init">
   var editor; // use a global for the submit and return data rendering
    
   $(document).ready(function() {
   
       editor = new $.fn.dataTable.Editor( {
    ajax: {
        url: '../SchoolIndex.php'
    },
           table: '#school_index',
           fields: [ {
                   label: 'School:',
                   name: 'aSchool'
               }, {
                   label: 'City:',
                   name: 'aCity'
               }
           ]
       } );
    
       var table = $('#school_index').DataTable( {
           dom: 'Bfrtip',
           ajax: {
        url: '../SchoolIndex.php',
        data: {
            'cityToFilterBy': 'Athens, Georgia'
        }
           },
           columns: [
               {
                   data: null,
                   defaultContent: '',
                   className: 'select-checkbox',
                   orderable: false
               },
               { data: 'aTeam' },
               { data: 'aCity' }
           ],
           order: [ 1, 'asc' ],
           select: true,
           buttons: [
               { extend: 'create', editor: editor },
               { extend: 'edit', editor: editor },
               { extend: 'remove', editor: editor }
           ]
      } );
    
   } );
</script>

And here is the PHP:

<?php

// DataTables PHP library
include "../../Editor/php/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;

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'sa_school' )
   ->fields(
      Field::inst( 'aTeam' )->validator( 'Validate::notEmpty' ),
      Field::inst( 'aCity' )
   )
   ->where( 'aCity', 'Athens, Georgia' )
   ->process( $_POST )
   ->json();

In this example I can get it to work by putting in "Athens, Georgia" directly into the ->where() function like I did above, but I can't figure out how to pass the value from ajax data like I think it was designed to do.

Is there an example I'm overlooking?

This question has an accepted answers - jump to answer

Answers

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10
    Answer ✓

    OK, I solved it my normal way -- fight with it for hours, post a question in a public forum, then find the answer within 10 minutes . . .

    I didn't include type: 'POST'. It now reads:

    ajax: {
       url: '../SchoolIndex.php',
       type: 'POST',
       data: {
          'cityToFilterBy': 'Athens, Georgia'
       }
    

    and it works.

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Thanks for posting back. Good to hear you've got it working now.

    Allan

This discussion has been closed.