• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Need a way to repeat a group of fields without creating additional records

railer

Member
I could please use a bit of advice with setting up part of a form.

As I currently have the form set up, there are four sections, each of which is a group. When submitted (without any repeats), all four sections display as a single record in my list.

The first group of the four is a repeating group with three fields in the group. One of the fields is an integer field where an admin can assign a point value to the group.

I've enabled it so that the user can repeat this group up to 10 times. But I've since learned that each repeated group creates a new record in the database.

How can I get it so that all of the repeated groups in this first section are combined somehow into a single record?

This particular repeated group allows the user to enter in their daily meals. The fields in this group are meal_log, slip_ups, and nutrition_points. The user would enter their first meal of the day. They would click Add to enter their second meal, and then click Add to enter a third meal, and so on. Later, an admin would open the record and assign a point value for each meal. I've been able to get the form to total up all of the point values for the four sections/groups and display them all in a single record, but any time I repeat a group, I get a new record for each repeated group.

There's a bit more to the form, but the above describes the immediate issue.

Is there another way to create four sections and to allow the user to repeat a set of fields?

Thanks in advance for your help.

Best,
Railer
 
A repeat group is recording in it's own DB table and it is necessary that each new group entry is creating it's own record.
Are you adressing the display in list view (it's always good to give some screenshots)?
You can set "Display mode" to "Merge rows" or "merge rows and reduce data" in your list settings (Data/Joins).
 
Hi Troester,

Thanks for your reply. Yes, I am referring to the the list view display, and in general to each repeating group creating extra records. I'm pretty sure that merging the rows is not what I want to do. This form would be submitted on daily basis over a 30 day period, which would be viewed in a list, one day per row. All of the data would need to be seen, and having extra rows would be confusing.

I'm guessing that a javascript could be used to allow the user to add additional groups of fields, if needed, but that's beyond my skill level.

I might end up reverting to providing 4 sets of fields by default and let the user fill them out. The sets would be Meal 1, Meal 2, Meal 3, and Meal 4.

I've attached a screenshot of the current Nutrition set of fields. If you have some code which could be used to create extra sets by pushing an "Add" button without creating extra database rows, and don't mind sharing, I would appreciate it. Many people eat 4 or 5 small meals during the day, so being able to add another meal set as needed would be more intuitive.

Best,
Railer
 

Attachments

  • nutrition_fieldset.png
    nutrition_fieldset.png
    41.2 KB · Views: 569
Did you try to set "merge rows and reduce data"?
From your description I think this is exactly what you are looking for.
 
Well, son of a gun, that did the trick! Thanks!

Do you know how I might be able to get the data for each of the field sets to display in its own paragraph? By default, they all run right after each each other ? not even a letter space to separate.

Another question is regarding how I can change my calc elelement to observe and sum the additional point value fields added, because at present it is not calculating the additional field's points into the total sum?

Here is my calculation code:

$sum='{fab_dailysheet_3_repeat___nutrition_points}'+'{fab_dailysheet___physical_points}'+'{fab_dailysheet___sleep_points}'+'{fab_dailysheet_6_repeat___bonus_points}';
return $sum;

And here is my Ajax Observe Fields code:

{fab_dailysheet_3_repeat___nutrition_points},{fab_dailysheet___physical_points},{fab_dailysheet___sleep_points},{fab_dailysheet_6_repeat___bonus_points}

---

Here is what the id of the first nutrition points field is when I look at the source:
join___1___fab_dailysheet_3_repeat___nutrition_points_0

And here is what the name is for the first field:
join[1][fab_dailysheet_3_repeat___nutrition_points][0]

As I'm sure you know, each additional field increases by 1.

---
Would I change the sum and observe code to some kind of a wildcard array? If so, how and what?

Thanks for your help,
Railer
 
Calc:
You don't need to add elements to the Ajax Observe Fields (those used in the calc are observed anyway).
To get the "repeat" elements try
$sum = $data[join][1][fab_dailysheet_3_repeat___nutrition_points] + $data[join][1][fab_dailysheet___physical_points]...;

Formatting in list view:
the repeated data is formatted with <li> tags
<ul class="fabrikRepeatData">
<li>zwei</li>
<li>eins</li>
</ul>
Use firefox with firebug to examine the CSS you need, then add custom CSS to your list template.
http://www.fabrikar.com/forums/index.php?wiki/form-and-details-templates/#changing-a-fabrik-template (it's the same principle for lists)
http://fabrikar.com/forums/index.php?wiki/creating-a-custom-list-template/
 
Thanks Troester,
What follows is the code I used for the calculation, but it just gives me a blank white screen. Must be missing something?

$sum = $data[join][1][fab_dailysheet_3_repeat___nutrition_points] + $data[join][1][fab_dailysheet___physical_points] + $data[join][1][fab_dailysheet___sleep_points] + $data[join][1][fab_dailysheet___bonus_points];
return $sum;

Thanks,
Railer
 
Sorry, I missed this in my code example (copy/paste;))
Alle repeat elements need
fab_dailysheet_3_repeat
 
