conditionize2.js icon indicating copy to clipboard operation
conditionize2.js copied to clipboard

Feature request: disable fields action handler

Open Seb33300 opened this issue 4 years ago • 2 comments

Hello,

I would like to see a new action handler disableFields very similar to the clearFields handler.

Instead of clearing fields, I would like to disable them.

It's quite similar to the clearFields because disabled field are not submitted. But the advantage is that if the user hide the field, and then display it again, all the values he started to enter are not lost. Its also possible to set default values that will not be cleared and not submitted if the fields are hidden.

Thank you

Seb33300 avatar May 21 '21 08:05 Seb33300

Hello,

Thanks for your suggestion. I am not sure if I got your idea. It would be nice if you could provide a PR or, at least, an example.

Another thing is that for disableFields it is not enough to have one action, so one will need also to un-disable fields. So, it looks like it is simpler to write your custom ifTrue and ifFalse functions.

rguliev avatar May 21 '21 22:05 rguliev

Oups, sorry I never answered this. Here is how I fixed it:

$('...').conditionize({
    ifTrue: [
        // ...
        function ($s) {
            if ($s.is('option')) {
                $s.prop('disabled', false).insertAfter($s.parent().find(':not([disabled]):last'));
            } else {
                $s.find('input, select, textarea, button').prop('disabled', false);
            }
        }
    ],
    ifFalse: [
        // ...
        function ($s) {
            if ($s.is('option')) {
                if ($s.is(':selected')) {
                    $s.closest('select').val(null);
                }
                $s.prop('disabled', true).appendTo($s.parent());
            } else {
                $s.find('input, select, textarea, button').prop('disabled', true);
            }
        }
    ]
});

Seb33300 avatar Nov 20 '22 04:11 Seb33300