View field event

image.png

Quick overview of the JSON properties passed to onclick

PropertyDescriptionExample / Usage
previousIDThe ID of the previously selected record. Default is "A1".“A1”
targetIDThe ID of the record targeted by the click. Default is "A3".“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.
  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

function onclick(event : any) do
    "ID of selected customer := event.targetID";
    var t := this;
    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

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").
  • 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.