Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Using Javascript to show a tab based on context

(0) ShareShare
ReportReport
Posted on by 15

Hi 

I'm a total novice when it comes to Javascript so I was just wondering if anyone could help with this code? 

I want to hide a tab if the user is viewing a form on desktop and show it if they are using mobile. 

Here's what I have so far: 

// If mobile, show mobile tab, otherwise don't.

var showMobileTab;
function OnLoad (executionContext) {
var form = executionContext.getFormContext();
var context = executionContext.getContext();
var isCrmForMobile = (Xrm.Page.context.client.getClient() == "Mobile")
if (isCrmForMobile)
}
// RateCard.showMobileTab = showMobileTab
function showMobileTab(executionContext) {
form.ui.tabs.get("mobile")
.setVisible(true);
}
else
{
// RateCard.showMobileTab = showMobileTab
function showMobileTab(executionContext) {
form.ui.tabs.get("mobile")
.setVisible(false);
}

pastedimage1569470661830v2.png

Thanks in advance. 

  • cloflyMao Profile Picture
    25,208 on at
    RE: Using Javascript to show a tab based on context

    Hi rtrickey,

    It's glad to see that your issue has been solved perfectly,

    and thanks for sharing solution.

    Have a good day. :)

    Regards,

    Clofly

  • rtrickey Profile Picture
    15 on at
    RE: Using Javascript to show a tab based on context

    Hi Clo

    I've changed the code to display sections rather than tabs and it works perfectly.

    Here's the final code:

    // If mobile, show mobile section, otherwise show desktop section.

        function onLoad(executionContext)  {    

             var onLoad;

             var form = executionContext.getFormContext();

             var context = executionContext.getContext();

             var isCrmForMobile = (Xrm.Page.context.client.getClient() == "Mobile")

             if (isCrmForMobile) {

            form.ui.tabs.get("summary").sections.get("mobile").setVisible(true);

             } else {

            form.ui.tabs.get("summary").sections.get("desktop").setVisible(true);

    }

    }

    Thanks for all your help.

  • cloflyMao Profile Picture
    25,208 on at
    RE: Using Javascript to show a tab based on context

    Hi retro key,

    I didn’t encounter this issue in iOS application.

    You could remove setVisible(true) function.(not do any action if the client is mobile)

    Regards,

    colfly 

  • rtrickey Profile Picture
    15 on at
    RE: Using Javascript to show a tab based on context

    Hi Clo

    What I meant is the re-ordering isn't an issue as I only have one tab. 

    The issue is that the data does not automatically appear - you have to physically click the tab for the fields to show. Have you experienced this before?

    Thank you,  

  • cloflyMao Profile Picture
    25,208 on at
    RE: Using Javascript to show a tab based on context

    Hi rtrickey,

    What does "it had no effect" mean ? It seems that Mobile has been added as first tab from your screenshot,

    does Javascript code not work for display tab after re-ordering ?

    Supplement: 

    Hi rtrickey, do you mean you should click screen then data in Mobile tab will display ?

    You could try remove setVisible(true) .

    Rrgards,

    Clofly

  • rtrickey Profile Picture
    15 on at
    RE: Using Javascript to show a tab based on context

    Hi Clo, not quite.

    I tried re-ordering as per above but it had no effect.

    I've attached some screenshots of what I see for your reference.

    pastedimage1569812688357v1.jpeg Post-click

    Regards,

    Ryan

  • cloflyMao Profile Picture
    25,208 on at
    RE: Using Javascript to show a tab based on context

    Hi rtrickey,

    Firstly I would like to point out that our user agent will only be recognized as 'Mobile' in Mobile Application.(AppStore or Google Play),

    even if we works in mobile browser, CRM will still regard user agent as "Web".

    From your description and as per my understand, do you mean you have to click the horizonal ellipsis to get the tab you want ?

    For example, details tab is only available in Mobile application and it will be shown after clicking ellipsis icon:

    Web:

    58943141.jpg

    Mobile:

    1326126056.jpg

    If so, we should know that setVisible function is to control whether the specific tab be shown on form,

    and your purpose might be you would like to show it as first tab(as Summary tab in screenshot).

    Those tabs display order are decided by their top and bottom order in form editor,

    so you should open form editor, and drag the tab you want to top, then it will show directly without have to open ellipsis drop-down menu:

    (I dragged Details tab to top to replace Summary tab)

    6102.JPG

    Now Details has been the first tab in mobile.

    IMG_5F00_2198.PNG

    While Summary still be the first because Details tab was hidden.

    IMG_5F00_2200.PNG

    Regards,

    Clofly

  • rtrickey Profile Picture
    15 on at
    RE: Using Javascript to show a tab based on context

    Hi Clofly, thank you so much for your response. I've added the code and it works except the tab does not automatically appear on mobile, you have to click on it to show the figures. Anything I can do to make this display without clicking?

    Thanks again.  

  • Verified answer
    cloflyMao Profile Picture
    25,208 on at
    RE: Using Javascript to show a tab based on context

    Hi Partner,

    You should fire the outter onLoad function in form settings instead of showMobileTab.

    (Or you could change its name to showMobileTab as you would like)

    A modified code could be below:

    function OnLoad(executionContext) {

    var showMobileTab;
    var form = executionContext.getFormContext();
    var context = executionContext.getContext();
    var isCrmForMobile = (Xrm.Page.context.client.getClient() == "Mobile")
    if (isCrmForMobile) {
    form.ui.tabs.get("mobile").setVisible(true);
    } else {
    form.ui.tabs.get("mobile").setVisible(false);
    }

    }

    While you should also check Pass executioncontext .. option for your function.

    6661.JPG

    And for get("mobile"), the parameter should be match with Tab Name property:

    1106.JPG 

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Dynamics 365 general forum

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans