Ordering repeated groups

fanta00

Member
How can I order repeated groups added within one group so that the latest one is always added to the top, not to the bottom, of the group?
 
Thanks for reply, but it is not what I was asking about, I know where to order different groups but I was asking how can I change the order of the repeated groups within the same group - so when the group is repeated I would like the newest one to show on top, not at the bottom of the whole group.
 
Yeah, you could probably do it with a custom template, but it's probably something we should provide a built in option for.

I'm not sure if this would be something we'd allow ordering by any field for, but (initially) a "Order: Ascending / Descending" option shouldn't be too hard. Although i say that without having examined the code carefully for any gotchas. But I suspect we'd do this in PHP, rather than messing with the ORDER BY logic in the main data query that grabs form data.

One question ... if you have "Descending" ordering (where the existing behavior is "Ascending", would you expect newly added groups to go at the top as well?

-- hugh
 
I think all you would need to do in a custom template would be to find this line in default.php (line 95 in the current default template):

Code:
			foreach ($group->subgroups as $subgroup) {

and change it to:

Code:
			foreach (array_reverse($group->subgroups) as $subgroup) {

I haven't tested to see if adding and removing subgroups would cause any issues, and adding a group will still add it at the bottom.

But it's a start. I'll add a github feature request, and take a look at the top / bottom issue for the adding a new subgroup, which would have to be done in the main form JavaScript rather than on a template, as the DOM structures are created and inserted in the JS.

-- hugh
 
Just remember to clone a copy and use that, rather than changing any of the out-of-box templates (which will get overwritten next time you update).

-- hugh
 
Thank Hugh. Meanwhile I tried write some php code in dafault.php (template) but it doesn work:

$order = array();
foreach ($group->subgroups as $subgroup) {
$order[] = $subgroup['order_field_name']; <== 'order_field_name' is the colounm that should be ordered ??
}

foreach (array_multisort($order, SORT_ASC, $group->subgroups) as $subgroup) {
.............................
$order[] = $subgroup['order_field_name'] I think is the code that need to be correct, but I dont know how ?
Can someone help me ?
Richy
 
Try using uasort().

Code:
uasort($group->subgroups, function($a, $b) {
    return strcasecmp($a['order_field_name'], $b['order_field_name']);
});

Do that just before the main foreach() (the original teplate code, not your modified version).

I'm assuming your key is an alphanumeric field, and you want to sort it alphabetically, ignoring case, so I'm using strcasemp(). If you need it sorted any other way, just use the appropriate function or operator to compare the two values.

I *think* this will be harmless. It should re-sort the subgroups array on your key, and I don't think that will have any nasty side effects. Let me know.

-- hugh
 
Nice. Thx hugh. Cause is a numeric order the my complete working code is:

Code:
uasort($group->subgroups, function($a, $b) {
 
return $a['order_field_name']->value > $b['order_field_name']->value;
 
});
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top