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

☆樱花仙子☆ 提交于 2019-12-23 17:34:48

问题


So I am using a Zend_Form_Element_MultiCheckbox to 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?


回答1:


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.



来源:https://stackoverflow.com/questions/6944753/zend-form-element-multicheckbox-how-to-display-a-long-list-of-checkboxes-as-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!