如何添加CSS类到Zend_Form_Element_Select选项 [英] How to add CSS classes to Zend_Form_Element_Select option

查看:239
本文介绍了如何添加CSS类到Zend_Form_Element_Select选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加一个CSS类到Zend_Form_Element_Select选项,但我只是找不到一个办法。

I'm trying to add a CSS class to a Zend_Form_Element_Select option, but I just can't find a way to do it.

所需的输出将是像这样:

The desired output would be something like this:

<select name="hey" id="hey">
    <option value="value1" style="parent">label1</option>
    <option value="value2" style="sibling">sublabel1</option>
    <option value="value3" style="sibling">sublabel2</option>
    <option value="value4" style="parent">label2</option>
    <option value="value5" style="sibling">sublabel3</option>
    <option value="value6" style="sibling">sublabel4</option>
</select>

但我得到这个:

<select name="hey" id="hey">
    <option value="value1">label1</option>
    <option value="value2">sublabel1</option>
    <option value="value3">sublabel2</option>
    <option value="value4">label2</option>
    <option value="value5">sublabel3</option>
    <option value="value6">sublabel4</option>
</select>



我似乎不能将CSS类属性传递给select元素中的任何选项

I can't seem to pass a CSS class attribute to any of the options in the select element although I can style the select element itselft.

我的代码:

$sel = new Zend_Form_Element_Select('hey');
$sel->setRequired(true)->setLabel('Select an Option:');
$sel->addMultiOption('value1', 'label1', array('class' => 'parent'))
    ->addMultiOption('value2', 'sublabel1', array('class' => 'sibling')) (etc...);

经过研究,我发现Element_Select没有一个方法来添加CSS样式到

After researching a bit I found out that Element_Select doesn't have a method for adding CSS styles to the options in the select box, only for the select itself.

那么,我如何添加它们呢?我应该扩展form_element_select吗?或者一个定制的装饰器够了吗?任何人都可以给我一个提示?

So, how can I add them? Should I extend the form_element_select? Or would a custom decorator suffice? Can anyone give me a hint? I'm baffled with this.

提前感谢!

推荐答案

In form:

<?php

require_once 'glx/Form/Element/Select.php'; // custom select class

// ... in init or __create function :

$categories = new Model_DbTable_Categories(); // some Model

$PID = new glx_Form_Element_Select('PID'); // custom select object

$PID
  ->setLabel('PID')
  ->setDecorators(array('ViewHelper'))
  ->addMultiOptions($categories->getSelectOptions())
;

文件库/ glx / Form / Select.php:

File library/glx/Form/Select.php :

<?php

require_once 'Zend/Form/Element/Multi.php';

$error_reporting = error_reporting(0);
@include_once '../application/views/helpers/glxFormSelect.php'; // first, maby here
if (! class_exists('Zend_View_Helper_glxFormSelect'))
  require_once 'glx/View/Helper/glxFormSelect.php'; // or least, maby here
error_reporting($error_reporting);

class glx_Form_Element_Select extends Zend_Form_Element_Multi
{
  public $helper = 'glxFormSelect'; // this is my custom code
}

?>

文件应用程序/ views / helpers / glxFormSelect.php或library / glx / View / Helpe / glxFormSelect。 php:

File application/views/helpers/glxFormSelect.php or library/glx/View/Helpe/glxFormSelect.php :

<?php

require_once 'Zend/View/Helper/FormElement.php';

class Zend_View_Helper_glxFormSelect extends Zend_View_Helper_FormSelect
{
    public function glxFormSelect($name, $value = null, $attribs = null, $options = null, $listsep = "<br />\n")
    {
      return parent::formSelect($name, $value, $attribs, $options, $listsep);
    }

    protected function _build($value, $label, $selected, $disable)
    {
        if (is_bool($disable))
            $disable = array();

        $oldLabel = $label;                                                   // this is my custom code
        $label = ltrim($label);                                               // this is my custom code

        $opt = '<option'
             . ' value="' . $this->view->escape($value) . '"'
             . ' label="' . $this->view->escape($label) . '"';

        if (($countSpaces = strlen($oldLabel) - strlen($label)) > 0)          // this is my custom code
          $opt.= sprintf(' style="padding-left:%dpx"', (15 * $countSpaces));  // this is my custom code

        if (in_array((string) $value, $selected))
            $opt .= ' selected="selected"';

        if (in_array($value, $disable))
            $opt .= ' disabled="disabled"';

        $opt .= '>' . $this->view->escape($label) . "</option>";

        return $opt;
    }
}

?>

最后的HTML结果代码(已添加样式填充):

And final HTML result code (style padding-left added):

<select name="PID" id="PID">
<option value="1" label="Categories" style="padding-left:15px">Categories</option>
<option value="2" label="Publications" style="padding-left:30px">Publications</option>
<option value="83" label="Links" style="padding-left:45px">Links</option>
...

这篇关于如何添加CSS类到Zend_Form_Element_Select选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