Carbon-fields: Complex Field: add `collapse()` option

Created on 21 Jun 2016  ·  6Comments  ·  Source: htmlburger/carbon-fields

Maybe it's interesting to collapse the fields by default, at least in the case that the developer has defined a custom header template (#27). The idea is to hide clutter to make it cleaner for the user.

Somewhat related to #41 (add collapse all button).


Edit: changed issue title, it would be better to just add a flag as mentioned by @m1r0 instead of assuming that if there is a template the field should collapse.

feature

Most helpful comment

Nice idea! We will consider adding a collapsed flag option to the complex field in a future version.

All 6 comments

This would be perfect for what I need.

Currently I have a complex field set up on pages where a user will commonly create 10+ rows containing 4 or 5 fields each - makes for lots of scrolling. Collapsing by default would make the screen a lot more readable.

I've just made this little script to handle this situation:

function cf_collapse_complex_fields($field) {
    add_action('in_admin_footer', function () use ($field) {
        ?>
        <script type="text/javascript">
            jQuery(function () {
                var counter = 0; // Tries counter
                var tries   = 10;
                var delay   = 250;
                var query   = '#carbon-_<?php echo $field; ?>-complex-container .carbon-btn-collapse';

                function check() {
                    var els = jQuery(query);

                    if (els.length) {
                        els.click();
                    } else if (counter < tries) {
                        counter++;
                        setTimeout(check, delay);
                    }
                }

                check();
            });
        </script>
        <?php
    });
}

// Somewhere else
// `program` is the name of the complex field to collapse
cf_collapse_complex_fields('program');

Nice idea! We will consider adding a collapsed flag option to the complex field in a future version.

I'm working on this, trying to understand how CF works… I was thinking in something like this:

  • add collapse() method in php
  • in backbone:

    • if a group/row is duplicated keep the collapsed state (if collapsed remains collapsed, if opened remains opened).

    • when adding a new group/row always show it opened

But maybe, you prefer to keep it opened both when creating and duplicating?

@doup, yeah, keeping the group open in both situations maybe makes more sense. We may change this depending on how it feels in practice.

This feature was added as set_collapsed(). Thanks @doup for the implementation! 👍

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArekZw picture ArekZw  ·  4Comments

emohamed picture emohamed  ·  3Comments

halvardos picture halvardos  ·  3Comments

Roman52 picture Roman52  ·  3Comments

bjoernhasse picture bjoernhasse  ·  3Comments