2

I am currently writing a module with many backend settings, some of these use the "showon" attribute.

For example: I have two fields of type "list". In the second one, the options are evaluated with the "showon" of the first list.

    <field  name="fieldCat" 
            type="list" 
            default="0" 
            required="" 
            label="Category:" 
            description="">
        <option value="">Select</option>
        <option value="1">Category 1</option>
        <option value="2">Category 2</option>
        <option value="3">Category 3</option>
    </field>
    
    <field  name="fieldItem" 
            type="list" 
            default="0" 
            required="" 
            label="Item:" 
            description="">
        <option value="">Select</option>
        <option value="1" showon="fieldCat:1">Item 1</option>
        <option value="2" showon="fieldCat:2">Item 2</option>
        <option value="3">Item 3</option>
    </field>

If I set the first list to Category 1, Item 1 is displayed and can be selected.

But if I now select Category 2, Item 1 remains in the second list. Please see the picture:

enter image description here

Is it possible to reset the value of the second list (fieldItem) to default when changing the "higher-level" list (fieldCat)? Is this only possible with a custom field?

My currently soulution is, that I have separate subordinate lists (fieldItem1, fieldItem2) for each fieldCat value.

4
  • No, ‘showon’ is not for changing field values, so either you create a custom field solution on that or you use your already created solution.
    – Zollie
    May 17 at 12:39
  • Earlier I had a long write-up on how to create dynamically changing custom list/selection fields using Ajax. Maybe it can help you on the subject: joomla.stackexchange.com/questions/28763/…
    – Zollie
    May 17 at 12:42
  • Thanks for the answers Zollie and for the massive tutorial at How to dynamically reload admin-form-fields in Joomla. Correct me if i am wrong, but i cant create a "models" folder for standalone modules right? Where I need to create the code for the custom fields?
    – r4id
    May 18 at 7:47
  • @r4id - theoretically you can add models to a module too (and you can access DB Tables from a module directly without a model), but if you basically will not have too many values for the Form fields then you can just get the list/selection field values from an object or array coded in your custom Form field directly or in your module’s Helper file, you can also create fields folder within a module if you need that, so there are many ways to reformulate these codes of course. In a module you can also use Joomla base Classes which can help you.
    – Zollie
    May 18 at 10:49
2

You need to use your own custom JS for this.

Here is an example for your given XML.

(function($) {
    var catFieldName = 'fieldCat';
    var itemFieldName = 'fieldItem';
    
    var showHideFieldOptions = function() {
        var selectedCategory = $('select[name="jform[params][' + catFieldName + ']"]').val(),
            $itemField = $('select[name="jform[params][' + itemFieldName + ']"]');
        
        $itemField.find('option[value="1"], option[value="2"]').attr('disabled', true);
        
        if (selectedCategory == 1) {
            $itemField.find('option[value="1"]').removeAttr('disabled');
        } else if (selectedCategory == 2) {
            $itemField.find('option[value="2"]').removeAttr('disabled');
        }
        
        $itemField.trigger('liszt:updated');
    }
    
    $(function() {
        showHideFieldOptions();
        
        $('select[name="jform[params][' + catFieldName + ']"]').on('change', function() {
            showHideFieldOptions();
        });
    });
    
})(jQuery);

It will be better if you set the item field as required (required="true").

3
  • Yes, this is also a good and the simplest approach for solving this.
    – Zollie
    May 18 at 16:18
  • 1
    How about passing multiple selectors to find() when disabling? stackoverflow.com/a/11758129/2943403 Also, why not pass selectedCategory into find() when removing the attr, so that you don't need if and else if? PHPStorm would probably complain to me if I wrote $('select[name="jform[params][' + itemFieldName + ']"]') 5 times instead declaring a re-usable variable. jsfiddle.net/rkhm6ywa
    – mickmackusa
    May 18 at 16:19
  • Yes, you can adjust the script based on actual usage. I have slightly edited the codes to minimise repetition.
    – Jumbo
    May 18 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.