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.
Asked
Active
Viewed 1,228 times
1 Answers
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');
});

Community
- 1
- 1

Allie Fitter
- 1,689
- 14
- 18
-
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. – Lorry Apr 07 '17 at 08:13
-
They should be relatively small files. I doubt it would bloat your codebase too much. – Allie Fitter Apr 07 '17 at 15:31