1. Fabrik 3.8.1 has been released. It is mostly bug fixes and feature enhancements, but does include two new plugins (push notifications, and the sequence element). As usual we strongly recommend testing the new release on a sandbox if your application is mission critical, and always do an Akeeba backup before updating.
    Dismiss Notice

Condition the display of repeated groups according to a field

Discussion in 'Community' started by georgie, Sep 23, 2018.

  1. georgie

    georgie Member

    Level: Community
    Hello

    I would to limit the display of a repeated group according to a field.
    I mean to hide each row from a group repeatable if his field is equal to "0" for example.

    Can I do this in the form template?

    Georges
     
  2. Sophist

    Sophist Well-Known Member

    Level: Community
    You can use javascript on-load and on-change events to hide the elements. That means that they are in the HTML if someone sufficiently knowledgeable wants to look, but they are not visible in the web page.
     
    georgie likes this.
  3. georgie

    georgie Member

    Level: Community
    Héhé

    Thank you, but it is not exactly I would.
    Javascript rules go to hide all the group or only element. But me I want to hide rows related only.

    It is something like Prefilters in list, but I can not use Prefilters in my main list, because it hide records when just one row is related.

    No, I think I have to do this in PHP in my form template, or creating a list pointing on my subgroup (and then prefilter on this sublist).
    But I would prefer do this in PHP in my form template.

    Georges
     
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    You could do it in a custom template, editing default_repeatgroup.php, around line 21 at the start of the foreach. Test your element's value, and just 'continue' if it is 0 ...

    Code (Text):

    if ($subgroup['yourfield']->value === '0'):
       continue;
    endif;
     
    -- hugh
     
    georgie likes this.
  5. georgie

    georgie Member

    Level: Community
    Hello

    Thank you for your help, indeed I would do it in a custom template.
    But it does not work as it, or maybe I do a mistake.

    I try this, but with no effect:

    PHP:

    <?php

    // No direct access
    defined('_JEXEC') or die('Restricted access');

    $input = JFactory::getApplication()->input;
    $group = $this->group;
    $i = 1;
    $w = new FabrikWorker;

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

    // ADD TEST
    if ($subgroup['commande___DUE_TTC_raw']->value === '0.00'):
    continue ;
    endif ;

        $introData = array_merge($input->getArray(), array('i' => $i));
        ?>
        <div class="fabrikSubGroup">
            <div data-role="group-repeat-intro">
                <?php echo $w->parseMessageForPlaceHolder($group->repeatIntro, $introData);?>
            </div>
        <?php
            // Add the add/remove repeat group buttons
            if ($group->editable && ($group->canAddRepeat || $group->canDeleteRepeat)) : ?>
                <div class="fabrikGroupRepeater pull-right btn-group">
                    <?php if ($group->canAddRepeat) :
                        echo $this->addRepeatGroupButton;
                    endif;
                    if ($group->canDeleteRepeat) :
                        echo $this->removeRepeatGroupButton;
                    endif;?>
                </div>
            <?php
            endif;
            ?>
            <div class="fabrikSubGroupElements">
                <?php

                // Load each group in a <ul>
                $this->elements = $subgroup;
                echo $this->loadTemplate('group');
                ?>
            </div><!-- end fabrikSubGroupElements -->
        </div><!-- end fabrikSubGroup -->
        <?php
        $i ++;

    endforeach;
     
    Also I do not understand how my template knows in what subgroups to work, only by the field?
    Indeed I have several subgroups in my form, and also the subgroup concerned is in "Read only".

    Georges
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Don't use the _raw suffix, that won't exist.

    Try something like this ...

    Code (Text):

    if (array_key_exists('commande___DUE_TTC', $subgroup)):
       //var_dump($subgroup['commande___DUE_TTC']);
       if ($subgroup['commande___DUE_TTC']->value === '0.00'):
          continue ;
       endif;
    endif;
     
    If it doesn't work, uncomment the var_dump() and see if that outputs anything.

    -- hugh
     
    georgie likes this.
  7. georgie

    georgie Member

    Level: Community
    Thanks, I understand better (a little bit...).

    But it does not work for now, no bug but no effect.

    Uncommenting var_dump does not display anything, no bug but no effect.

    I note I can do a var_dump like this:
    Code (Text):
    $My_Var = $this->getModel()->getElementData('commande___DUE_TTC', true) ;
    var_dump($My_Var) ;
    This display a correct array, but not in the good subgroup. The array is displayed in my other subgroups.

    Please have you an idea with theses new details?

    Georges
     
  8. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Oh, sorry, at that point it's using short element names, so just change commande___DUE_TTC to DUE_TTC. Hopefully you don't use the same name in a different group.

    -- hugh
     
    georgie likes this.
  9. georgie

    georgie Member

    Level: Community
    Thank you but no change...

    Also I can not do a dump, so I do not undertand.

    Note my DUE_TTC field use a format string (%s €). It is not a problem?

    Please see my actual code (no bug but no change):

    Code (Text):
    <?php

    // No direct access
    defined('_JEXEC') or die('Restricted access');

    $input = JFactory::getApplication()->input;
    $group = $this->group;
    $i = 1;
    $w = new FabrikWorker;

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

    // ADD TEST
    if (array_key_exists('DUE_TTC', $subgroup)):
       // var_dump($subgroup['DUE_TTC']);
       if ($subgroup['DUE_TTC']->value === '0'):
          continue ;
       endif;
    endif;

        $introData = array_merge($input->getArray(), array('i' => $i));
        ?>
        <div class="fabrikSubGroup">
            <div data-role="group-repeat-intro">
                <?php echo $w->parseMessageForPlaceHolder($group->repeatIntro, $introData);?>
            </div>
        <?php
            // Add the add/remove repeat group buttons
            if ($group->editable && ($group->canAddRepeat || $group->canDeleteRepeat)) : ?>
                <div class="fabrikGroupRepeater pull-right btn-group">
                    <?php if ($group->canAddRepeat) :
                        echo $this->addRepeatGroupButton;
                    endif;
                    if ($group->canDeleteRepeat) :
                        echo $this->removeRepeatGroupButton;
                    endif;?>
                </div>
            <?php
            endif;
            ?>
            <div class="fabrikSubGroupElements">
                <?php

                // Load each group in a <ul>
                $this->elements = $subgroup;
                echo $this->loadTemplate('group');
                ?>
            </div><!-- end fabrikSubGroupElements -->
        </div><!-- end fabrikSubGroup -->
        <?php
        $i ++;

    endforeach;
    Georges
     
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    So no debug outputs if you uncomment that var_dump()?

    -- hugh
     
    georgie likes this.
  11. georgie

    georgie Member

    Level: Community
    Exact, no debug output.

    Georges
     
  12. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    This will probably genefrate a lot of debug code, but there's no other way for me to see what's going on, so put ...

    var_dump($subgroup)

    ... just after the foreach.

    I'll need to see all the debug output.

    -- hugh
     
  13. georgie

    georgie Member

    Level: Community
    Yes I tried, without much change so I did not pay much attention.
    Now, re-trying, I think begin to understand something! Thank you, I will try to explain:

    When I put

    var_dump($subgroup)

    just after the foreach, so my others groups (the others, not the one concerned), display a NULL. And the group concerned does not display nothing.

    And if I try to use another field used in one of these other groups, yeah it works!
    Indeed it displays the array and value, or it hides the group with your code. Good!

    So the problem is only in a group, precisely in the one I need...
    And it is exactly the only group which is in "Always show as read Only".

    I think it is about my custom template, which has no effect on my group in readonly.

    Georges
     
    Last edited: Oct 15, 2018 at 8:03 PM

Share This Page