There are only two elements that repeat, the others do not, so should they not have the 3_repeat?
 
Would it make sense to create a separate calc element for my first repeating group and somehow total up the points for that repeat group separately, and then have my original calc element sum all of the group points? Or can they all be summed up in one element?

Unless I'm mistaken, don't I need some kind of code to sum up my repeat group point as an array?
 
I just tried the following, but it is not adding them up correctly:

$sum = $data[join][1][fab_dailysheet_3_repeat___nutrition_points] + $data[join][1][fab_dailysheet___physical_points] + $data[join][1][fab_dailysheet___sleep_points] + $data[join][1][fab_dailysheet_6_repeat___bonus_points];
return $sum;
 
Okay, I've been doing some research in the forums and here is what I've found:

Javascript to access the elements in the repeat group array:

var elements = $('group3').getElements('input[name*=fab_dailysheet_3_repeat___nutrition_points]');

Javascript for calculating cost in a repeat group:

var i = this.id.match(/_(\d+)$/)[1];
document.id('join___12___order___total_'+i).value = parseFloat(document.id('join___12___order___qty_'+i).value) * parseFloat(document.id('join___12___order___unit_price_'+i).value);
Replace '12' with your join id (execute your form and look at the code to find this ID)

Replace 'order' with your table name.

--------------

I don't quite know whether this stuff is something I need for my purposes, and also how to implement it, AND also remember I have two repeating groups in this form so how do I sum both of them, plus the two non-repeating groups points?
 
Thanks Rob -- well, I noticed that there is a Javascript tab on the right of the calc element. At any rate, what Troester recommended above wasn't working for me, though it's probably me messing it up.

What would I need to set up given my scenario... Four groups, two which repeat and need to be sub-totalled along with two other groups which do not repeat, but also need to be included in the final total.

I tried creating a calc element for one of the repeating groups as a test thinking I could sum the repeating field(s), but they just repeated themselves.

I would think I would need to somehow sum the repeating fields, and then sum up those sums along with the other two fields. Frankly, I'm stuck.
 
I went back to this, which has the 3_repeat and 6_repeat part which Troester mentioned above. But the calc sum total is zero (0).

$sum = $data[join][1][fab_dailysheet_3_repeat___nutrition_points] + $data[join][1][fab_dailysheet___physical_points] + $data[join][1][fab_dailysheet___sleep_points] + $data[join][1][fab_dailysheet_6_repeat___bonus_points];
return $sum;
 
Hi Rob,

I want to try the following javascript code, which I got from a thread about how to sum an element in a repeating group. Regarding a javascript, you actually said of the suggested javascript that "it sounds pretty good".

Here is the code in my 2.js file:

