【问题标题】:How to add CSS classes to Zend_Form_Element_Select option如何将 CSS 类添加到 Zend_Form_Element_Select 选项
【发布时间】:2010-02-07 18:03:40
【问题描述】:

我正在尝试将 CSS 类添加到 Zend_Form_Element_Select 选项,但我找不到方法。

想要的输出应该是这样的:

<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 元素中的任何选项,尽管我可以设置 select 元素本身的样式。

我的代码:

$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 样式添加到选择框中的选项的方法,仅适用于选择本身。

那么,我该如何添加它们?我应该扩展 form_element_select 吗?或者定制装饰器就足够了?谁能给我一个提示?我对此感到困惑。

提前致谢!

【问题讨论】:

标签: php zend-framework zend-form zend-form-element css


【解决方案1】:
$htmlEgressCss='<style>';
$multiOptions = array("" => "All");
$resEg = $this->commonDB->getEgressTrunk();
while ($row = $resEg->fetch()) {
    if($row['IsActive']==0){
        $htmlEgressCss .= '.egressClass select, option[value="'.$row['TrunkInfoID'].'"] {color:red;font-weight:bold;}';
    }
    $multiOptions[$row['TrunkInfoID']] = $row['IngressTrunkName'];
}
$htmlEgressCss.='</style>';
$this->addElement(
        'select',
        'cmbEgressTrunk',
        array(
            'multiOptions' =>$multiOptions,
        )
    );
$html = '<form><div>'.$this->cmbEgressTrunk .'</div></form>'.$htmlEgressCss;

【讨论】:

    【解决方案2】:

    这确实是不可能的,但已经要求实施:

    【讨论】:

      【解决方案3】:

      您可以使用 Javascript,特别是 jQuery 来添加它。这将导致选择下拉菜单的背景被着色。

      <style type="text/css">
          .t1 {background: red; color:#fff;}
      </style>
      <form>
          <select id="test">
              <option value="abc">ABC</option>
              <option value="123">123</option>
              <option value="foo">Foo</option>
          </select>
      
      </form>
      
      <script type="text/javascript">
      $('#test [value*="abc"]').addClass('t1');
      $('#test [value*="foo"]').addClass('t1');
      </script>
      

      【讨论】:

        【解决方案4】:

        形式:

        <?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:

        <?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
        }
        
        ?>
        

        文件 application/views/helpers/glxFormSelect.php 或 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 结果代码(样式 padding-left 添加):

        <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>
        ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-05-26
          • 1970-01-01
          • 2023-03-06
          • 1970-01-01
          • 1970-01-01
          • 2017-06-26
          • 1970-01-01
          相关资源
          最近更新 更多