I'm using codeigniter with datatables and i want to order a select by a column.
How can i do that ?
$this->datatables->select('col_1, col_2, col_3');
$this->datatables->from('table');
....$this->datatables->order ?!?
Thanks
I'm using codeigniter with datatables and i want to order a select by a column.
How can i do that ?
$this->datatables->select('col_1, col_2, col_3');
$this->datatables->from('table');
....$this->datatables->order ?!?
Thanks
use:
$this->db->order_by("column name", "desc");
You can use the aaSorting parameter to order the table on initialization.
$(document).ready( function() {
$('#example').dataTable( {
"aaSorting": [[2,'asc'], [3,'desc']]
} );
} );
where 2 and 3 are column's index
You can use this way i am controlling here all required value with codeigniter DATA-TABLE and MYSQL select include a small joining
function get_ComponentList($rowperpage, $row, $search='',$order, $dir)
{
$this->db->select('a.component_id, a.component_name as component_name, a.eco_code as eco_code, b.component_name as parent_name');
$this->db->from('component_info as a');
$this->db->join('component_info as b', 'b.component_id = a.parent_id', 'left');
$this->db->order_by($order,$dir);
if($search != ''){
$this->db->like('a.component_name', $search);
$this->db->or_like('a.eco_code', $search);
$this->db->or_like('b.component_name', $search);
}
$this->db->limit($rowperpage,$row);
$query = $query->result();
return $query;}
As you want to use with $this->datatables, you need to make a custom function for that, add the below custom function in Datatables.php library file:
public function corder_by($column, $type = ASC)
{
$this->order_by[] = array($column, $type);
$this->ci->db->order_by($column, $type);
return $this;
}
If the sorting type is not defined "ASC/DESC", then it will by default sort as "Ascending".
And use it as:
$this->datatables->corder_by('column_name','desc');
© 2022 - 2024 — McMap. All rights reserved.