Pharmacy drug master: switch to AJAX DataTable search
This commit is contained in:
parent
e15d876a3f
commit
12cacc3a9c
|
|
@ -4944,6 +4944,99 @@ class Medical extends BaseController
|
|||
return $deny;
|
||||
}
|
||||
|
||||
$isDataTableRequest = $this->request->getPost('draw') !== null || $this->request->getGet('draw') !== null;
|
||||
|
||||
if ($isDataTableRequest) {
|
||||
$draw = (int) ($this->request->getPost('draw') ?? $this->request->getGet('draw') ?? 1);
|
||||
$start = max(0, (int) ($this->request->getPost('start') ?? $this->request->getGet('start') ?? 0));
|
||||
$length = (int) ($this->request->getPost('length') ?? $this->request->getGet('length') ?? 25);
|
||||
if ($length <= 0 || $length > 500) {
|
||||
$length = 25;
|
||||
}
|
||||
|
||||
if (! $this->db->tableExists('med_product_master')) {
|
||||
return $this->response->setJSON([
|
||||
'draw' => $draw,
|
||||
'recordsTotal' => 0,
|
||||
'recordsFiltered' => 0,
|
||||
'data' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
$search = $this->request->getPost('search');
|
||||
if (! is_array($search)) {
|
||||
$search = ['value' => (string) ($this->request->getGet('search') ?? '')];
|
||||
}
|
||||
$globalSearch = trim((string) ($search['value'] ?? ''));
|
||||
$manualSearch = trim((string) ($this->request->getPost('txtsearch') ?? $this->request->getGet('txtsearch') ?? ''));
|
||||
$searchValue = $manualSearch !== '' ? $manualSearch : $globalSearch;
|
||||
$searchValue = preg_replace('/[^A-Za-z0-9_ \-]/', '', $searchValue);
|
||||
|
||||
$order = $this->request->getPost('order');
|
||||
if (! is_array($order)) {
|
||||
$order = [];
|
||||
}
|
||||
$orderColIndex = isset($order[0]['column']) ? (int) $order[0]['column'] : 0;
|
||||
$orderDir = strtolower((string) ($order[0]['dir'] ?? 'desc')) === 'asc' ? 'ASC' : 'DESC';
|
||||
$orderableColumns = [
|
||||
0 => 'id',
|
||||
1 => 'item_name',
|
||||
2 => 'formulation',
|
||||
3 => 'genericname',
|
||||
4 => 'packing',
|
||||
];
|
||||
$orderBy = $orderableColumns[$orderColIndex] ?? 'id';
|
||||
|
||||
$recordsTotal = (int) ($this->db->table('med_product_master')->countAllResults());
|
||||
|
||||
$baseBuilder = $this->db->table('med_product_master')
|
||||
->select('id, item_name, formulation, genericname, packing');
|
||||
|
||||
if ($searchValue !== '') {
|
||||
$baseBuilder->groupStart()
|
||||
->like('item_name', $searchValue)
|
||||
->orLike('genericname', $searchValue);
|
||||
|
||||
if (ctype_digit($searchValue)) {
|
||||
$baseBuilder->orWhere('id', (int) $searchValue);
|
||||
}
|
||||
|
||||
$baseBuilder->groupEnd();
|
||||
}
|
||||
|
||||
$countBuilder = clone $baseBuilder;
|
||||
$recordsFiltered = (int) $countBuilder->countAllResults();
|
||||
|
||||
$rows = $baseBuilder
|
||||
->orderBy($orderBy, $orderDir)
|
||||
->limit($length, $start)
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
$data = [];
|
||||
foreach ($rows as $row) {
|
||||
$pid = (int) ($row['id'] ?? 0);
|
||||
$editUrl = base_url('Product_master/Product_edit/' . $pid);
|
||||
$data[] = [
|
||||
$pid,
|
||||
esc((string) ($row['item_name'] ?? '')),
|
||||
esc((string) ($row['formulation'] ?? '')),
|
||||
esc((string) ($row['genericname'] ?? '')),
|
||||
esc((string) ($row['packing'] ?? '')),
|
||||
'<button onclick="load_form_div(\'' . $editUrl . '\',\'searchresult\',\'Drug Master : Edit :Pharmacy\');" type="button" class="btn btn-warning btn-sm">Edit</button>',
|
||||
];
|
||||
}
|
||||
|
||||
return $this->response->setJSON([
|
||||
'draw' => $draw,
|
||||
'recordsTotal' => $recordsTotal,
|
||||
'recordsFiltered' => $recordsFiltered,
|
||||
'data' => $data,
|
||||
'csrfName' => csrf_token(),
|
||||
'csrfHash' => csrf_hash(),
|
||||
]);
|
||||
}
|
||||
|
||||
if (! $this->db->tableExists('med_product_master')) {
|
||||
return view('medical/product_search_result', [
|
||||
'product_list' => [],
|
||||
|
|
|
|||
|
|
@ -1,27 +1,104 @@
|
|||
<section class="section">
|
||||
<form id="drug-master-search-form" class="row g-2 mb-3" method="post" action="javascript:void(0)">
|
||||
<?= csrf_field() ?>
|
||||
<div class="col-md-6">
|
||||
<input class="form-control" type="text" id="txtsearch" name="txtsearch" placeholder="Search by Product / Generic / ID">
|
||||
<div class="col-md-5">
|
||||
<input class="form-control" type="text" id="txtsearch" name="txtsearch" placeholder="Search by Product ID / Name / Generic Name">
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<button type="submit" class="btn btn-info">Search Product</button>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<button type="button" id="btnResetSearch" class="btn btn-secondary">Reset</button>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<button onclick="load_form_div('<?= base_url('Product_master/Product_edit/0') ?>','searchresult','Drug Master : New Product :Pharmacy');" type="button" class="btn btn-warning">Add New Product</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div id="searchresult"></div>
|
||||
<div id="searchresult" class="table-responsive">
|
||||
<table id="product_report_list" class="table table-bordered table-striped table-sm align-middle" style="width:100%;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Prod. ID</th>
|
||||
<th>Name</th>
|
||||
<th>Formulation</th>
|
||||
<th>Generic Name</th>
|
||||
<th>Packing Type</th>
|
||||
<th style="width:110px;">Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
$('#drug-master-search-form').off('submit').on('submit', function (event) {
|
||||
event.preventDefault();
|
||||
$.post('<?= base_url('product_master/Product_search') ?>', $(this).serialize(), function (html) {
|
||||
$('#searchresult').html(html || '');
|
||||
});
|
||||
if (!window.jQuery || !$.fn || !$.fn.DataTable) {
|
||||
return;
|
||||
}
|
||||
|
||||
var $form = $('#drug-master-search-form');
|
||||
var dt = $('#product_report_list').DataTable({
|
||||
processing: true,
|
||||
serverSide: true,
|
||||
pageLength: 25,
|
||||
order: [[0, 'desc']],
|
||||
ajax: {
|
||||
url: '<?= base_url('product_master/Product_search') ?>',
|
||||
type: 'POST',
|
||||
data: function (d) {
|
||||
var tokenName = '<?= csrf_token() ?>';
|
||||
var tokenValue = $form.find('input[name="<?= csrf_token() ?>"]').val() || '<?= csrf_hash() ?>';
|
||||
d[tokenName] = tokenValue;
|
||||
d.txtsearch = $('#txtsearch').val();
|
||||
},
|
||||
dataSrc: function (json) {
|
||||
if (json && json.csrfName && json.csrfHash) {
|
||||
$form.find('input[name="' + json.csrfName + '"]').val(json.csrfHash);
|
||||
}
|
||||
return (json && json.data) ? json.data : [];
|
||||
},
|
||||
error: function () {
|
||||
$('#product_report_list tbody').html('<tr><td colspan="6" class="text-center text-danger">Failed to load product list.</td></tr>');
|
||||
}
|
||||
},
|
||||
columnDefs: [
|
||||
{ targets: 5, orderable: false, searchable: false }
|
||||
]
|
||||
});
|
||||
|
||||
$form.off('submit').on('submit', function (event) {
|
||||
event.preventDefault();
|
||||
dt.ajax.reload();
|
||||
});
|
||||
|
||||
$('#btnResetSearch').off('click').on('click', function () {
|
||||
$('#txtsearch').val('');
|
||||
dt.search('').ajax.reload();
|
||||
});
|
||||
|
||||
$('#txtsearch').off('keypress').on('keypress', function (event) {
|
||||
if (event.which === 13) {
|
||||
event.preventDefault();
|
||||
dt.ajax.reload();
|
||||
}
|
||||
});
|
||||
|
||||
$('#product_report_list_filter input[type="search"]').off('keyup').on('keyup', function () {
|
||||
var value = $(this).val();
|
||||
if (value === '' || value.length >= 2) {
|
||||
dt.search(value).draw();
|
||||
}
|
||||
});
|
||||
|
||||
$('#product_report_list_filter input[type="search"]').attr('placeholder', 'Quick filter table...');
|
||||
})();
|
||||
|
||||
// Refresh list after returning from add/edit forms.
|
||||
window.reloadDrugMasterList = function () {
|
||||
if (window.jQuery && $.fn && $.fn.DataTable && $.fn.DataTable.isDataTable('#product_report_list')) {
|
||||
$('#product_report_list').DataTable().ajax.reload(null, false);
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user