How to add CSS classes to Zend_Form_Element_Select option

岁酱吖の 提交于 2019-12-05 20:16:11
$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;

This is indeed not possible but already requested to be implemented:

See

You can add it with Javascript, specifically jQuery. This will result in the background of the select dropdown to be colored.

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

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())
;

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
}

?>

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

?>

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