Load select mysql in specific column of datatables with Ajax

Asked

Viewed 108 times

0

Good morning to all, I have a datatables with 4 columns. It serves to control the processing time of some routines. In the first 3 columns, I load the data from a table with name, description and expected time, which are standard information. In the last column, I have to move up the final processing times of these routines, which are in another table of the database.

This is the question: how can I, if possible, load these values in the fourth column of the datatables, from another database table?

HTML

 <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th bgcolor="#BDBDBD">Rotina</th>
                <th bgcolor="#BDBDBD">Descricao</th>
                <th bgcolor="#BDBDBD">Previsto</th>
                <th bgcolor="#BDBDBD">Realizado</th>
            </tr>
        </thead>
        <tbody>
        </tbody>

        <tfoot>
        <tr>
            <th bgcolor="#BDBDBD"></th>
            <th bgcolor="#BDBDBD"></th>
            <th bgcolor="#BDBDBD"></th>
            <th bgcolor="#BDBDBD"></th>
        </tr>
        </tfoot>
    </table>

AJAX

$(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('projeto/ajax_list')?>",
        "type": "POST"  
    },

    //Set column definition initialisation properties.
    "columnDefs": [
    { 
        "targets": [ -1 ], //last column
        "orderable": false, //set not orderable
    },
    ],

});
});

PHP

public function ajax_list()
{
    $list = $this->projeto->get_datatables();
    $data = array();
    $no = $_POST['start'];
    foreach ($list as $projeto) {
        $no++;
        $row = array();
        $row[] = $projeto->rotina;
        $row[] = $projeto->descricao;
        $row[] = $projeto->previsto;
        $row[] = $projeto->realizado;     

        $data[] = $row;
    }

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


private function _get_datatables_query()
{

    $this->db->from($this->table);

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

The idea here is I have a combobox to select a date and with that, in the fourth column, is filled the final processing of routines.

There is this possibility to load information from two tables of the database in the same Datatables?

Thank you,

  • Guys, could you give us some strength or some idea for this situation? Thank you

  • Someone can help out ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.