[eluser]cebalzer[/eluser]
I'm trying adapter my needs, but now is not working the sorting, can someone help me change the script?
I made some changes to use with PostgreSQL.
Code:
class Datatables_model extends CI_Model {
var $ci;
var $imported;
public function __construct() {
//parent::__construct();
$this->ci = & get_instance();
}
public function generate($table, $columns, $index, $joins, $where, $search, $groupby) {
$joins = 'inner join pessoa_fisica on (pessoa.id_pessoa = pessoa_fisica.id_pessoa)';
$sLimit = $this->get_paging();
$sOrder = $this->get_ordering($columns);
$sWhere = $this->get_filtering($columns, $where, $search);
$rResult = $this->get_display_data($table, $columns, $sWhere, $sOrder, $sLimit, $joins, $where);
$tamanho = $this->total_rows($table);
$rResultFilterTotal = $this->get_data_set_length($table, $sLimit,$joins);
$aResultFilterTotal = $rResultFilterTotal->result_array();
$iFilteredTotal = $tamanho;
$rResultTotal = $this->get_total_data_set_length($table, $index, $sWhere, $joins, $where);
$aResultTotal = $rResultTotal->result_array();
$iTotal = $aResultTotal;
return $this->produce_output($columns, $iTotal, $iFilteredTotal, $rResult);
}
protected function get_paging() {
if ($this->ci->input->post("iDisplayStart") && $this->ci->input->post("iDisplayLength") !== "-1") {
$sLimit = "LIMIT " . $this->ci->input->post("iDisplayLength") . " OFFSET " . $this->ci->input->post("iDisplayStart");
} else {
$iDisplayLength = $this->ci->input->post("iDisplayLength");
if (empty($iDisplayLength)) {
$sLimit = "LIMIT " . " 10 OFFSET 0 ";
} else {
$sLimit = "LIMIT " . $iDisplayLength . " OFFSET " . $this->ci->input->post("iDisplayStart");
}
}
return $sLimit;
}
protected function get_ordering($columns) {
$sOrder = "";
if ($this->ci->input->post("iSortCol_0")!== NULL) {
$sOrder = "ORDER BY ";
for ($i = 0; $i < intval($this->ci->input->post("iSortingCols")); $i++)
$sOrder .= $columns[intval($this->ci->input->post("iSortCol_" . $i))] . " " . $this->ci->input->post("sSortDir_" . $i) . ", ";
$sOrder = substr_replace($sOrder, "", -2);
}
return $sOrder;
}
protected function get_filtering($columns, $where, $search) {
$sWhere = "";
if ($this->ci->input->post("sSearch") != '') {
$sWhere = "WHERE ";
for ($i = 0; $i < count($columns); $i++)
$sWhere .= $columns[$i] . " ILIKE '%" . $this->ci->input->post("sSearch") . "%' OR ";
$sWhere = substr_replace($sWhere, "", -3);
}
if ($sWhere == '') {
if ($where !== '') {
$where = 'WHERE ' . $where;
}
}
return $sWhere . $where;
}
protected function get_display_data($table, $columns, $joins, $sWhere, $sOrder, $sLimit) {
return $this->ci->db->query
('
SELECT ' . str_replace(" , ", " ", implode(", ", $columns)) . '
FROM ' . $table . ' ' . $joins . ' ' . $sWhere . ' ' . $sOrder . ' ' . $sLimit . ' ');
}
public function total_rows($table) {
$sql = "select count(*) as qtd from $table ";
$res = $this->db->query($sql);
$res = $res->row();
return $res->qtd;
}
protected function get_data_set_length($table, $joins, $sLimit) {
$sql = 'select count(*) from ' . $table .' '.$joins. ' ' . $sLimit;
$res = $this->db->query($sql);
return $res;
}
protected function get_total_data_set_length($table, $index, $sWhere) {
return $this->ci->db->query
('
SELECT COUNT(' . $index . ')
FROM ' . $table . '
' . $sWhere . '
');
}
protected function produce_output($columns, $iTotal, $iFilteredTotal, $rResult) {
$aaData = array();
foreach ($rResult->result_array() as $row_key => $row_val) {
foreach ($row_val as $col_key => $col_val) {
if ($row_val[$col_key] == "version")
$aaData[$row_key][$col_key] = ($aaData[$row_key][$col_key] == 0) ? "-" : $col_val;
else {
switch ($row_val[$col_key]) {
default: $aaData[$row_key][] = $col_val;
break;
}
}
}
}
$sOutput = array
(
"sEcho" => intval($this->ci->input->post("sEcho")),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => $aaData
);
return json_encode($sOutput);
}
}