lookup field - default value

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
christian
Posts: 15
Joined: 2022-07-31 08:33

lookup field - default value

Post by christian » 2023-04-09 06:44

Hello,

I have the following problem:

I have a table that should take over a value from another table via the lookup function. This works very well with the standard means.

Now I want to set the default value of the lookup field to $memberInfo["custon"][3]; when entering a record ...

I do this in the .php of the table file under:

* Called when a user requests to view the detail view (before displaying the detail view).

function verein_dv($selectedID, $memberInfo, &$html, &$args) {

if (!$selectedID) {
$fieldname = "user";
$default = $memberInfo["custom"][3];
$html .= '<script>$j("#' . $fieldname . '").val("' . $default . '");</script>';
}
}


Unfortunately, this doesn't work if the field is a lookup field. If I use a text field instead of a lookup field, assigning the default value works fine.


Thanks for the help !
Christian

christian
Posts: 15
Joined: 2022-07-31 08:33

Re: lookup field - default value

Post by christian » 2023-04-22 07:38

Hello,

can anyone help me with a solution or a solution approach ?

Thank you and have a nice day
Christian

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: lookup field - default value

Post by jsetzer » 2023-04-22 08:58

Since no one was able to help you with a standard solution for many months now, I would like to show how this can be done using latest version of AppGini Helper Javascript Library (commercial, I am the author).

If you only know the id (primary key):

Code: Select all

// file: hooks/TABLENAME-dv.js
AppGiniHelper.DV.waitForAllFields(function() {
    const fieldname = "project_id";
    const field = AppGiniHelper.DV.getField(fieldname);

    // we only know the id:
    var id = 3;

    // request id and text from the server, then change the field's value
    field.lookupText(id, function(value) {
        if (value.id) field.setValue(new AppGiniLookupValue(value.id, value.text));
    });
});
If you know both, id (primary key) and text

Code: Select all

// file: hooks/TABLENAME-dv.js
AppGiniHelper.DV.waitForAllFields(function() {
    const fieldname = "project_id";
    const field = AppGiniHelper.DV.getField(fieldname);

    // we know both, id and text
    var id = 3;
    var text = "ERP"
    
    // just change the field's value
    field.setValue(new AppGiniLookupValue(id, text));
});
Result

chrome_yzF7EQqJ69.gif
chrome_yzF7EQqJ69.gif (20.77 KiB) Viewed 1481 times

PS: I hope nobody feels offended by the fact that I show a solution which requires an additional product. At least this solution should help those AppGineers who are already using my library.

PPS: The field.setValue() function works for normal input fields and also for lookup fields. For lookups we have to pass an object having id and text property. For inputs you can just pass the simple value. It is the same for field.getValue() function, which returns a simple value or an object (having id and text), depending on the field type.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

christian
Posts: 15
Joined: 2022-07-31 08:33

Re: lookup field - default value

Post by christian » 2023-04-22 10:19

Thank you very much !
I have the AppGini Helper Javascript Library in use and will test that right away.

Thank you and have a nice weekend

christian
Posts: 15
Joined: 2022-07-31 08:33

Re: lookup field - default value

Post by christian » 2023-04-25 16:03

Thank you very much - works perfectly.

A supplementary question.

I use an extended "user settings table" along these lines:

viewtopic.php?f=4&t=4262&p=17059&hilit= ... ser#p16929

From this table I want to read the values for the "var id" and "var text".
Where and how do I do this best ?

Thanks for the help.
When I have solved this problem, my project will be finished :-)

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: lookup field - default value

Post by jsetzer » 2023-04-26 15:28

Easiest would probably be fetching required data in _dv hook using PHP and then adding a Javascript-<script> to $html variable which declares those two Javascript variables.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply