Ignited+datatables -> order by
Asked Answered
E

4

6

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

Elli answered 6/8, 2014 at 9:10 Comment(0)
I
7

use:

$this->db->order_by("column name", "desc");
Interchange answered 6/8, 2014 at 10:45 Comment(2)
yes it works. thanks bro. all this time i was searching for a datatables function.Elli
it do work with datatables, but when you use datatables its own column ordering. it will give error. it will stay on top and rest fields go up and down, which makes the record bad :(Casia
C
2

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

Capitalist answered 21/4, 2015 at 12:45 Comment(0)
S
2

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;}
Spacious answered 15/5, 2019 at 4:15 Comment(0)
A
0

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');
Axiomatic answered 14/6, 2022 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.