Pass a variable to Editor's where clause from Codeigniter View page
Pass a variable to Editor's where clause from Codeigniter View page
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
I made a simpler version for testing. Now my model looks like
I can change the value in
$data['name'] = 'Bruno';
and the where function works.
However, I still cant send a variable from the javascript
Shouldn't I be able to pull the 'name' value out of the 'data' array?
Thanks
I think I got it
I needed to use
to get the 'name' value. That is, calling $_POST instead of calling $data
Javascript:
and model
I hope this helps another newbie like me!