I have following datatable:
var dataTable = $('#dataTable').DataTable({
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
url: "<?php echo base_url() . 'crud/fetch_patient'; ?>",
type: "POST",
},
"columnDefs": [{
targets: 2,
render: DataTable.render.datetime('DD.MM.YYYY'),
type: 'datetime-moment'
}, {
targets: 7,
"orderable": false,
}, ],
});
First column is last name, second column is first name. I would like to search lastname (column 0) and first name (column 1) with space after lastname in datatable searchbox. Example "Doe John". When I write Doe and space ('Doe ') in searchbox my datatable is empty. Here is what I tried:
"ajax": {
url: "<?php echo base_url() . 'crud/fetch_patient'; ?>",
type: "POST",
data: function(d) {
d.search_terms = $('#dataTable_filter input').val().trim().split(' ');
},
success: function(data) {
console.log(data);
}
},
Codeigniter - controller
function fetch_patient()
{
$this->load->model("crud_model");
$u_id = $this->session->userdata('u_id');
$fetch_data = $this->crud_model->make_datatables($u_id);
$data = array();
foreach ($fetch_data as $row) {
$sub_array = array();
$sub_array[] = $row->pr_lname;
$sub_array[] = $row->pr_fname;
$sub_array[] = $row->pr_bdate;
$sub_array[] = date_diff(date_create($row->pr_bdate), date_create('now'))->y;
$sub_array[] = mb_strlen($row->pr_number) == 9 ? preg_replace("/(\\w{3})/uim", "$1 ", $row->pr_number) : $row->pr_number;
$sub_array[] = $row->pr_diseases != '' ? 'Tak' : 'Nie';
$sub_array[] = $row->pr_date;
$sub_array[] = '<a class="btn btn-brown p_tb_03" href="' . base_url() . 'maincontrol/edit_form/' . $row->pr_id . '">Edytuj</a><a class="btn btn-green ml_5 p_tb_03" href="' . base_url() . 'maincontrol/patientdataview/' . $row->pr_id . '">Przeglądaj</a><a class="btn btn-medium-dark ml_5 p_tb_03" href="' . base_url() . 'maincontrol/xraysview/' . $row->pr_id . '#xrays' . '">RTG</a><a class="btn btn-blue ml_5 p_tb_03" href="' . base_url() . 'maincontrol/add_findings/' . $row->pr_id . '#findings' . '"><i class="fa fa-plus"></i> Zabieg</a><a class="btn btn-medium-dark ml_5 p_tb_03" href="' . base_url() . 'maincontrol/add_xrays/' . $row->pr_id . '#xrays' . '"><i class="fa fa-plus"></i> RTG</a>';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $this->crud_model->get_all_data($u_id),
"recordsFiltered" => $this->crud_model->get_filtered_data($u_id),
"data" => $data
);
echo json_encode($output);
}
Codeigniter - model
var $table = "patient_record";
var $select_column = array("pr_id", "pr_lname", "pr_fname", "pr_bdate", "pr_bdate", "pr_number", "pr_diseases", "pr_date");
var $order_column = array("pr_lname", "pr_fname", "pr_bdate", "pr_bdate", "pr_number", "pr_diseases", "pr_date");
function make_datatables($pr_user_id)
{
$this->db->select($this->select_column);
$this->db->from($this->table);
$this->db->where('pr_user_id', $pr_user_id);
if (isset($_POST["search"]["value"])) {
$this->db->group_start();
$this->db->like("pr_date", $_POST["search"]["value"]);
$this->db->or_like("pr_lname", $_POST["search"]["value"]);
$this->db->or_like("pr_fname", $_POST["search"]["value"]);
$this->db->or_like("pr_bdate", $_POST["search"]["value"]);
$this->db->or_like("pr_number", $_POST["search"]["value"]);
$this->db->or_like("pr_diseases", $_POST["search"]["value"]);
$this->db->group_end();
}
if (isset($_POST["order"])) {
$this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
if ($_POST["length"] != -1) {
$this->db->limit($_POST['length'], $_POST['start']);
}
$query = $this->db->get();
return $query->result();
}
function get_filtered_data($pr_user_id)
{
$this->db->select($this->select_column);
$this->db->from($this->table);
$this->db->where('pr_user_id', $pr_user_id);
if (isset($_POST["search"]["value"])) {
$this->db->group_start();
$this->db->like("pr_date", $_POST["search"]["value"]);
$this->db->or_like("pr_lname", $_POST["search"]["value"]);
$this->db->or_like("pr_fname", $_POST["search"]["value"]);
$this->db->or_like("pr_bdate", $_POST["search"]["value"]);
$this->db->or_like("pr_number", $_POST["search"]["value"]);
$this->db->or_like("pr_diseases", $_POST["search"]["value"]);
$this->db->group_end();
}
if (isset($_POST["order"])) {
$this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
$query = $this->db->get();
return $query->num_rows();
}
function get_all_data($pr_user_id)
{
$this->db->select("pr_date, pr_lname, pr_fname, pr_bdate, pr_number, pr_diseases");
$this->db->from($this->table);
$this->db->where('pr_user_id', $pr_user_id);
return $this->db->count_all_results();
}
This code shows me an empty array in console when I write 'Doe John'. Could you help me please? Thanks in advance.