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.

43replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • 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
  • 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 

    Like
    • Jacques TUR 

      Like
    • UKenGB
    • UKenGB
    • 2 mths 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?

    Like
    • 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.

      Like
      • UKenGB
      • UKenGB
      • 2 mths 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?

      Like
      • UKenGB
      • UKenGB
      • 2 mths 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?

      Like
    • 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 :

      Like
      • UKenGB
      • UKenGB
      • 2 mths 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.

      Like
    • 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.

      Like
    • rcrema
    • rcrema
    • 2 mths 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?

    Like
    • 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

      Like
      • rcrema
      • rcrema
      • 2 mths 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!

      Like
    • Maurice
    • Maurice
    • 3 wk 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

    Like
    • 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
      
      Like
      • Maurice
      • Maurice
      • 3 wk 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
      
      
      Like
    • 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
      Like
      • Maurice
      • Maurice
      • 3 wk ago
      • Reported - view

      Jacques TUR Thanks, now it works for me too.

      Like
Like4 Follow
  • 4 Likes
  • 3 wk agoLast active
  • 43Replies
  • 461Views
  • 13 Following