4

I want classical tr-state checkboxes, like the ones on the picture below:

Tri-state checkbox image from Wikipedia

Checked would mean "true for node and all it's children", intermediate "true for some children, leave it as it was" and uncecked "false for all children". Desired values (but that's not a must) are:

  • checked - TRUE
  • unchecked - FALSE
  • intermediate - NULL

Is there a module to provide control like this? Or to provide intermediate value for regular checkboxes?

I need it for Drupal 7, but I'll gladly welcome answers for 6 and 8 too.

4
  • 1
    Nice question and AFAIK, Form API does not support this. You can always set a #default_value if necessary, but for tri-state, you will need some javascript combination like this: is.gd/md9XFu
    – AKS
    Commented May 28, 2013 at 7:40
  • Well, Form API by itself does not support it, but can be extended using modules. That's why I asked for module. "indeterminate" is not universally supported and it's visual only, and I want it to be fully functional. But that plus some hidden fields and it might work. I just hope someone made it happen and now can share. It's hard to believe I'm the first one in Drupal community to need this!
    – Mołot
    Commented May 28, 2013 at 7:44
  • Is there any particular reason not to use select field provided by Form API?
    – Elin Y.
    Commented Jun 8, 2013 at 11:24
  • @ЕлинЙ. Yes, the same reason normal checkboxes exist, and nobody says "hell, let's remove them, yes/no select can do the job just as well". Anyway, my question is "how", not "if" or "why". I know I can get my results with ugly UI all right and I don't feel answers in that line would be on-topic.
    – Mołot
    Commented Jun 8, 2013 at 11:28

1 Answer 1

3

From what I understand, the indeterminate property of a checkbox is purely a visual thing, and also cannot be set with HTML, which in turn means that you won't be able to use the FormAPI or create an indeterminate element without JavaScript.

Chris Coyier sets out some example situations though in this blog post, but an important quote is below (in case the post goes down).

Here are some things to know about indeterminate checkboxes:

You can't make a checkbox indeterminate through HTML. There is no indeterminate attribute. It is a property of checkboxes though, which you can change via JavaScript.

I should imagine you could probably use Drupal.settings to your advantage here though, and run the check server-side for "true for some children", and pass an array of ID's through to Drupal.settings. Following that run something like the following in a Drupal behaviour:

 // This is an array of IDs
 trueForSomeChildren = Drupal.settings.myModule.trueForSomeChildren;

 $(trueForSomeChildrenjoin(', ')).attr("indeterminate", true);

Howeverm I don't think there's an already-built module to do what you want. There is a patch here for the FacetAPI where they allow for indeterminate, but it kind of follows this example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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