View field event

image.png

Ninext adds events to Ninox's views: the click on a row of a view field (onclick) and the selection of a record in a table's view (onselected). In your formulas, these functions are written exactly like this, in lowercase.

These events can be turned off in the Ninext Settings, "UI Enhancements" section: View Events (onClick, onSelect) (on by default).

Click on a row of a view field: onclick

Quick overview of the JSON properties passed to onclick

PropertyDescriptionExample / Usage
previousIDThe ID of the record selected in the view before the click; empty if there was none.“A1”
targetIDThe ID of the record targeted by the click.“A3”
targetLineNumThe row number on which the user clicked (zero-indexed).3
targetColumnNumThe column number that was clicked in the view (zero-indexed).4
targetColumnValueThe text value displayed in the clicked cell.“Lisa”
targetColumnCaptionThe column caption from the field configuration (using either "caption" or "field.caption").“First Name”

General Workflow

  1. Define the onclick Function within the View Field Formula
    The onclick function must be integrated into the view field formula. It receives an object (named "event") containing the properties described above. In this function, this designates the record that carries the view field, not the clicked row: the latter is found through event.targetID.
  2. Executing Custom Code
    When a user clicks or taps on a cell in the view, Ninext’s hook executes the onclick function. The script can then, for example:
    • Update properties of a record (e.g., toggling a Boolean field).
    • Store the clicked record’s ID into a designated field.
    • Trigger conditional actions based on the clicked column.
  3. Returning the View Content
    At the end of the function, the formula must return the content of the view (for example, "select Customer"). This tells Ninox which records to display after the script executes.

Implementation Examples

Complete Example

Ninox
function onclick(event : any) do
    'ID of selected customer' := event.targetID;
    var selCustomer := first(select Customer where Id = event.targetID);
    debug("onclick");
    if event.targetColumnCaption like "checked" then
        selCustomer.(Checked := not Checked);
        true
    else
        if event.targetColumnNum = 0 then
            true
        else
            false
        end
    end
end;
select Customer

Comments:

  • The function starts by storing the clicked record’s ID into the field "ID of selected customer".
  • It then retrieves the corresponding record from the "Customer" table.
  • If the column caption of the clicked cell contains "checked", it toggles the "Checked" field on that record and returns true to indicate that the click has been handled.
  • Otherwise, the function returns true or false based on whether the clicked column number is 0.
  • Finally, the formula returns "select Customer", which displays the view’s content after executing the script.

Minimal Example

Ninox
function onclick(event : any) do
     'ID of selected customer' := event.targetID;
     debug("Clicked on Customer " + event.targetID);
     "No specific action is performed here.";
     false
 end;

select Customer

Comments:

  • This script simply stores the clicked record’s ID and logs a debug message.
  • Returning false lets the default behavior proceed for actions that aren’t specifically handled.
  • As always, the formula ends with "select Customer" to display the view’s content.

Key Points to Remember

  • Placement within the View Field Formula:
    The onclick function must be defined directly in the view field formula, not in a trigger or other part of the application.
  • Returning the View Content:
    Unlike other functions that expect a Boolean, the last line of the formula must return the view’s content (e.g., "select Customer").
  • Value returned by onclick:
    true signals that the click has been handled: Ninox does not open the clicked record. false, or no value, lets Ninox apply its usual behavior and open the record. A click on a grouping row does not call onclick.
  • A function evaluated separately:
    Ninext extracts the onclick function alone from the formula, then evaluates it separately. It therefore sees neither the variables nor the other functions declared elsewhere in the formula: declare inside onclick everything it needs.
  • Utilizing the Event Parameters:
    Use the information provided in the "event" object (previousID, targetID, targetLineNum, targetColumnNum, targetColumnValue, targetColumnCaption) to tailor your actions according to the click context.
  • Testing on Different Devices:
    Ensure that the function works correctly on both desktops and touch devices, as the hook handles both "click" and "touchend" events.

Selecting a record in a table's view: onselected

Ninext calls an onselected() function each time a row is selected in a table's view (the list of the table's records). Declare it, without parameters, in the formula of the table's read permission ("Readable if"). In this function, this designates the selected record. The value it returns is not used, and Ninox opens the record as usual.

Ninox
function onselected() do
    debug("customer->onselect : " + this)
end;
true

Here, each selection writes in the Debug Tracer, with the Ninext panel open, a line that sums up the chosen record.

The formula keeps its role: Ninox uses its last value to decide who can read the table. So end it with your usual read condition; true, as here, changes nothing to the read permission of a table open to everyone. Like onclick, the function is extracted alone from the formula.