fix(ipd_discharge): remove fragile tbody early-return guard in medicine remove handler

This commit is contained in:
DevSoftTech Server 2026-09-01 17:51:23 +05:30
parent fe26588dca
commit 1f8651e3b3

View File

@ -6643,19 +6643,32 @@ $historyFields = [
$('#discharge_med_name').trigger('focus');
});
$(document).on('click', '.btn-remove-discharge-med', function(ev) {
$(document).off('click', '.btn-remove-discharge-med').on('click', '.btn-remove-discharge-med', function(ev) {
if (ev) ev.preventDefault();
var $btn = $(this);
var $tr = $btn.closest('tr');
var tbody = section.querySelector('#discharge_medicine_tbody');
if (!$tr.length || !tbody) {
return;
}
// Extract IDs before removing from DOM
var rowId = parseInt($btn.attr('data-id') || $btn.data('id') || $tr.attr('data-row-id') || $tr.data('row-id') || 0, 10);
var rowSource = String($btn.attr('data-source') || $btn.data('source') || $tr.attr('data-row-source') || $tr.data('row-source') || 'legacy').trim() || 'legacy';
var activeForm = section.closest('form') || getDischargeForm();
// Instantly remove row from DOM
if ($tr.length) {
$tr.remove();
}
var tbody = document.getElementById('discharge_medicine_tbody');
if (tbody && !tbody.querySelector('tr')) {
tbody.innerHTML = '<tr><td colspan="8" class="text-muted text-center py-2">No medicine added</td></tr>';
}
var secEl = document.getElementById('section-medicine');
var activeForm = (secEl && secEl.closest('form')) || getDischargeForm();
if (!activeForm) {
setMedicineStatus('Medicine removed.', 'info');
return;
}
var csrf = getCsrfPair(activeForm);
var payload = {
action: 'remove_drug',
@ -6667,12 +6680,6 @@ $historyFields = [
setMedicineStatus('Removing medicine...', 'muted');
// Instantly remove row from DOM
$tr.remove();
if (!tbody.querySelector('tr')) {
tbody.innerHTML = '<tr><td colspan="8" class="text-muted text-center">No medicine added</td></tr>';
}
if (rowId > 0) {
$.post($(activeForm).attr('action') || window.location.href, payload, function(data) {
updateFormCsrf(activeForm, data || {});