Page 1 of 1

lookup field - default value

Posted: 2023-04-09 06:44
by christian
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

Re: lookup field - default value

Posted: 2023-04-22 07:38
by christian
Hello,

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

Thank you and have a nice day
Christian

Re: lookup field - default value

Posted: 2023-04-22 08:58
by jsetzer
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 4242 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.

Re: lookup field - default value

Posted: 2023-04-22 10:19
by christian
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

Re: lookup field - default value

Posted: 2023-04-25 16:03
by christian
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 :-)

Re: lookup field - default value

Posted: 2023-04-26 15:28
by jsetzer
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.