0

In FlaskAdmin, how do I disable a set of fields so that they cannot be edited if a checkbox is not ticked? For instance, here if enabled is not checked, I would like to gray out the other fields so that they cannot be edited.

Screenshot: enter image description here

1 Answer 1

0

After a cursory glance at the FlaskAdmin docs and similar questions on this site, there doesn't seem to be a clear way of accomplishing this. (I could be wrong as I haven't used this extension.) Despite that, you can accomplish this in JavaScript. In your Jinja, you can pretty easily set a class on your fields that can then be used to select elements in JavaScript. Here's a very rough example.

Jinja

{{ field(class_="checkbox") }}
{{ field(class_="input") }}
{{ field(class_="input") }}
{{ field(class_="input") }}

JQuery

$('body').off('change.checkbox')
    .on('.checkbox', 'change.checkbox', function(e) {
        if (this.checked) {
            return $('.input').attr('disabled', '');
        }
        return $('.input').removeAttr('disabled');
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks for this Allie Fitter. After much research I have come to a similar conclusion. I had hoped not to have to do a bunch of extra templates though.
They should be relatively small files. I doubt it would bloat your codebase too much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.