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

Question

Question

Forms collection numbering

asked on May 24, 2016 Show version history

Hi

I have a field in my form and a collections that follows it. if in the filed it says 3 then i would like to have collection repeated 3 times automatically. And if i delete one from collection, number in the field should decrement.

I have been trying to achieve this but haven't gotten anything solid so far. Any help is greatly appreciated. 

Thanks in advance

Junaid Inam

0 0

Answer

SELECTED ANSWER
replied on May 24, 2016 Show version history

Hi, I use a single line field and a collection. If the single line have default value 4 , the collection have 4 repeats when load the form. If the single line has no value, it will change to the repeat count when load the form.

If you want to limit the repeat count to the allow range, please use the following JavaScript

$(document).ready(function () {
    if($('#q1 input').val()=="")
    {
    updateSetCount();
    }
    else
    {
      setCollectionRepeat();
    }
    $('#q2').on('click', '.cf-collection-append', updateSetCount);
    $('#q2 .cf-collection').on('click', '.cf-collection-delete', updateSetCount);
    $('#q1').on('blur', 'input', setCollectionRepeat);
  
    function updateSetCount() {
        $('#q1 input').val($('#q2 li').length);
    }
  
    function setCollectionRepeat() {
        var count=parseNumber($('#q1 input').val());
        var current=$('#q2').find("li").length;
        if(count>current)
        {
          
          for(var i=0;i<count-current;i++)
          {
            if($('#q2 .cf-collection-append').is(":hidden"))
           { 
            updateSetCount();
            return;
           }
            else
           {
             $('#q2 .cf-collection-append').trigger('click');
           }
          }
       
       }
       else {
          for(var i=0;i<current-count;i++)
          {
            if($('#q2 .cf-collection .cf-collection-delete:last').is(":hidden"))
           { 
            updateSetCount();
            return;
           }
            else
           {
            $('#q2 .cf-collection .cf-collection-delete:last').trigger('click');
           }
          }
       }
      
    }
  
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

If you don't want to limit the repeat count to the allow range, please use the following JavaScript

$(document).ready(function () {
    if($('#q1 input').val()=="")
    {
    updateSetCount();
    }
    else
    {
     setCollectionRepeat();
    }
  
    $('#q2').on('click', '.cf-collection-append', updateSetCount);
    $('#q2 .cf-collection').on('click', '.cf-collection-delete', updateSetCount);
    $('#q1').on('blur', 'input', setCollectionRepeat);
  
    function updateSetCount() {
        $('#q1 input').val($('#q2 li').length);
    }
  
    function setCollectionRepeat() {
        var count=parseNumber($('#q1 input').val());
        var current=$('#q2').find("li").length;
        if(count>current)
        {        
          for(var i=0;i<count-current;i++)
          {
             $('#q2 .cf-collection-append').trigger('click');
          }
       
       }
       else 
       {
          for(var i=0;i<current-count;i++)
          {
            $('#q2 .cf-collection .cf-collection-delete:last').trigger('click');
          }
       }
    }
  
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

2 0

Replies

replied on May 24, 2016

Another version without setting initial value (change the id to yours)

$('document').ready(function(){
  $('#Field1').change(function(){
    var row = $('#q2 .kx-repeatable').children('div').length;
    if (row < $('#Field1').val())
    {
      for (var i = row; i < $('#Field1').val(); i++)
      {
        $('#q2 .cf-collection-append').click();
      }
    }    
  });
  
  $('#q2 .cf-collection-append').click(function(){
      $('#q2 .cf-collection-delete').unbind('click.delete').bind('click.delete', function(){
        $('#Field1').val($('#Field1').val()-1);
      });
  });    
})

 

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.