1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Repeat group check box 'make all data same'

Discussion in 'Community' started by Mono, Nov 10, 2018.

  1. Mono

    Mono Member

    Level: Community
    I'm not sure the title explains this well, but how do you place a check box (or other control) to make the data entered in one field in a repeat group be repeated on all?

    Thank you :)
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    You'd need to write some custom Javascript fired from the element.

    -- hugh
     
  3. Mono

    Mono Member

    Level: Community
    Thanks Hugh
     
  4. Mono

    Mono Member

    Level: Community
    I wrote this code, a check box located in the repeat group, when checked copies all values of the elements in this repeat group (except any that contain 'id' in the name) into all remaining repeat groups.
    It can be used on any repeat group.
    Unchecking clears all data in remaining groups.

    In the javascript onchange event of the checkbox I put this code
    Code (Javascript):
    populateRemainingFields(this,40);
    Where 40 is the number of the repeat group.

    This code is placed in form_X.js

    Code (Javascript):
    function populateRemainingFields(el,groupId) {
    // get the element value ...
    var x = el.getValue();


    // if checkbox is checked
    if (x==1){
      insertValue("no",el,groupId)
      // if the checkbox is unclicked lets remove all the data
    }else{
      // if the check box is checked append new data to remaining  fields
      insertValue("delete",el,groupId);
      }
    }

    function insertValue(remove,el,groupId){
          // get the repeat group on which checkbox was clicked
          var myForm = 'form_1'
          var targetRepeatCount = getRepeatCount(groupId);
          var block = Fabrik.getBlock(myForm);
          // find how many repeat groups are open
          var numRepeats = block.repeatGroupMarkers[groupId];


          jQuery.each(el.form.formElements, function(key, element) {
            //Is this the same group as clicked checkbox
            if (element.groupid == groupId) {
              // do not include id items, add 'id' to element name to exclude it    
              if (key.indexOf('id')== -1) {


                  //from key trim off the number of the repeat group and add #
                  var targetField = ('#' + key.slice(0, -1));

                  //get the value to be copied
                  // if the checkbox is checked onclick use the value in the group, if not clear the field
                  if (remove == "delete"){
                  var targetValue = "" // jQuery(targetField + targetRepeatCount).val();
                  }else{
                  var targetValue = jQuery(targetField + targetRepeatCount).val();
                  }
                  //add 1 to group count
                  var newTargetRepeatCount = targetRepeatCount + 1

                      do {
                       //update the rest of the groups with the value
                       //console.log(targetField+newTargetRepeatCount,"=",targetValue);
                      jQuery(targetField + newTargetRepeatCount).val(targetValue);
                      newTargetRepeatCount = newTargetRepeatCount + 1;
                      }
                      while (newTargetRepeatCount < numRepeats);
               
           
              }
       
            }

          });
      }

      function getRepeatCount(groupId){

        // get the DOM object for this group ...
        var thisGroup = jQuery('#group' + groupId);
        // get a jQuery array of all the subgroups in this group ...
        var allSubGroups = jQuery(thisGroup).find('.fabrikSubGroup');
        // get the sub group the button was clicked on ...
        var targetSubGroup = event.target.getParent('.fabrikSubGroup');
        // find the index of the target subgroup in all groups
        var RepeatCount = jQuery.inArray(targetSubGroup, allSubGroups);
        return RepeatCount;
      }
     
    Thanks to Hugh most of the work is his, I just glued it together.
     
    cheesegrits likes this.

Share This Page