Pagination didn't change data on data table server side

Pagination didn't change data on data table server side

fahmifahmi Posts: 1Questions: 1Answers: 0
edited February 2017 in Free community support

hai, im using codeginiter and data table server-side. i had 11 data and when i click for page 2 didnt change anthing, can someone help me . Here's my code :

view.php :

<script type="text/javascript">
 
var table;
 
$(document).ready(function() {
 
    //datatables
    table = $('#table').DataTable({ 
 
        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.
        "order": [], //Initial no order.
 
        // Load data for the table's content from an Ajax source
        "ajax": {
            "url": "<?php echo site_url('Buku/ajax_list')?>",
            "type": "POST"
        },
 
        //Set column definition initialisation properties.
        "columnDefs": [
        { 
            "targets": [ 0 ], //first column / numbering column
            "orderable": false, //set not orderable
        },
        ] 
    });
 
});
</script>

my controller :

public function ajax_list(){
        $list = $this->Mbuku->get_datatables();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $a) {
            if ($a->gambar_1 != '') {
                $b = $a->gambar_1;
            }elseif ($a->gambar_2 != '') {
                $b = $a->gambar_2;
            }elseif ($a->gambar_3 != '') {
                $b = $a->gambar_3;
            }
            $no++;
            $row = array();
            $row[] = $no;
            $row[] = $a->noisbn;            
            $row[] = '<img src="'.base_url().'assets/images/buku/'.$b.'" width="60" height="80">';
            $row[] = $a->judul;
            $row[] = $a->nama_penerbit;
            $row[] = $a->nama_jenis;
            $row[] = $a->stok;
            $row[] = $a->harga;
            $row[] = '<a href="'.base_url().'index.php/Buku/edit/'.$a->noisbn.'" class="btn btn-info btn-xs">Edit</a><button class="btn btn-danger btn-xs" onclick="myFunction('.$a->noisbn.')">Delete</button>';
            $data[] = $row;
        }
 
        $output = array(
                        "draw" => 0,
                        "iTotalDisplayRecords" => $this->Mbuku->count_all(),
                        "iTotalRecords" =>$this->Mbuku->count_all(),
                        "data" => $data
                );
        //output to json format
        echo json_encode($output);
    }

my model :

var $table = 'buku';
    var $column_order = array(null,'buku.*','nama_penerbit','nama_jenis');
    var $column_search = array('noisbn','judul','nama_penerbit','nama_jenis','stok','harga');
    var $order = array('judul' => 'asc');

    public function __construct()
    {
        $this->load->database();
    }

    private function _get_datatables_query(){
        $this->db->from($this->table);
        $this->db->join('penerbit', 'buku.id_penerbit = penerbit.id_penerbit');
        $this->db->join('jenis', 'buku.id_jenis = jenis.id_jenis');
        $this->db->where('buku.status','1');

        $i = 0;
     
        foreach ($this->column_search as $item) // loop column 
        {
            if($_POST['search']['value']) // if datatable send POST for search
            {
                 
                if($i===0) // first loop
                {
                    $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }
 
                if(count($this->column_search) - 1 == $i) //last loop
                    $this->db->group_end(); //close bracket
            }
            $i++;
        }
         
        if(isset($_POST['order'])) // here order processing
        {
            $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } 
        else if(isset($this->order))
        {
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order)]);
        }
    }

    function get_datatables()
    {
        $this->_get_datatables_query();
        if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);
        $query = $this->db->get();
        return $query->result();
    }
 
    function count_filtered()
    {
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    }

    public function count_all()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }


This is make me crazy. hope you can solve it. Thanks

This discussion has been closed.