3

add onselect event for view field

In response to Alain Fontaine  & Fred Christmas wish (https://forum.ninox.com/t/h7hbkmp?r=h7hbvzj), I have added a viewEvent function to Ninext project.

Just put viewEvent at true in an init code formula:

var configLoadModules := {
        completion: true,
        badges: true,
        evalJS: true,
        viewEvent: true
    };
html(http("GET", "https://raw.githubusercontent.com/JacquesTur/Ninext/main/loadModules.html").result);

This will be returned:

All that remains is to add the onselect() event function to the view field source formula:

The full code of the view field is :

function onselect(selectedID : text) do
    'Selected customer' := first(select Customer where ID = selectedID)
end;
select Customer

When you select a line in the field of view, the onselect function is called instead of the automatic event that displays a popup.

selectID represents the full (alphanumeric) id of the selected record. The context record this remains the owner record of the view field.

The second part (select Customer) is the standard initialization for the field of view content.

27replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
    • Fred
    • Fred
    • 2 mths ago
    • Reported - view

    I hate to say this but you have to stop doing Ninox’s work for them. Unless they pay you. 
     

    Like 2
    • Fred "Unless they pay you." : Good idea! 😂

      Thank's for your message 🙏

      Like
    • Jacques TUR Fred True, they still have 10 Jobs open 😉Maybe a career switch Jacques?  

      Like
    • Fred
    • Fred
    • 2 mths ago
    • Reported - view

    I can't seem to get it to work. I got the module loaded

    and I put this in the formula of my view:

    function onselect(selectedID : text) do
        Nr := first(select Table3 where Id = selectedID)
    end;
    select Table3
    

    I'm in Table1. Nr is a text field in Table1.

    When I select a record in the view element it opens the record.

    What am I missing?

    Like
      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Fred It works for me in the Mac app. I set it up like you did, assigned the return value to a text field, and I get the alphanumeric value (TableId/RecordId).

      Like
  • I don't understand why it doesn't work for you! I have tried on the online app and on the Mac and everything works. Can you try with the test application attached?

    • Fred
    • Fred
    • 2 mths ago
    • Reported - view

    Jacques, your DB worked fine. Just to verify that I'm using the MacOS app. I've attached my test DB. It is a mess so:

    1) open the System table to load your modules

    2) open Table1, select record 1, then scroll down to the view element

    3) I've changed the view formula to link a Table3 reference field like you did for your sample but the view still opens the record and not link the field.

    Thanks for helping out.

    Sean , yeah probably something my wee brain can't yet grasp.

      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Fred I deleted the Reference fields Table2, Table22 and child from Table1, closed and reopened the database and it worked. It looks like you can't have a 1:N Reference field in the same table.

      Like
      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Correction, you can't have a 1:N Reference field before the View, but you can have one after the View.

      Like
      • Fred
      • Fred
      • 2 mths ago
      • Reported - view

      Wow, you are correct. That is so weird. I wonder why?

      Like
      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Fred Because his code is searching for the first class named "component editor editor-list editor-4col" and your View wasn't first. If you changed the Reference fields to "Show as..." "Field" or "Radio Buttons" it would work, but a View and Reference field with "Show as..." "Table" share the same CSS class name.

      Like 1
    • Thanks Sean. Indeed, to access the part of the code that handles the view selection, I have to pass through the field that displays it on the screen. So I set up a code that waits for a view field to be displayed to finish the initialization and derive the selection function. The only way I found today is to do this search is with the class name "component editor editor-list editor-4col". It turns out that the relationship 1:n and relationship n:1 fields also share the same class name. So as Sean understood, when the first field found was of type relation 1:n or n:1, it was the code of the latter that was initialized and not the view fields.

      I have just corrected the code to check that the initialization is done on a view field. Now version is 1.01 beta.


      Thank you both for your help in stabilising the code.

      Like 2
      • Fred
      • Fred
      • 2 mths ago
      • Reported - view

      As always, Jacques, you are the best! v1.02 works perfectly. Even if there is another view element in front of the one with the onselect it still works.

      Like
    • John Halls
    • John_Halls
    • 2 mths ago
    • Reported - view

    Jacques TUR Just brilliant. Where there is a many to many join I have always wanted to be able to popup the record of the other side, not the join. Solved!

    In a Student  --<  Join >--  Course relationship, this pops up the record of the student from the course

    function onselect(selectedID : text) do
        let a := first(select Join where Id = selectedID);
        popupRecord(a.Student)
    end;
    select Join
    

    Regards John

    Like 2
      • Fred
      • Fred
      • 2 mths ago
      • Reported - view

      That is what I've been wanting to do myself. Now with v1.02 I can do it in my dashboard with many views. Works great!

      Love how people work off other people's ideas.

      Like
      • John Halls
      • John_Halls
      • 2 mths ago
      • Reported - view

      Jacques TUR I have noticed that if I chose a different course each time the student record pops up as expected, but if I chose the same course again that student doesn't pop up. It needs a change of record to fire. Regards John

      Like
    • John Halls yes, it is normal.  the event fire when selected.  if you want, I can add an onclick event that will fire on every click, whether the line is already selected or not.

      Like
      • John Halls
      • John_Halls
      • 2 mths ago
      • Reported - view

      Jacques TUR That's very kind of you Jacques. I think it would be worthwhile

      Regards John

      Like
  • I have just added the ability to update the view field when the record or some fields changes.

    Just add an onload() function in "Display field only, if:" :

    function onload() do
        'selected Customer'.ID
    end;
    true

    The function must return the alphanumeric ID of the record to be selected hot from the view field.
    note: the constant 'true' after the function ensures that the field will be displayed.

    In this example, when the value of the 'selected customer' field changes, the selection of the view field is updated automatically.

    • Jacques TUR At John Halls  request, I have added an onclick event that is called even if the row is already selected (exViewEvent version 1.03 beta). 

      function onclick(event : any) do
          'Selected customer' := first(select Customer where ID = event.targetID);
      
          if event.targetColumnNum = 0 then
              'Selected customer'.(checked := not checked)
          end;
      
          alert(---
       previous ID : { event.previousID }, target ID : { event.targetID }, line : { event.targetLineNum } col n° : { event.targetColumnNum }, col value : { event.targetColumnValue }
      ---);
      end;
      
      select Customer;

      The procedure receives an event parameter of type JSON which contains several values:

      - previousID: previous ID selected. Null if none,
      - targetID: new selected ID,
      - targetLineNum: number of the line in the list (starting from 0),
      - targetColumnNum: number of the column (starting from 0),
      - targetColumnValue: text value of the column.

      This allows, for example, to select or deselect a record directly by clicking on in the column of the tick:

      Important: if the onclick event is called, then onselect will not be called even if it is declared.

      Like
      • John Halls
      • John_Halls
      • 2 mths ago
      • Reported - view

      Jacques TUR That is awesome. Thank you!

      Like
      • Sam
      • Sam.1
      • 1 mth ago
      • Reported - view

      Jacques TUR Very Nice, Thanks!!
       

      Like
      • MODA
      • MODA
      • 5 days ago
      • Reported - view

      Jacques TUR 

      Yep that's exactly what I needed 🙏

      Like
    • Carmelo Padula
    • SmartConfesercenti
    • Carmelo_Padula
    • 2 mths ago
    • Reported - view

    Sei un grande Jacques TUR

    Like
  • Hello everyone, 

    I made an update with two additions: V1.05 : 12/05/2022

    1. if onclick return true, the default event is fire. In this case, if line is newly selected, the default popup appear
    2. add targetColumnCaption on event parameter of onclick event

    The example below changes the selection of a record each time the user clicks on the column named "selected". If the user clicks on another column then the default event (Ninox's) is executed, which causes the Fields form to pop up. 

    function onclick(event : any) do
        if event.targetColumnCaption = "selected" then
            var field := first(select Fields where ID = event.targetID);
            field.(selected := not selected);
            false
        else
            true
        end
    end;
    var current := this;
    select Fields where Tables.Configurations = current
    
    Like
Like3 Follow
  • 3 Likes
  • 5 days agoLast active
  • 27Replies
  • 192Views
  • 9 Following