开发者

Zend_Form_Element_MultiCheckbox: How to display a long list of checkboxes as columns?

So I am using a Zend_Form_Element_MultiCheckbox to 开发者_JAVA技巧display a long list of checkboxes. If I simply echo the element, I get lots of checkboxes separated by <br /> tags. I would like to figure out a way to utilize the simplicity of the Zend_Form_Element_MultiCheckbox but also display as multiple columns (i.e. 10 checkboxes in a <div style="float:left">). I can do it manually if I had an array of single checkbox elements, but it isn't the cleanest solution:

<?php
    if (count($checkboxes) > 5) {
        $columns = array_chunk($checkboxes, count($checkboxes) / 2); //two columns
    } else {
        $columns = array($checkboxes);
    }
?>

<div id="checkboxes">
    <?php foreach ($columns as $columnOfCheckboxes): ?>

        <div style="float:left;">

            <?php foreach($columnOfCheckboxes as $checkbox): ?>
                <?php echo $checkbox ?> <?php echo $checkbox->getLabel() ?><br />
            <?php endforeach; ?>

        </div>

    <?php endforeach; ?>
</div>

How can I do this same sort of thing and still use the Zend_Form_Element_MultiCheckbox?


The best place to do this is using a view helper. Here is something I thought of really quickly that you could do. You can use this in your view scripts are attach it to a Zend_Form_Element.

I am going to assume you know how to use custom view helpers and how to add them to form elements.

class My_View_Helper_FormMultiCheckbox extends Zend_View_Helper_FormMultiCheckbox
{
    public function formMultiCheckbox($name, $value = null, $attribs = null,
        $options = null, $listsep = "<br />\n")
    {
        // zend_form_element attrib has higher precedence 
        if (isset($attribs['listsep'])) {
            $listsep = $attribs['listsep'];
        }

        // Store original separator for later if changed
        $origSep = $listsep;

        // Don't allow whitespace as a seperator 
        $listsep = trim($listsep);

        // Force a separator if empty
        if (empty($listsep)) {
            $listsep = $attribs['listsep'] = "<br />\n";
        }

        $string  = $this->formRadio($name, $value, $attribs, $options, $listsep);
        $checkboxes = explode($listsep, $string);

        $html = '';
        // Your code
        if (count($checkboxes) > 5) {
            $columns = array_chunk($checkboxes, count($checkboxes) / 2); //two columns
        } else {
            $columns = array($checkboxes);
        }

        foreach ($columns as $columnOfCheckboxes) {
            $html .= '<div style="float:left;">';

            $html .= implode($origSep, $columnOfCheckboxes);

            $html .= '</div>';
        }

        return $html;
    }
}

If you need further explanation just let me know. I did this fairly quickly.

EDIT

The reason I named it the same and placed in a different directory was only to override Zend's view helper. By naming it the same and adding my helper path:

$view->addHelperPath('My/View/Helper',  'My_View_Helper');

My custom view helper gets precedence over Zend's helper. Doing this allowed me to test without changing any of my forms,elements, or views that used Zend's helper. Basically, that's how you replace one of Zend's view helpers with one of your own.

Only reason I mentioned the note on adding custom view helpers and adding to form elements was because I assumed you might rename the helper to better suit your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