0

How do you add columns to an action button such as the detail button in AJAX? This is my code: Controller:

    public function ajax_list()
    {
        $list = $this->th_ajaran->get_datatables();
        $data = array();
        $no = $_POST['start'];
        $tools = $_POST['start'];
        foreach ($list as $th_ajaran) {
            $no++;
            $row = array();
            $row[] = $no;
            $row[] = $th_ajaran->nama_kelas;
            $row[] = $th_ajaran->jurusan;

            $data[] = $row;
        }

        $output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->th_ajaran->count_all(),
                        "recordsFiltered" => $this->th_ajaran->count_filtered(),
                        "data" => $data,
                );
        //output to json format
        echo json_encode($output);
    } 

This is My Model

<?php
class Th_Ajaran_kelas_model extends CI_Model {

    var $table = 'th_ajaran';
    var $column_order = array(null, 'nama_kelas','jurusan'); //set column field database for datatable orderable
    var $column_search = array('nama_kelas','jurusan'); //set column field database for datatable searchable 
    var $order = array('th_ajaran' => 'asc'); // default order 

    private function _get_datatables_query()
    {

        //add custom filter here
        if($this->input->post('th_ajaran'))
        {
            $this->db->where('th_ajaran', $this->input->post('th_ajaran'));
        }
        if($this->input->post('nama_kelas'))
        {
            $this->db->like('nama_kelas', $this->input->post('nama_kelas'));
        }
        if($this->input->post('jurusan'))
        {
            $this->db->like('jurusan', $this->input->post('jurusan'));
        }

        $this->db->from($this->table);
        $this->db->join('th_ajaran_kelas', 'th_ajaran_kelas.id_th_ajaran = th_ajaran.id_th_ajaran');
        $this->db->join('kelas', 'th_ajaran_kelas.id_kelas = kelas.id_kelas');

        $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)]);
        }
    }

    public 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();
    }

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": "",
            "type": "POST",
            "data": function ( data ) {
                data.th_ajaran = $('#th_ajaran').val();
            }
        },

        //Set column definition initialisation properties.
        "columnDefs": [
        { 
            "targets": [ 0 ], //first column / numbering column
            "orderable": false, //set not orderable
        },
        ],

    });

    $('#btn-filter').click(function(){ //button filter event click
        table.ajax.reload();  //just reload table
    });
    $('#btn-reset').click(function(){ //button reset event click
        $('#form-filter')[0].reset();
        table.ajax.reload();  //just reload table
    });

});


Vickel
  • 7,879
  • 6
  • 35
  • 56
  • https://stackoverflow.com/questions/22471862/how-do-i-add-button-on-each-row-in-datatable – Mohammedshafeek C S May 07 '20 at 17:19
  • But in defining the data the table is run on the controller then the new data is called on ajax. If the link you provide is done in ajax definition. How do I implement it? *sorry my bad english – Galih Wahyu May 08 '20 at 02:19

0 Answers0