Get Focused Row In Grid

frappe.ui.form.on(cur_frm.doctype, {
  'onload_post_render': function(frm, cdt, cdn) {
    frm.fields_dict.items.grid.wrapper.on('click', 'input[data-fieldname="item_code"][data-doctype="Sales Invoice Item"]', function(e) {

      console.log(e);
      var d = locals[cdt][cdn];
      msgprint(d.item_code); //it showed me all item codes in the grid
    });
  }

});

How to get active / focused row when it’s clicked?

We had similar requirement.
We did normal jquery select.

var current_doc = $('.data-row.editable-row').parent().attr("data-name");
var doc = locals["Sales Invoice Item"][current_doc];

Try doing this just on click ?
Hope it helps

2 Likes

@fahimalizain It works thanks a lot…

The case is … I want to get price value when user click the price on the popup then update rate field in the grid for that item…do you have some hints how to do that? It will be so helpful for others too…

frappe.ui.form.on(cur_frm.doctype, {
  'onload_post_render': function(frm, cdt, cdn) {

    frm.fields_dict.items.grid.wrapper.on('keydown', 'input[data-fieldname="item_code"][data-doctype="Sales Invoice Item"]', function(e) {
      var current_doc = $('.data-row.editable-row').parent().attr("data-name");
      var d = locals["Sales Invoice Item"][current_doc];

      frappe.call({
        method: "frappe.client.get_list",
        args: {
          doctype: "Sales Invoice Item",
          fields: ["*"],
          filters: [
            ["item_code", "=", d.item_code],
            ["customer", "=", frm.doc.customer],
            ["docstatus", "=", 1]
          ],
          limit_page_length: 5
        },
        callback: function(r) {
          //console.log(r.message);
          if (r.message) {
            if (r.message.length > 0) {
              var html_str = '<table class="table table-bordered" width="100%"><thead><tr>' +
                '<th bgcolor="#F7FFFE">Date</th><th bgcolor="#F7FFFE">Price</th><th bgcolor="#F7FFFE">UOM</th></tr></thead>' +
                '<tbody>'

              for (i = 0; i < r.message.length; i++) {
                if (r.message[i].docstatus == "1") {
                  html_str += '<tr><td>' + r.message[i].posting_date + '</td><td><a href="">' + (r.message[i].rate / r.message[i].conversion_factor).toLocaleString() + '</a></td><td>' + r.message[i].stock_uom + '</td></tr>'
                }
              }

              html_str += '</tbody></table>'
              html_str += '</tbody></html>'
                //console.log(html_str)
             
              msgprint(html_str, 'Price History : ' + d.item_code);
            } else {
              alert('No price history :' + d.item_code);
            }
          }

        }
      });
    });
  }
});

Hii @jof2jc

We had to make on demand backups so we made a popup with behavior. I have used id selector for download all button on the $wrapper. Instead, give a data attribute to select and assign onclick event on all the list items, and access the attribute through this variable- which points to the list item.

Assigning that value to the selected doc row should be pretty much straight.

Reference:

Hope it helps!