Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

[Added] JS function nuBrowseRow() (nuBuilder 4.7.x)

Information about updates, news, Code Library
admin
Site Admin
Posts: 2813
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

[Added] JS function nuBrowseRow() (nuBuilder 4.7.x)

Unread post by admin »

We’ve just added a small but useful helper function, nuBrowseRow(), to make it trivial to grab information about the clicked-on cell (or any other cell in the same row) when you use nuSelectBrowse().

Key features of nuBrowseRow()
  • Automatic unpacking of primary key, row index, column index, cell HTML, cell text value, and the jQuery cell object
  • Optional parameter to pull data for a different column in the same row
  • Zero boilerplate—one call instead of half a dozen attribute lookups
Usage example 1: Get the row number of the selected cell

Code: Select all

function nuSelectBrowse(event, element) {
    const cell = nuBrowseRow(element);
    console.log(cell.row);
}
Usage example 2: Get info about the selected cell

Code: Select all

function nuSelectBrowse(event, element) {
    const { pk, row, column, html, value, $cell } = nuBrowseRow(element);
    console.log(pk, row, column, html, value, $cell);
}
Usage example 3: Get the value of column 2 in the same row

Code: Select all

function nuSelectBrowse(event, element) {
    // Pass zero-based column index (1 = second column)
    const otherCell = nuBrowseRow(element, 1);
    console.log(otherCell.value);
}
Usage example 4: Get the value of a specific column in the same row

Code: Select all

function nuSelectBrowse(event, element) {
    // Pass the column primary key which is more stable than a numeric index
    const otherCell = nuBrowseRow(element, 'nu6afd6cb37627eb1'); // data-nu-column-id="nu6afd6cb37627eb1"
    console.log(otherCell.value);
}

Before v4.7.x, you’d have to do something like this:

Code: Select all

function nuSelectBrowse(event, element) {
    const $cell  = $(element);
    const pk     = $cell.attr('data-nu-primary-key');
    const row    = $cell.attr('data-nu-row');
    const column = $cell.attr('data-nu-column');
    const value  = $cell.text();
    const html   = $cell.html();

    console.log(pk, row, column, html, value, $cell);
}
…or, if you wanted another cell in the same row:

Code: Select all

function nuSelectBrowse(event, element) {
    const $cell = $(element);
    const row   = $cell.attr('data-nu-row');

    // Build the other cell’s ID: "nucell_<row>_<column>"
    const otherCellId    = 'nucell_' + row + '_1';  // second column
    const $otherCell     = $('#' + otherCellId);
    const otherCellValue = $otherCell.text();

    console.log(otherCellValue);
}
Post Reply