Code:
function sumNutritionPoints(){
    var qtys = document.getElements('[id^=join___13___fab_dailysheet_3_repeat___nutrition_points_]');
    var sum = 0;
    qtys.each(function (val) {
        sum += parseInt(form_2.formElements.get(val.id).getValue());
    });
    form_2.formElements.get('fab_dailysheet_3_repeat___nutrition_points').update(sum);
}
function sumBonusPoints(){
    var qtys = document.getElements('[id^=join___13___fab_dailysheet_3_repeat___nutrition_points_]');
    var sum = 0;
    qtys.each(function (val) {
        sum += parseInt(form_2.formElements.get(val.id).getValue());
    });
    form_2.formElements.get('fab_dailysheet_3_repeat___nutrition_points').update(sum);

But before I implement it, I have a couple of questions:

1. How do I determine what the number needs to be after "join___X"?
2. Do I have my full element name setup in the correct place(s)?
3. Do I need additional code to sum all of the elements? Where would I put it? In the 2.js file, or in the calc element?
4. Would that code be the calc code in my #6 reply, above?

Thanks,
Railer
 
Friendly bump.

Still working on this. Okay, scrap the javascript above. I found in another post in the forum here, in which Rob discusses a snippet of code to calculate a repeating group element.

Here is the code I have from the other thread modified with variables suitable for my purposes:

Code:
$nutritionpoints = explode(',', "{fab_dailysheet_3_repeat___nutrition_points_raw}");
 
$nutritionpointstotal = array();
for ($i=0; $i < count($nutritionpoints); $i++) {
  $nutritionpointstotal[$i] = $nutritionpoints[$i];
}
$sumnutritionpoints = array_sum($nutritionpointstotal);
return $sumnutritionpoints;

In the other thread, it said to access the $_POST request data, the relevant portion of which is below for the entire repeating group (remember I am interested in summing the nutrition points array values):

Code:
    [join] => Array
        (
            [1] => Array
                (
                    [fab_dailysheet_3_repeat___nutrition_meals_log] => Array
                        (
                            [0] => Meal 1
                            [1] => Meal 2
                            [2] => Meal 3
                        )
 
                    [fab_dailysheet_3_repeat___id] => Array
                        (
                            [0] =>
                        )
 
                    [fab_dailysheet_3_repeat___parent_id] => Array
                        (
                            [0] =>
                            [1] =>
                            [2] =>
                        )
 
                    [fab_dailysheet_3_repeat___nutrition_slipups] => Array
                        (
                            [0] =>
                            [1] =>
                            [2] =>
                        )
 
                    [fab_dailysheet_3_repeat___nutrition_points] => Array
                        (
                            [0] => 5
                            [1] => 5
                            [2] => 5
                        )
 
                    [fab_dailysheet_3_repeat___nutrition_meals_log_raw] => Array
                        (
                            [0] => Meal 1
                            [1] => Meal 2
                            [2] => Meal 3
                        )
 
                    [fab_dailysheet_3_repeat___id_raw] => Array
                        (
                            [0] =>
                        )
 
                    [fab_dailysheet_3_repeat___parent_id_raw] => Array
                        (
                            [0] =>
                            [1] =>
                            [2] =>
                        )
 
                    [fab_dailysheet_3_repeat___nutrition_slipups_raw] => Array
                        (
                            [0] =>
                            [1] =>
                            [2] =>
                        )
 
                    [fab_dailysheet_3_repeat___nutrition_points_raw] => Array
                        (
                            [0] => 5
                            [1] => 5
                            [2] => 5
                        )
 
                )

But I don't understand which data I would put into my code at the top. It doesn't look like what Rob described in the reference thread:

You should see a nested array

['join'][{yourjoinsid}]['z12_package_forward_order_270_repeat___test1']

which itself is an array of the submitted repeat values.

So replace you $test1 ($nutritionpoints in my version) with the data found there
It's not summing the nutrition points for me and returning a grand total in my form.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top