google-app-maker

Very simple if else check in Google App Maker


This is likely embarrassingly easy but I'm new and I've been beating my head against the wall on this for a while now. What I am attempting to do is basically a modified version of the "Hello App Maker!" If else test.

The necessary info I have the following widgets attached to the appropriate data sources:

  • Dropdown widget called source_name (string - list)
  • Label widget I've called name (string)
  • Text Box widget called qty_duration (number)
  • Label widget I've called hours (number)

I have a dropdown widget called source_name with 5 options. On selection I have the value appear in a label widget I've called name. If the option selected from the drop down widget is ever LABOUR I am trying to then have the value of a Text Box widget called qty_duration appear in a label widget I've called hours

On the source_name dropdown event - onValueChange I have the following code:

// Define variables for the input and output widgets
var nameWidget = app.pages.Apex_job_details.descendants.name;
var outputWidget = app.pages.Apex_job_details.descendants.hours;
var techhours = app.pages.Apex_job_details.descendants.qty_duration;
var nothing = 0;


// If a name is LABOUR, add the qty to the output widget Else output 0.
if (nameWidget == 'LABOUR') {
  outputWidget.text = techhours;
} else {
  outputWidget = nothing;
}

It's not giving me any errors, but it's also not outputting to the hours label. If I edit the code as follows just to muck with it:

// Define variables for the input and output widgets
var nameWidget = app.pages.Apex_job_details.descendants.name;
var outputWidget = app.pages.Apex_job_details.descendants.hours;
var techhours = app.pages.Apex_job_details.descendants.qty_duration;
var nothing = 0;

// If a name is LABOUR, add the qty to the output widget Else output 0.
if (nameWidget == 'LABOUR') {
  outputWidget.text = techhours;
} else {
  outputWidget.text = nothing;
}

I'm not sure what I'm doing wrong.


Solution

  • Assuming all labels and input widgets are inside a table row you will want to adjust your code as follows:

    var tablerow = widget.parent;
    var nameWidget = tablerow.descendants.name.text;
    var outputWidget = tablerow.descendants.hours;
    var techhours = tablerow.descendants.qty_duration.value;
    
    if(nameWidget === 'LABOUR') {
      outputWidget.text = techhours;
    } else {
      outputWidget.text = null;
    }
    

    By using widget.parent in the onValueChange event of the dropdown you will automatically reference the table row and then by using descendants you are referencing only the descendants of that table row. This will bridge the error by using an absolute reference when using table rows. If it still doesn't work let me know.