You are viewing limited content. For full access, please sign in.

Question

Question

Change minimum required in a collection based on checkbox

asked on October 22, 2014

I am looking for a way to change the minimum required inputs for a collection based on a check box input.

example:

On an application a user selects a Supervisor position that requires a minimum of 5 references and I want to allow them to put more if needed but if they select a non supervisor position I only require a minimum of 3 references.

I want to avoid creating two different collections for references if possable

0 0

Answer

SELECTED ANSWER
replied on October 23, 2014

Sure, it's possible using Javascript. I set up a radio button group with text choices and assigned values to each corresponding to the minimum number of references. Then I gave my radio button a CSS class name of rb_NumRefs and my collection a class name of ReferenceCollection. I set up a function to run whenever the Radio Button group changes, and it modifies the minimum number of required items in the collection (the 'prop' attribute). Then I check the current number of items and trigger presses of the Add/Delete buttons to adjust accordingly. Deletions occur based on the end of the collection. If you don't want that to occur, you can remove that portion of the code.

$('.rb_NumRefs').change( function() {
   var newNumRefs = $('.rb_NumRefs :checked').val();
   var currentNumRefs = $('.ReferenceCollection .kx-repeatable > div').length;
   $('.ReferenceCollection .kx-repeatable').attr('prop',newNumRefs);
   if (newNumRefs > currentNumRefs){
      for(i=currentNumRefs;i<newNumRefs;i++){
         $('.ReferenceCollection .cf-collection-append').trigger('click');
      }
   } else if (newNumRefs < currentNumRefs) {
      for(i=currentNumRefs;i>newNumRefs;i--){
         $('.ReferenceCollection .cf-collection-delete:last').trigger('click');
      }
   }
});

 

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.