Skip to content

Latest commit

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

#DataTable Library for Codeigniter

This library requires DataTables 1.10 >

  • Generates Necessary JSON For Sever Side Tables

  • Handles Sorting & Column Based Searching

  • Builds the select automatically from the DataTable column names

  • Append custom SQL expressions to the generated Select statement

  • Append a static where clause

  • Formatters For Date, Percent, Currency, and Boolean

  • Utilizes CI Active Record

  • Drastically Reduces PHP Code Necessary To Generate a Server Side Table

Install

  • jQuery

    <script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>

  • DataTables

    <script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>

    <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">

  • DataTable Library

    Copy Datatable.php to the libraries folder of your CI install

Parameters

NameDescriptionRequired
modelModel classname implmenting the DatatableModel interfaceYES
rowIdColColumn name used to assign the row id values in the tableNO

DatatableModel Interface

NameDescriptionReturn
appendToSelectStrAdds additional columns or SQL Expressions to the generated SELECTAssoc. ArrayKey = Column Alias, Value = Column Name or Expression ORNULL
fromTableStrSpecify the table name to select from. Tip: You can also include an alias here return 'mytable a';**String **
joinArrayJoin additional tables for DataTable columns to reference. Tip: Join Types CAN be specifed by using a pipe in the key value 'table_to_join b&#124;left outer'Assoc. ArrayKey=Table To Join Value=SQL Join Expression.
whereClauseArrayAppend Static SQL to the generated Where ClauseAssoc. ArrayKey= Column Name Value=Value To Filter ORNULL

Basic DatatableModel Implementation

class store_dt extends MY_Model implements DatatableModel{
publicfunctionappendToSelectStr() {
returnNULL;
}
publicfunctionfromTableStr() {
return'store s';
}
publicfunctionjoinArray(){
returnNULL;
}
publicfunctionwhereClauseArray(){
returnNULL;
}
}

More Advanced DatatableModel Implementation

class state_dt extends MY_Model implements DatatableModel{
publicfunctionappendToSelectStr() {
returnarray(
'city_state_zip' => 'concat(s.s_name, \'\', c.c_name, \'\', c.c_zip)'
);
}
publicfunctionfromTableStr() {
return'state s';
}
publicfunctionjoinArray(){
returnarray(
'city c|left outer' => 'c.state_id = s.id',
'user u' => 'u.state_id = s.id'
);
}
publicfunctionwhereClauseArray(){
returnarray(
'u.id' => $this -> ion_auth -> get_user_id() );
}
}

Controller Example

class DataTableExample extends CI_Controller {
publicfunctiondataTable() {
//Important to NOT load the model and let the library load it instead. $this -> load -> library('Datatable', array('model' => 'state_dt', 'rowIdCol' => 'c.id'));
//format array is optional, but shown here for the sake of example$json = $this -> datatable -> datatableJson(
array(
'a_date_col' => 'date',
'a_boolean_col' => 'boolean',
'a_percent_col' => 'percent',
'a_currency_col' => 'currency'
)
);
$this -> output -> set_header("Pragma: no-cache");
$this -> output -> set_header("Cache-Control: no-store, no-cache");
$this -> output -> set_content_type('application/json') -> set_output(json_encode($json));
}
}

Table HTML In View

<tableid="myDataTable"><thead><tr><th>State</th><th>City</th><th>Zip</th><th>Combined Exp</th></tr></thead><tbody></tbody></table>

JavaScript To Call Controller

**Custom Expressions: ** SQL Expressions setup in the model in the appendToSelectStr() must be reference by the $ in the data property of the columns array.

$('#myDataTable').dataTable({processing: true,serverSide: true,ajax: '/index.php/DataTableExample/dataTable',columns: [{data: "s.s_name"},{data : "c.c_name"},{data : "c.c_zip"},{data: "$.city_state_zip"}//refers to the expression in the "More Advanced DatatableModel Implementation"]});

Resources

License Information

License: The MIT License (MIT), http://opensource.org/licenses/MIT

About

CodeIgniter Library For Server Side DataTables 1.10 >

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages