I am trying to add a keyboard shortcut to add new items to the quotation.
Seems to work.
frappe.ui.form.on('Quotation', {
refresh: function (frm) {
frm.cscript.custom_on_keydown = function (event) {
if (event.ctrlKey && event.key === 'i') {
event.preventDefault();
frm.fields_dict.items.grid.add_new_row(); //this works!
// Set focus on the 'item_code' field in the newly added row with a slight delay
setTimeout(function () {
const grid = frm.fields_dict.items.grid;
const newRowIndex = grid.grid_rows.length - 1;
var columnName = "item_code";
// not working!
var $fieldElement = frm.fields_dict['items'].grid.get_field(newRowIndex,columnName).$input;
// Check if the field element exists
if ($fieldElement) {
// Set focus on the field element - does NOT work
$fieldElement.focus();
}
}, 100);
}
};
BUT, i would be really nice if the new empty item_code field gets focus. Tried different approaches, did not succedd…
EDIT: i am currently on version 13.
EDIT2: it seems like also just clicking on the Add Row button does not give focus. Should it by default?
Edit3: also this does NOT work:
frappe.ui.form.on('Quotation', {
refresh: function(frm) {
frm.cscript.custom_on_keydown = function(event) {
if (event.ctrlKey && event.key === 'i') {
event.preventDefault();
frm.fields_dict.items.grid.add_new_row();
// Wait for a short delay before setting focus
setTimeout(function() {
const grid = frm.fields_dict.items.grid;
const newRowIndex = grid.grid_rows.length - 1;
const fieldname = 'item_code';
// Check if the grid row is fully initialized
if (grid.grid_rows[newRowIndex]) {
grid.grid_rows[newRowIndex].doc[fieldname] = '';
grid.grid_rows[newRowIndex].refresh_field(fieldname);
// Set focus on the 'item_code' field
setTimeout(function() {
const inputSelector = `[data-fieldname="${fieldname}"][data-rowidx="${newRowIndex}"] input`;
const $input = $(inputSelector);
if ($input.length) {
$input.focus();
}
}, 100);
}
}, 100);
}
};
// Attach the keydown event handler to the document body
$(document.body).on('keydown', frm.cscript.custom_on_keydown);
},
before_unload: function(frm) {
// Remove the keydown event handler when leaving the form
$(document.body).off('keydown', frm.cscript.custom_on_keydown);
}
});