Pass a variable to Editor's where clause from Codeigniter View page

Pass a variable to Editor's where clause from Codeigniter View page

dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

I need to pass a variable 'theID' to the Editor's 'where' clause to load a record by 'id'

I am using this tutorial to pair codeigniter with Editor.
http://ci.dubbel16.nl/index.php/2015/12/22/codeigniter-with-datatables-and-editor/

I tried to follow this thread, but not sure what to put in the Editor where clause
https://datatables.net/forums/discussion/42035/editor-passing-a-parameter-to-the-where-clause

Here is my code:

Staff view pages loads custon.js which contains:

    $('#example').DataTable( {
        dom: "Bfrtip",
        ajax: {
            url: "Ajax/Staff",
            type: "POST",
        data: {
        "theID": 4}
        },

Ajax.php Staff method:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Ajax extends CI_Controller {
    public function staff()
    {   
        //Load our library EditorLib 
        $this->load->library('EditorLib');
        
        //`Call the process method to process the posted data
        $this->editorlib->process($_POST);
    }

EditorLib.php method

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class EditorLib {
    
    private $CI = null;
    
    function __construct()
    {
        $this->CI = &get_instance();
    }   

    public function process($post)
    {   
        // DataTables PHP library
        require dirname(__FILE__).'/Editor-PHP-1.7.4/php/DataTables.php';
        
        //Load the model which will give us our data
        $this->CI->load->model('StaffModel');
        
        //Pass the database object to the model
        $this->CI->StaffModel->init($db);
        
        //Let the model produce the data
        $this->CI->StaffModel->getStaff($post);
    }

StaffModel.php

<?php

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;
    
class StaffModel extends CI_Model 
{
    private $editorDb = null;
    
    //constructor which loads the CodeIgniter database class (not required)
    public function __construct()   {
        $this->load->database();
    }    
    
    public function init($editorDb)
    {
        $this->editorDb = $editorDb;
    }
    
    public function getStaff($post)
    {
        // Build our Editor instance and process the data coming from _POST
        // Use the Editor database class
        Editor::inst( $this->editorDb, 'datatables_demo' )
        ->fields(
            Field::inst( 'id' ),
            Field::inst( 'first_name' )->validator( 'Validate::notEmpty' ),
            Field::inst( 'last_name' )->validator( 'Validate::notEmpty' ),
            Field::inst( 'position' ),
            Field::inst( 'email' ),
            Field::inst( 'office' ),
            Field::inst( 'extn' ),
            Field::inst( 'age' )
            ->validator( 'Validate::numeric' )
            ->setFormatter( 'Format::ifEmpty', null ),
            Field::inst( 'salary' )
            ->validator( 'Validate::numeric' )
            ->setFormatter( 'Format::ifEmpty', null ),
            Field::inst( 'start_date' )
            ->validator( 'Validate::dateFormat', array(
                "format"  => Format::DATE_ISO_8601,
                "message" => "Please enter a date in the format yyyy-mm-dd"
            ) )
            ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 )
        )
        ->where('id', 6)  // I want to replace 6 with myID
        ->process( $_POST )
        ->json();    
    }

How do I get this variable to pass through to the Editor so I can search for a specific record by id?
Any help would be appreciated. Thanks!
Next I will figure out how to set the varialbe 'theID' by clicking on a row in the view page.

Answers

  • dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

    I made a simpler version for testing. Now my model looks like

    $data['name'] = 'Bruno';
    // Build our Editor instance and process the data coming from _POST
    $editor = Editor::inst( $db, 'datatables_demo', 'id' )
        ->fields(
            Field::inst( 'first_name' )
        )   
        ->where( function ( $q ) use ($data) {
            $q->where( 'first_name', $data['name']);
        } )
        ->process( $_POST )
        ->json();
    

    I can change the value in
    $data['name'] = 'Bruno';
    and the where function works.
    However, I still cant send a variable from the javascript

        var table = $('#datatables_demo').DataTable( {
            dom: 'Bfrtip',
            ajax: {
                url: 'php/table.datatables_demo.php',
                type: 'POST',
                data: {
                    'name': 'Ashton'}
            },
            columns: [
                {
                    "data": "first_name"
                }
            ],
            select: true,
            lengthChange: false,
            buttons: [
                { extend: 'create', editor: editor },
                { extend: 'edit',   editor: editor },
                { extend: 'remove', editor: editor }
            ]
        } );
    } );
    

    Shouldn't I be able to pull the 'name' value out of the 'data' array?
    Thanks

  • dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

    I think I got it
    I needed to use

    ->where( 'first_name', $_POST['name'])
    

    to get the 'name' value. That is, calling $_POST instead of calling $data

    Javascript:

    var table = $('#datatables_demo').DataTable( {
            dom: 'Bfrtip',
            ajax: {
                url: 'php/table.datatables_demo.php',
                //dataType : "json",
                type: 'POST',
                data: {"name" : "Bruno"}
            },
    

    and model

    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'datatables_demo', 'id' )
        ->fields(
            Field::inst( 'first_name' )
        )   
        ->where( 'first_name', $_POST['name'])
        ->process( $_POST )
        ->json();
    

    I hope this helps another newbie like me!

This discussion has been closed.