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

how to order list by count of record in each group by section?

Discussion in 'Community' started by samani, Jun 2, 2020.

  1. samani

    samani Member

    Level: Community
    hi
    i use group by in my list and i want sort descending of group with max record. is it possible and how? for example i want come the group of maximum count of record to first of list.
     
  2. juuser

    juuser Active Member

    Level: Community
    Hi, haven't tried or tested this approach, but I would try something like this:

    1) Create a hidden field on that list;
    2) Update the field with the number of records in group with php list plugin on every time the list loads;
    3) In list settings "group by" section, select to order by that hidden field.
     
  3. dimoss

    dimoss Well-Known Member

    Level: Community
    @juuser imho i don't think that adding a hidden field in the same list for counting the grouped records of that list will work. Can you explain more pls?
    I recall that I was looking also for a solution on this.
     
  4. juuser

    juuser Active Member

    Level: Community
    When you have that extra field, you will have your list something like:

    count_and_order_element | your_groupby_element, etc
    3 | some_element_name
    3 | some_element_name
    3 | some_element_name
    2 | some_element_name1
    2 | some_element_name1
    1 | some_element_name2

    Now you can order your list by that "count_and_order_element". And one mistake in my first post, the ordering should be set for the whole list in Data->Order by, not in Order by -> Order by field which is for grpup internal ordering.

    Just tested with small existing table with "count_and_order_element" values added manually. Seems to be working as it should.
     
  5. dimoss

    dimoss Well-Known Member

    Level: Community
    So basically what you say is to put the php list plugin onLoad running the query updating the "hidden element" and then return the grouped records ordered based on that alement?
    I see that this could work in a joined table case.
     
  6. juuser

    juuser Active Member

    Level: Community
    Exactly, and joined or not shouldn't matter.
     
    dimoss likes this.
  7. dimoss

    dimoss Well-Known Member

    Level: Community
    Ok now makes sense to me and maybe I found a "fast and dirty" solution to my joined case scenario :)
     
    juuser likes this.
  8. samani

    samani Member

    Level: Community
    @dimoss @juuser
    i do this and it work for me:
    you must make a new layout (copy from bootstrap folder) template and select that in the List>Details>layout>Templates>Front-end template.
    then i use this code after line 92 in default.php:

    Code (Text):


        $new_rows = $this->rows;
            if($this->isGrouped) {
                $rows = $this->rows;
                function cmp($a,$b) {
                    if(count($a) == count($b)) return 0;
                    return count($a) < count($b)?1:-1;
                }
                $keys = array_keys($rows);
             
                usort($rows, "cmp");
                $new_rows = [];
                foreach ($rows as $index => $r) {
                    $new_rows[$r[0]->data->_groupId] = $r;
                }
            }
            foreach ($new_rows as $groupedBy => $group) :

     
    in this way when you select group by other element , then you can see each element can be select as Group by option and sort group according descendeing.
    test it and feedback to me
     

Share This Page