Search code examples
google-apps-scriptgoogle-app-maker

Google App Maker navigate to page while getting page name from data model


I've created a list of menu items with dynamic names from DataSource, I want to navigate to appropriate pages by clicking the menu items. I have got this code where we can hard code the page name to use in onClick for the current menu item.

app.showPage(app.pages.home);

I want to use dynamic page name from DataSource for the current item in the list.

I've data model named MenuItems with 2 string fields DisplayName and PageName

I want to use PageName value in showPage method instead of hardcoded value--- Thanks for help


Solution

  • In this case you should be able to change your code as follows:

    app.showPage(app.pages[app.datasources.MenuItems.item.PageName]);
    

    This will only work if your MenuItems datasource is loaded to the client however.

    Let me add that if you are running this code out of a list widget and your onclick event of the list row is your navigation, then you can simplify your code by just doing:

    app.showPage(app.pages[widget.datasource.item.PageName]);
    

    This way you will eliminate the possibility of a race condition where App Maker may first recognize the the showPage() function before it will recognize the correctly selected item in your list widget.