4

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.

61 replies

null
    • Fred
    • 2 yrs ago
    • Reported - view

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

      • Ninox developper
      • Jacques_TUR
      • 2 yrs ago
      • Reported - view

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

      Thank's for your message 🙏

      • Ninox partner
      • RoSoft_Steven.1
      • 2 yrs ago
      • Reported - view

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

    • Fred
    • 2 yrs 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?

      • Sean
      • 2 yrs 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).

    • Ninox developper
    • Jacques_TUR
    • 2 yrs ago
    • Reported - view

    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
    • 2 yrs 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
      • 2 yrs 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.

      • Sean
      • 2 yrs ago
      • Reported - view

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

      • Fred
      • 2 yrs ago
      • Reported - view

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

      • Sean
      • 2 yrs 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.

      • Ninox developper
      • Jacques_TUR
      • 2 yrs ago
      • Reported - view

      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.

      • Fred
      • 2 yrs 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.

    • John_Halls
    • 2 yrs 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

      • Fred
      • 2 yrs 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.

      • John_Halls
      • 2 yrs 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

      • Ninox developper
      • Jacques_TUR
      • 2 yrs ago
      • Reported - view

      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.

      • John_Halls
      • 2 yrs ago
      • Reported - view

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

      Regards John

      • Maurice
      • 2 yrs ago
      • Reported - view

      John Halls I just changed the setting in:

      function onclick(event : any) do    let a := first(select student where Nr = event.targetID);    popupRecord(first(a.join))end;

      I get the first join. But over the entry of the first join lies the entry of the student. I first have do close the student-entry to see the join. How can I avoid this.

      Maurice

    • Ninox developper
    • Jacques_TUR
    • 2 yrs ago
    • Reported - view

    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.

      • Ninox developper
      • Jacques_TUR
      • 2 yrs ago
      • Reported - view

      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.

      • John_Halls
      • 2 yrs ago
      • Reported - view

      Jacques TUR That is awesome. Thank you!

      • Sam.1
      • 2 yrs ago
      • Reported - view

      Jacques TUR Very Nice, Thanks!!
       

      • MODA
      • 2 yrs ago
      • Reported - view

      Jacques TUR 

      Yep that's exactly what I needed 🙏

    • Manager
    • Carmelo_Padula
    • 2 yrs ago
    • Reported - view

    Sei un grande Jacques TUR

Content aside

  • 4 Likes
  • 2 mths agoLast active
  • 61Replies
  • 1662Views
  • 16 Following