cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
leif_frederikse
Nintex Newbie

Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

I have a form with a Choice control. It has a number of choices, and I need some of them to be either hidden or disabled based on some condition. In my case the condition is something like

   fn-IsMemberOfGroup("administratorgroup")

 

How can I achieve this?

 

Regards

Leif

Labels: (2)
Reply
22 Replies
fhunth
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

I'd do it having 2 panels of choices, 1 (visible) for administrators and the other for NonAdministrators

0 Kudos
Reply
leif_frederikse
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

That could be a way to go, but I am implementing tabs (as per Nintex Forms - Tab like Functionality - Vadim Tabakman  ​) where each tab is reprented as an option in a Choice control. My goal is then to have the Administrator tab be either disabled or hidden (whatever is achievable) for those not member of the correct security group. Hence the need to be able to control the visibility/availability of each option.

/Leif

0 Kudos
Reply
Not applicable

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

You could also use Java Script as well to simply remove the option that you don't want to be displayed. For this,

  1. Go to control properties of the drop down control.
  2. In the Advanced section, select Store Client ID in Java Script Variable as yes.
  3. This will open option to name the variable. Name the variable and click save.
  4. Now open form settings and open the Custom Java Script section.
  5. Here you can write your script and access the drop down using the ID that you have given in step 3.

I hope this method will help you. All the best.

0 Kudos
Reply
leif_frederikse
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

Hi Ratnesh,

I did try fooling around with JS, but was unable to get it right. COuld you supply me with the code that would do the trick?

Regards

Leif

0 Kudos
Reply
thomas_xu
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

Hi Leif.

Not sure if you found a solution yourself but the following are the js code required to achieve what you wanted here.

NWF$(document).ready(function()
{

   if(IsCurrentUserMemberOfGroup("administratorgroup"))

  {

    //remove options
      NWF$("#"+myOptions).find("option[value='Option 1']").remove()

  }

});

//check if member belongs to a sharepoint group

function IsCurrentUserMemberOfGroup(groupName) {

    var dfd = NWF$.Deferred(function(){
        var currentContext = new SP.ClientContext.get_current();
        var currentWeb = currentContext.get_web();

        var currentUser = currentContext.get_web().get_currentUser();
        currentContext.load(currentUser);

        var allGroups = currentWeb.get_siteGroups();
        currentContext.load(allGroups);

        var group = allGroups.getByName(groupName);
        currentContext.load(group);

        var groupUsers = group.get_users();
        currentContext.load(groupUsers);

        currentContext.executeQueryAsync(
          function(sender,args){
              var userInGroup = false;
              var groupUserEnumerator = groupUsers.getEnumerator();
              while (groupUserEnumerator.moveNext()) {
                  var groupUser = groupUserEnumerator.get_current();
                  if (groupUser.get_id() == currentUser.get_id()) {
                      userInGroup = true;
                      break;
                  }
              }
              dfd.resolve(userInGroup);
          },
            function(sender,args){
                console.log(args.get_message()+" "+groupName);
                dfd.resolve(false);
                //dfd.reject(args.get_message());
            }
        );
    });
    return dfd.promise();
}

0 Kudos
Reply
leif_frederikse
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

Hi Thomas,

Thank you for your input - it seems to be in the right direction. However I have one problem with getting the code to work: Because of the asynchronous nature of the code, the function IsCurrentUser... is getting a return value before the code that actually contains the logic to determine the result is run. I have tried to simplify what is happening in the code here, and in the image attached.

The simplified code:

NWF$(document).ready(function()

{

  1. console.log("A");

  if(myFunc())

  {

   console.log("B - myFunc returned true");

  } else

  {

   console.log("C - myFunc returned false");

  }

});

function myFunc() {

  console.log("D - starting myFunc");

  var dfd = NWF$.Deferred(function(){

  var currentContext = new SP.ClientContext.get_current();

  currentContext.executeQueryAsync(

        function(sender,args){

          console.log("D - succes");

          dfd.resolve(false);

        },

        function(sender,args){

          console.log("E - failure");

          dfd.resolve(false);

        }

  );

});

  console.log("F - returning promise");

  return dfd.promise();

}

Console output is:

A

D - starting myFunc

F - returning promise

B - myFunc returned true

D - succes

As you can see from the console output, myFunc returns true, even though both functions given to executeQueryAsync returns false.

So to illustrate the order of events graphically:

Untitled.png

As you can see, the promise is returned before the resolve happens.

I tried to remove everything in the original code that tasted like async, but then I got an error saying "the collection has not been initialized".

Please advise, how I can change this so my "business logic" is run before the function returns the result.

Regards

Leif

0 Kudos
Reply
thomas_xu
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

Hi Leif.

I just checked my working solution again. The following should get the function calling order right.

Let me know if any further issues

NWF$(document).ready(function()
{

  checkIfGroupMember("Group Name");

});

function checkIfGroupMember(groupName)
{
NWF$.when(IsCurrentUserMemberOfGroup(groupName).done(function(hasPermission){
   if(hasPermission)
   {
    IsGroupMember = true;
   }
   }),
  ).then(function(){
     if(IsGroupMember)
     {
     //your logic
     }
  
  });
}

View solution in original post

0 Kudos
Reply
leif_frederikse
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

Thank again - did not know about the when/then contruction in jQuery...

I implemented the following, where I move the stuff that I want done into the async functions. Some would probably say that it's not pretty, butit's pretty simple ;-)

The code now looks like this:

NWF$(document).ready(function()

{

          removeTabIfNotAllowed("Administrator","WorkWear Admins");

}

)

function removeTabIfNotAllowed(tabName,groupName) {

    var dfd = NWF$.Deferred(function(){

                   var currentContext = new SP.ClientContext.get_current();

                   var currentWeb = currentContext.get_web();

                   var currentUser = currentContext.get_web().get_currentUser();

                   currentContext.load(currentUser);

                   var allGroups = currentWeb.get_siteGroups();

                   currentContext.load(allGroups);

                   var group = allGroups.getByName(groupName);

                   currentContext.load(group);

                   var groupUsers = group.get_users();

                   currentContext.load(groupUsers);

                   currentContext.executeQueryAsync(

                       function(sender,args){

                              var userInGroup = false;

                              var groupUserEnumerator = groupUsers.getEnumerator();

                              while (groupUserEnumerator.moveNext()) {

                                 var groupUser = groupUserEnumerator.get_current();

                                 if (groupUser.get_id() == currentUser.get_id()) {

                                    userInGroup = true;

                                    break;

                                 }

                              }

                                        

                              if (!userInGroup) {

                                  NWF$("#"+varTabButtons).find("option[value=" + tabName + "]").remove();

                              }

                              dfd.resolve(userInGroup);

                        },

                        function(sender,args){

                              console.log(args.get_message()+" "+groupName);

                              dfd.resolve(false);

                        }

                   );

    });

    return dfd.promise();

}

/Regards

Leif

Reply
thomas_xu
Nintex Newbie

Re: Nintex Forms: How to disable or hide option in Choice control?

Jump to solution

Yeah when and then are very useful for these kinda asynchronous functions and you can have mutiple of them as well.

NWF$.when(

function1().done(function(result1){

  if(result1 meets some condition)

  {  }

  }),

  function2().done(function(result2)

   {  }

  }

  functionN().........

  )

  ).then(function(){

  });

I usually use them with REST service ajax calls as well to solve the same timing problem especially when you have multiple calls.

Glad to hear your own solution worked

0 Kudos
Reply