无法在数据表服务器端显示数据

时间:2020-09-22 16:06:12

标签: php json codeigniter

我试图通过join在我的2个表上使用数据表服务器端。 这是模型:

    var $table = 'm_mahasiswa';
    var $column_order = array('nobp', 'nama', 'prodi', 'angkatan', 'judul', 'pembimbing1',  'pembimbing2', 'penguji1', 'penguji4', 'penguji5'); //set column field database for datatable orderable
    var $column_search = array('nobp'); //set column field database for datatable searchable
    var $order = array('nobp' => 'asc'); // default order 

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

    private function _get_datatables_query()
    {
        $this->db->select('m_mahasiswa.nobp, m_mahasiswa.nama, m_mahasiswa.prodi, m_mahasiswa.angkatan, m_mahasiswa.judul, a.nip as nip1, a.nama as pembimbing1, b.nip as nip2, b.nama as pembimbing2, c.nip as nip3, c.nama as penguji1, d.nip as nip4,  d.nama as penguji4, e.nip as nip5, e.nama as penguji5');
        $this->db->from('m_mahasiswa');
        $this->db->join('m_dosen as a', 'm_mahasiswa.nip_pembimbing1 = a.nip','left');
        $this->db->join('m_dosen as b', 'm_mahasiswa.nip_pembimbing2 = b.nip','left');
        $this->db->join('m_dosen as c', 'm_mahasiswa.nip_penguji1 = c.nip','left');
        $this->db->join('m_dosen as d', 'm_mahasiswa.nip_penguji2 = d.nip','left');
        $this->db->join('m_dosen as e', 'm_mahasiswa.nip_penguji3 = e.nip','left');

        $i = 0;

        foreach ($this->column_search as $item) // looping awal
        {
            if($_POST['search']['value']) // jika datatable mengirimkan pencarian dengan metode POST
            {

                if($i===0) // looping awal
                {
                    $this->db->group_start(); 
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }

                if(count($this->column_search) - 1 == $i) 
                    $this->db->group_end(); 
            }
            $i++;
        }

        if(isset($_POST['order'])) 
        {
            $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();
}

这是控制器:

public function get_ajax() {
    $list = $this->M_pembimbing1->get_datatables();
   
    $data = array();
    $no = $_POST['start'];
    foreach ($list as $item) {
        $row = array();
        $row[] = $item->nobp;
        $row[] = $item->nama;
        $row[] = $item->prodi;
        $row[] = $item->angkatan;
        $row[] = $item->judul;
        $row[] = $item->pembimbing1;
        $row[] = $item->pembimbing2;
        $row[] = $item->penguji1;
        $row[] = $item->penguji4;
        $row[] = $item->penguji5;
        // add html for action
        $row[] = '<a href="'.site_url('c_dosen/v_cetak/'.$item->nobp).'" class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i> Edit</a>
                <a href="'.site_url('c_dosen/nilai/'.$item->nobp).'" onclick="return confirm(\'Yakin hapus data?\')"  class="btn btn-danger btn-xs"><i class="fa fa-trash"></i> Delete</a>';
        $data[] = $row;
    }
    $output = array(
                "draw" => $_POST['draw'],
                "recordsTotal" => $this->M_pembimbing1->count_all(),
                "recordsFiltered" => $this->M_pembimbing1->count_filtered(),
                "data" => $data,
            );
    // output to json format
    echo json_encode($output);

}

这是我在视图中使用的javascript

var table;
 
$(document).ready(function() {

    //datatables
   table = $('#tablesiswa').DataTable({ 

      "processing": true, 
      "serverSide": true, 
      "order": [], 

      "ajax": {
        "url": "<?php echo site_url('C_dosen/get_ajax')?>",
        "type": "POST"
      },
      "columnDefs": [
      { 
        "targets": [ 0 ], 
        "orderable": false, 
      },
      ],

    });

  });
</script>

这就是结果:

enter image description here

当我尝试从视图加载数据时,它不显示任何数据,也不显示任何错误。 请帮我。我是这段代码的新手

0 个答案:

没有答案