So I tried this tutorial : https://mbahcoding.com/tutorial/php/codeigniter/codeigniter-ajax-crud-modal-server-side-validation.html
and my data shows like this : image of how the data & the features looks
I just want to show the table and using the search feature on the green sign in the picture. But, when I follow the tutorial, there is some features showing (ex : red & blue sign in the picture).
Where I can change to remove the red feature in the picture and move the blue sign feature in the picture to the green sign?
I am still learning at how ajax works. Sorry if my explanation is too complicated. But, I hope you understand... Hope you can help me...
This is the function that I use at my Model - detkelModel.php:
var $table = 'detail_keluarga';
var $column_order = array('nama', 'hubungan', 'personilid', 'lahir', null); //set column field database for datatable orderable
var $column_search = array('nama', 'hubungan', 'personilid', 'lahir'); //set column field database for datatable searchable just nama, hubungan, personilid, lahir are searchable
var $order = array('id' => 'desc'); // default order
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();
}
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 my View of the table and the searchbar - detkel.php:
<div class="navbar navbar-expand navbar-white navbar-light">
<!--left navbar / Data Keluarga label-->
<div class="col-sm-6 float-left">
<h5>Data Keluarga Personil</h5>
</div>
<!-- Right navbar links -->
<ul class="navbar-nav ml-auto">
<!-- Navbar Search, filter, plus data -->
<li class="nav-item">
<div class="form-inline">
<div class="input-group" data-widget="search-bar">
</div>
</div>
</li>
<!-- Filter Dropdown Menu -->
<li>
</li>
<!-- Plus data dropdown menu -->
<li>
<a class="btn btn-default" data-toggle="dropdown" href="#">
</a>
<div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
</div>
</li>
</ul>
</div>
<div class="card">
<div class="table-responsive p-0">
<table id="table" class="table table-hover text-nowraps">
<thead>
<tr>
<th>Nama</th>
<th>id personil</th>
<th>Hubungan</th>
<th>Tanggal Lahir</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
This is the script in my view
<script type="text/javascript">
var save_method; //for save method string
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('DetkelController/ajax_list') ?>",
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [{
"targets": [-1], //last column
"orderable": false, //set not orderable
}, ],
});
});
</script>
This is ajax_list() in my Controller DetkelController.php:
public function ajax_list()
{
$list = $this->dk->get_datatables();
$data = array();
//$no = $_POST['start'];
foreach ($list as $dk) {
//$no++;
$row = array();
$row[] = $dk->nama;
$row[] = $dk->hubungan;
$row[] = $dk->personilid;
$row[] = $dk->lahir;
//add html for action
$row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person(' . "'" . $dk->id . "'" . ')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person(' . "'" . $dk->id . "'" . ')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->dk->count_all(),
"recordsFiltered" => $this->dk->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}