').text(row.med_name || '').html() + '" '
+ 'data-id="' + parseInt(row.id || 0, 10) + '" '
+ 'data-type="' + $('
').text(row.med_type || '').html() + '" '
@@ -5914,12 +5952,9 @@
// Detect datalist selection: if current value exactly matches a row, fill type etc.
// (Chrome fires 'input' on datalist pick, not 'change')
- var currentVal = ($('#med_name').val() || '').trim().toUpperCase();
- for (var i = 0; i < medicineSuggestRows.length; i++) {
- if ((medicineSuggestRows[i].med_name || '').trim().toUpperCase() === currentVal) {
- applyMedicineSelection(medicineSuggestRows[i]);
- break;
- }
+ var matched = findMedicineSuggestionRow(medicineSuggestRows, $('#med_name').val() || '');
+ if (matched) {
+ applyMedicineSelection(matched);
}
});
}, 180);
@@ -5927,13 +5962,7 @@
$('#med_name').on('change', function() {
var val = ($(this).val() || '').trim();
- var matchedRow = null;
- for (var i = 0; i < medicineSuggestRows.length; i++) {
- if ((medicineSuggestRows[i].med_name || '').trim().toUpperCase() === val.toUpperCase()) {
- matchedRow = medicineSuggestRows[i];
- break;
- }
- }
+ var matchedRow = findMedicineSuggestionRow(medicineSuggestRows, val);
if (matchedRow) {
applyMedicineSelection(matchedRow);
return;
@@ -5977,7 +6006,17 @@
$('#btn_add_medicine').on('click', function() {
var medId = parseInt($('#med_item_id').val() || '0', 10);
- var medName = ($('#med_name').val() || '').trim();
+ var medInputValue = ($('#med_name').val() || '').trim();
+ var matchedRow = findMedicineSuggestionRow(medicineSuggestRows, medInputValue);
+ var medName = matchedRow ? (matchedRow.med_name || '').toString().trim() : medInputValue;
+
+ if (matchedRow) {
+ $('#med_name').data('med-id', parseInt(matchedRow.id || 0, 10));
+ if (!($('#med_type').val() || '').trim()) {
+ $('#med_type').val((matchedRow.med_type || '').toString());
+ }
+ }
+
if (!medName) {
return;
}