Page 1 of 1

Using Current Record ID with Action Button

Posted: 2020-09-06 02:48
by hgarbe
Hi to all,
I stumbled over a potentially easy problem, but I could't figured it out on my own using the search function in the forum or the bizzworxx Javasript Helper Documentation. Shortest way to explain my question:

Let's assume I wan't to add an action button creating a new entry in another table with a predefined value in a lookup where the predefined value is the current records ID.

Code below as described here: https://appgini.bizzworxx.de/products/j ... n-buttons/

Code: Select all

var dv = new AppGiniDetailView();
var actionbuttons = dv.actionbuttons;
var group = actionbuttons.addGroup("Additional Links");
group.addLink("Link 1", "TABLENAME_view.php?addNew_x=1&filterer_LOOKUPFIELDNAME=#ID#");
I did try #ID# as when redicreting after creation of new record, after that the way its described here https://appgini.bizzworxx.de/appgini/ho ... able-view/ by using a function with window.location.href.
Neither did work. Is there anything like AppGini.currentTableName() to get this done or any other simple way? Or do I have to use php?

Re: Using Current Record ID with Action Button

Posted: 2020-09-06 03:44
by jsetzer
tl;dr

Code: Select all

var dv = new AppGiniDetailView();
var id = dv.getSelectedId();
var tn = dv.getTableName();
---

Good morning,
  • get primary key of current record in detail view
    • by using pure jQuery

      Code: Select all

      var id = $j('input[name="SelectedID"]').val();
      
    • using AppGini Helper Javascript Library

      Code: Select all

      var dv = new AppGiniDetailView();
      var id = dv.getSelectedId();
      
      or, if the field is visible in detail view,

      Code: Select all

      var dv = new AppGiniDetailView();
      var id = dv.getField("YOUR_PK_COLUMNNAME").getValue();
      
      Note that this will return undefined if that field does not exists for example if this has been configured as "[X] hide in detail view"
  • get name of current table in detail view
    1. using AppGini

      Code: Select all

      var tn = AppGini.currentTableName();
      
    2. using AppGini Helper Javascript Library

      Code: Select all

      var dv = new AppGiniDetailView();
      var tn = dv.getTableName();
      
    --
    [...] I did try #ID# the way its described here https://appgini.bizzworxx.de/appgini/ho ... able-view/ [...]
    In table view there is not one primary key but each row has an individual primary key. Therefore we need a placeholder which will be replaced by each row's primary key.
    The placeholder in table view is not #ID# but %ID%. This placeholder will not help you in detail view, as there should be exactly one primary key.
    --

    By the way:
    Since version 2020/06/25 there are two small helper functions for building url's:

    Code: Select all

    var url = AppGiniUrl.get("tablename"); 
    // tablename_view.php
    

    Code: Select all

    var url = AppGiniUrl.get("tablename", 123); 
    // tablename_view.php?SelectedID=123
    
    So, since 2020/06/24 you can build your add-new url like this:

    Code: Select all

    const SUBTABLENAME = "childtable_name";
    const LOOKUPFIELDNAME = "foreign_key_column_name";
    
    var id = dv.getSelectedId();
    let url = AppGiniUrl.get(SUBTABLENAME) + "?addNew_x=1&filterer_" + LOOKUPFIELDNAME + "=" + id;
    
    Now that I am reading the code above, I am considering whether I should add further functions in one of the next versions that will make it easier for you to concat such URLs.

Re: Using Current Record ID with Action Button

Posted: 2020-09-06 15:39
by hgarbe
Thanks for that detailed explanation. Decided to use Appgini Helper way as I just need to add the Current Record ID as Parameter.
I'm going to check out the jQuery way later as it might be useful when I want to use other Values as the ID for a Action Button with Function call instead of a simple URL.
Could definitely be handy to have further functions to concat URLs in an easy way. If I come across more thought through usecases I gonna add them to this post. Also maybe you want to consider to mention dv.getSelectedId() in your Documentation for Action Buttons?

Re: Using Current Record ID with Action Button

Posted: 2020-09-06 17:03
by jsetzer
Thanks for the hint.

I've created a blog post here (https://appgini.bizzworxx.de/appgini-he ... n-buttons/) and, as you have suggested, linked it in documentation for Action Buttons (https://appgini.bizzworxx.de/products/j ... n-buttons/)