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
    • Ninox developper
    • Jacques_TUR
    • 2 yrs ago
    • Reported - view

    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
    
    • Ninox developper
    • Jacques_TUR
    • 2 yrs ago
    • Reported - view

    After using this function, I noticed a flaw. When a row is already selected and you click on a column different from "selected", nothing happens, the popup does not open because the row was already selected. 

    To change this, just :
    - that the onclick function systematically returns false,
    - add yourself the popupRecord function to open the form when you want.

    function onclick(event : any) do
        var selCustomer := first(select Customer where ID = event.targetID);
        if event.targetColumnCaption = "selected" then
            selCustomer.(checked := not checked)
        else
            popupRecord(selCustomer)
        end;
        false
    end;
    select Customer

    With these modifications, the behavior of the list is identical to a normal list if you click

    Enregistrement de l’écran 2022-06-26 à 22.25.49 

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

      Jacques TUR 

      • Maurice
      • 1 yr ago
      • Reported - view

      Jacques TUR Hello Jaques, I have a view with a column I select a check like showed in the example. My code is

      function onclick(event : any) do
          let a := first(select Kurse_RuR where Nr = event.targetID);
          if event.targetColumnCaption = "Tutorenkurs" then
              a.(Tutorenkurs := not Tutorenkurs);
              true
          else
              false
          end
      end;
      

      When I click on a column entry for example in line 20 of the view, I get the right output (checked or not checked), but the view jumps backs to the top line of the view (1st line). I then have to scroll all the way down to continue to set true or false states in the columns. Is there a solution?

      Thanks for an answer.

      Maurice

      • Fred
      • 1 yr ago
      • Reported - view

      Maurice Ninext is shutting down. Please see this post.

      • Maurice
      • 1 yr ago
      • Reported - view

      Fred Thanks Fred. Oh my god. I only hope that Ninox is clever enough to take Jaques expertise to integrate the Ninext functions in the standard-Ninox.

      Thanks Jaques for all your work, ideas und always quick debugging.

      Maurice

    • UKenGB
    • 2 yrs ago
    • Reported - view

    Just trying to use the 'onclick' event to popup the other parent record rather than just show the child (that was clicked in the list). It does work, but when viewing that parent, the record selection controls (arrows) are all disabled and the arrow keys do not work as they normally do when a record is 'popped up' from a list. So once the parent record has 'popped up', there is no way to select the next/previous child and view its other parent, apart from closing that popup and clicking the next/previous one in the list.

    Is there any way this can be changed so that one can move up and down the list of children and display the other parent as specified in the onclick function?

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

      UKenGB Normally if the onclick function returns false or void, then it is the default Ninox code that runs. And in this case, the selection by with the arrow keys works well.

      Unfortunately, there is an error in my code and the default code never runs. I will correct this and I will notify you as soon as the update is done.

      • UKenGB
      • 2 yrs ago
      • Reported - view

      Jacques TUR Thanks, let me know when you've made the change.

      There is another issue that is puzzling me, but I don't know if it is actually anything to do with your module.

      If viewing the main table list you click a row, that record 'pops up' and other rows/records can be selected simply by clicking the other row in the main list, clicking the record selection arrows or using the arrow keys and the new selected row remains highlighted in the list. All standard default Ninox behaviour.

      However, if that popup contains any list (a linked table or other view) and you click that, the record associated with the clicked row will pop up, but the list row that was clicked becomes deselected, i.e. not highlighted and the arrow keys do not function at all. Clicking a record selection arrow does work as it should, but the underlying list is immediately de-selected.

      If however that second popup contains a list, clicking a row of that will display another popup and record selection all works perfectly again, arrow buttons, arrow keys and direct clicking of the underlying list.

      So one popup works. A second popup does not work correctly, but a third popup again works correctly. Not tried a forth yet.

      This seems consistent whatever table I start from, including those with none of Jacques' functions and I even started the db without loading Jacques' modules and the problem was still there, also after restarting Ninox entirely. So that would suggest it is nothing to do with Jacques' modules, but I only noticed the problem after adding an 'onclick' to one list/view although it may have already existed prior to that. Perhaps since the recent Ninox update?

      What I am sure about is that previously I saw no difference in behaviour between popup levels, they all worked the same way, as expected, but now there is this odd behaviour and it is directly connected with what Jacques' code is manipulating.

      Is anyone else experiencing this problem at the second popup level?

      • UKenGB
      • 2 yrs ago
      • Reported - view

       I just tried another entirely different database and there is the same problem at the second level. However, I described it slightly incorrectly.

      When clicking a list in the first popup, a second popup is opened that displays the appropriate record and the underlying list row remains highlighted. Using the arrow keys moves the highlight in the underlying list to the next/previous row, but the record displayed in the popup does NOT change. It remains as the selected one when the popup opened. However, if the record selection arrows (top right) are used, the record displayed in the popup DOES change, but the row in the underlying list is immediately de-selected.

      This other db has NEVER loaded Jacques module so it seems to not be connected and just a Ninox issue. As far as I can determine it looks as if only some popup levels work correctly. Others fail as described above. So the first popup works, the second does not, but any further popup levels may or may not. I think it likely there is a pattern, but hard to work out what that is.

      Is this a Ninox behaviour others are seeing? Mac or other? Does it need reporting elsewhere?

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

      UKenGB I fix it. Is work fine now. You can put this code to modify column number 0 when user click over and have a normal select record when user click on other column :

      • UKenGB
      • 2 yrs ago
      • Reported - view

      Jacques TUR Thanks.

      I have noticed another issue with using onselected() to run some code whenever the list selection changes. It does run whenever a row is first selected, or changed by arrows keys/record selection buttons, but if a new record is created, the onselected() function doesn't run.

      This is not a big deal and it may not be possible to catch, but thought I'd mention it.

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

      UKenGB Thank you very much for reporting problems or abnormal behaviors, this allows me to improve the code 🙏. I will look at it as soon as possible.

    • rcrema
    • 2 yrs ago
    • Reported - view

    I've been using this extension with no problems for at least a month now, but today it has started to behave differently.  I have a dashboard that shows all the registered students in classes for the teacher that is logged in.  Clicking on a student with the extension allowed the student record to pop up without popping up the registration record.  Today, the registration table pops up first.  When I close it, the student pop up remains.  So it seems to be popping both up at the same time.  Any ideas why this is happening and how to fix it?

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

      rcrema J'ai corrigé un bug il y a 2 jours. Maintenant, si la fonction onclick retour void ou false, l'évènement Ninox par défaut s'execute et le formulaire en lien avec l liste s'ouvre. Pour que le comportement par défaut de Ninox ne s'execute pas, il suffit que onclick retourne true (ou toutes autres valeurs) : https://forum.ninox.com/t/60hbvbz?r=35h7qkw

      • rcrema
      • 2 yrs ago
      • Reported - view

      Jacques TUR  I added "true" to the end of the onselect function and everything is working correctly again.  Fantastic extension!  Thanks so much for creating and sharing it!

    • Maurice
    • 1 yr ago
    • Reported - view

    Jacques TUR Hello Jacques, great stuff you made with your Ninext "Extensions". I really appreciate that. The fieldInspector saved me many hours of seaching.

    Now I tried the viewEvent Tool. I also - like many here - have a student-join-course relationship of 3 tables.

    I have a view which select the student table. I see the students name in one column. Every student has 4 oor 5 courses. So I have for each course a column. I like to click in the column-field of the student to open the right join database entry.

    Is this possible with your extension?

    Maurice

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Maurice thank you for your feedback 🙏.

      On the form of your "RuR Kolloquien", put the code like this (to adjust with your data) :

      function onclick(event : any) do
          "// retrieve the selected record"
          SelectedRecord := first(select YOURTABLE where Id = event.targetID);
      
          if event.targetColumnNum < 4 then
              "// when clicking on the columns 0 to 3 then open the selected record";
              popupReord(SelectRecord);
              "// return true so that the default Ninox event is not executed";
              true
          else if event.targetColumnNum = 4 then
              "// when clicking on the columns 4 then open record of course1 linked to SelectedRecord";
              popupReord(SelectRecord.course1);
              true
              else if event.targetColumnNum = 5 then
                      "// when clicking on the columns 5 then open record of course1 linked to SelectedRecord";
                      popupReord(SelectRecord.course2);
                      true
                  else
                      "// when another column is clicked, void is returned to let the default event run"
                      void;
                  end;
              end;
          end;
      end;
      
      "// Usual value of the formula to define which records should be displayed";
      select YOURTABLE
      
      • Maurice
      • 1 yr ago
      • Reported - view

      Jacques TUR thanks a lot for the significant coding. I could solve my problem and it works perfect like I want. The code differs a little bit because  I have to refer to the join table.  Here you see my code where "Kursart" means type of course (we have different types) and Fach 1, Fach 2 and Fach 3 are three different subject of the same type. The subjects of this type can be found n the table "Fächer".

      What I mentioned: when I took the "let lastKJ := Kursjahrgang.Jahrgang;" before the function command the code did not work.

      I tried to substitute the many if-then cases with a switch case command. That did not work. Is switch-case-do not aloud in the onclick function?

      The onclick function is a really great feature in Dashboards and helps in my case a lot for efficient work. Thanks.

      Maurice

      function onclick(event : any) do
          let lastKJ := Kursjahrgang.Jahrgang;
          let a := first(select RuR where Nr = event.targetID);
          let Fach2 := first(select RuR where Nr = event.targetID).'Fach 2'.Nr;
          let Fach1 := first(select RuR where Nr = event.targetID).'Fach 1'.Nr;
          let Fach3 := first(select RuR where Nr = event.targetID).'Fach 3'.Nr;
          if event.targetColumnNum < 3 or event.targetColumnNum > 7 then
              popupRecord(first(select RuR where Nr = event.targetID));
              true
          else
              if event.targetColumnNum = 3 then
                  popupRecord(first(a.Kurse[Kurs.Kursart = 1 and Kurs.Kursjahrgang.Jahrgang = lastKJ and Kurs.record('Fächer',number(Fach)).Nr = Fach1]));
                  true
              else
                  if event.targetColumnNum = 4 then
                      popupRecord(first(a.Kurse[Kurs.Kursart = 1 and Kurs.Kursjahrgang.Jahrgang = lastKJ and Kurs.record('Fächer',number(Fach)).Nr = Fach2]));
                      true
                  else
                      if event.targetColumnNum = 5 then
                          popupRecord(first(a.Kurse[Kurs.Kursart = 1 and Kurs.Kursjahrgang.Jahrgang = lastKJ and Kurs.record('Fächer',number(Fach)).Nr = Fach3]));
                          true
                      else
                          if event.targetColumnNum = 6 then
                              popupRecord(first(a.Kurse[Kurs.Kursart = 2 and Kurs.Kursjahrgang.Jahrgang = lastKJ]));
                              true
                          else
                              if event.targetColumnNum = 7 then
                                  popupRecord(first(a.Kurse[Kurs.Kursart = 3 and Kurs.Kursjahrgang.Jahrgang = lastKJ]));
                                  true
                              else
                                  void
                              end
                          end
                      end
                  end
              end
          end
      end;
      let thisKJ := Kursjahrgang.Jahrgang;
                  select RuR where (contains(text(Kurse[Kurs.Kursart = 1 or Kurs.Kursart = 2].Kurs.Kursjahrgang.Jahrgang), text(thisKJ)) or contains(text(Kurse[Kurs.Kursart = 1 or Kurs.Kursart = 2].'Prüfungskurs'.Kursjahrgang.Jahrgang), text(thisKJ))) and 'vorzeitig beendet' != true
      
      
      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Maurice switch function seem work fine on my database : 

      function onclick(event : any) do
          'selected Customer' := first(select Customer where Id = event.targetID);
          switch true do
          case event.targetColumnNum = 0:
              (
                  'selected Customer'.(Check := not Check);
                  true
              )
          case event.targetColumnNum < 2:
              (
                  popupRecord('selected Customer');
                  true
              )
          default:
              void
          end
      end;
      select Customer
      • Maurice
      • 1 yr ago
      • Reported - view

      Jacques TUR Thanks, now it works for me too.

    • Ninox developper
    • Jacques_TUR
    • 1 yr ago
    • Reported - view

    Hello,

    This is a message for those who use function onselect with view fields. 
    Because of Ninox version 3.8 I had to remove the onselect function. However, onclick still works and it does more than onselect: https://forum.ninox.com/t/60hbvbz?r=h7hdy3j

    If you have any problems adapting your code, please let me know and I will help you.

    I am using an example sent to me to show how to switch from onselect to onclick

    function onselect(selectedID : text) do
        let q := first(select Registration where Id = selectedID);
        popupRecord(q.Students);
        true
    end;

    With onclick, it becomes:

    function onclick(event : any) do
        let q := first(select Registration where Id = target.ID);
        popupRecord(q.Students);
        true
    end;

     

    if you want to go further and create more complex mechanisms, here are the variables available to you in the event parameter:

    event : {
            previousID,
            targetID,
            targetLineNum,
            targetColumnNum,
            targetColumnValue,
            targetColumnCaption }

    For example, you could open the q.Students popup only when column 2 is clicked :

    function onclick(event : any) do
        if event.targerColumnNum = 2 then
            let q := first(select Registration where Id = target.ID);
            popupRecord(q.Students);
            true;
        else
            "// default behaviour of the view field"
            false;
        end;
    end;
      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

      Jacques TUR  I made a mistake in the post above. it's not "where Id = target.ID" but "where Id = event.targetID

    • Fred
    • 1 yr ago
    • Reported - view

    Hi all -

    Here is my setup. I have a dashboard, "All Horses Stats" that has a view element to a table called DashTemp. In DashTemp is a number field called HorseID.

    Now I want to click on a record in the view element but onclick to a table called Horses but use the data in HorseID in DashTemp to match a similar field in Horses. I can't use recordId as there is no relationship between the tables.

    I've tried:

    function onclick(event : any) do
        var selHorse := first(select Horses where HorseID = event.targetId.HorseID);
        popupRecord(selHorse);
        true
    end;
    

    and

    function onclick(event : any) do
        var selHorse := first(select Horses where HorseID = event.HorseID);
        popupRecord(selHorse);
        true
    end;
    

    Is there a way to access other field's data from the event. part or can do I only have access to the recordId?

    Thanks,

      • Ninox developper
      • Jacques_TUR
      • 1 yr ago
      • Reported - view

       try it :

      First, select the current record in DashTemp, so you can find the horse from horseId

      var horseId := first( select DashTemp where id := event.targetId).HorseId;
      var horse := first( select Horses where id := horseId);
      

Content aside

  • 4 Likes
  • 2 wk agoLast active
  • 61Replies
  • 1631Views
  • 16 Following