Page 1 of 1

Select Object on Subform to Update fields

Posted: Mon Oct 05, 2020 11:04 am
by ernesttan1976
Hi there, I need some help...

I need to base on the onchange event of the "Select" object, to trigger an SQL and pull out the fields "roles" and "name" and "group" from "employee" table.
And write the values in these subform fields.

I have been trying for sometime now, got no clue.

I tried using the Lookup object, however it is very slow, the select object is very fast.

Many thanks!

Ernest

Re: Select Object on Subform to Update fields

Posted: Mon Oct 05, 2020 4:44 pm
by kev1n
Hi,


1. Put this code on the onchange event of the employee select field

Code: Select all

getEmployeeInfo(event);
employee_onchange.png
2. In your form's Custom Code, add this code:

Code: Select all

function fillEmployeeInfo(prefix, roles, name, group) {
  
    $('#' + prefix + 'roles').val(roles).change();  // replace 'roles' with your roles field object Id
    $('#' + prefix + 'name').val(name).change();  // replace 'name' with your name field object Id
    $('#' + prefix + 'group').val(group).change();  // replace 'group' with your group field object Id

}

function getEmployeeInfo(event) {

    var empId = $('#' + event.target.id).val();
    nuSetProperty('empId', empId);

    var prefix = $(event.target).attr('data-nu-prefix');
    nuSetProperty('prefix', prefix);

    nuRunPHPHidden('getEmployeeInfo', 0)
}
3. Create a PHP Procedure with the name getEmployeeInfo

Code: Select all

function getEmployeeInfo($employee_id) {
   
   // Change the SQL query here:
    $sql = "SELECT emp_roles, emp_name, emp_group FROM `employee` WHERE `employee_id` = ?";

    $qry = nuRunQuery($sql, [$employee_id]);
    $row = db_fetch_object($qry);

    return array(
        "roles" => $row->emp_roles,  // replace emp_roles with your sql column
        "name" => $row->emp_name, // replace emp_name with your sql column
        "group" => $row->emp_group, // replace emp_group with your sql column

    );
}


$empInfo = getEmployeeInfo("#empId#");

$roles = $empInfo["roles"];
$name = $empInfo["name"];
$group = $empInfo["group"];

$j = "fillEmployeeInfo('#prefix#', '$roles','$name','$group'); ";

nuJavascriptCallback($j);
php_procedure.png

Re: Select Object on Subform to Update fields

Posted: Tue Oct 06, 2020 4:37 am
by ernesttan1976
WOw, Kevin, Thank you so much. I will try it out and report back with good news :)

Re: Select Object on Subform to Update fields

Posted: Tue Oct 06, 2020 5:13 am
by ernesttan1976
Woohoo! it works! :) Thank you Kevin :D