• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Zend – 内部自定义属性setMultiOptions

php 来源:Seik 3次浏览

我尝试在setMultiOptions中添加自定义属性,但我不能。 我有类似:Zend – 内部自定义属性setMultiOptions

$locale = Zend_Registry::get('Zend_Translate')->getLocale(); 
$territories = Zend_Locale::getTranslationList('territory', $locale, 2); 

$this->setMultiOptions($territories); 

而这个回报:

<select class="field_container field_14 option_1 parent_1" data-field-id="14" style="" show="1" id="1_1_14" name="1_1_14"> 
    <option label="" value=""></option> 
    <option label="Andorra" value="AD">Andorra</option> 
</select> 

我想里面添加像

<option label="Andorra" **data-image="Graphics/Fruits/Andorra.png"** value="AD">Andorra</option> 

===========解决方案如下:

要在选项标签添加属性自定义属性,我使用一个特定的而不是Zend_View_Helper_FormSelect。

这是My\View\Helper\FormSelectDy.php文件

<?php 

/** 
* Abstract class for extension 
*/ 
require_once 'Zend/View/Helper/FormElement.php'; 


/** 
* Helper to generate "select" list of options 
* 
* @category Zend 
* @package Zend_View 
* @subpackage Helper 
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) 
* @license http://framework.zend.com/license/new-bsd  New BSD License 
*/ 
class My_View_Helper_FormSelectDy extends Zend_View_Helper_FormElement 
{ 
    /** 
    * Generates 'select' list of options. 
    * 
    * @access public 
    * 
    * @param string|array $name If a string, the element name. If an 
    * array, all other parameters are ignored, and the array elements 
    * are extracted in place of added parameters. 
    * 
    * @param mixed $value The option value to mark as 'selected'; if an 
    * array, will mark all values in the array as 'selected' (used for 
    * multiple-select elements). 
    * 
    * @param array|string $attribs Attributes added to the 'select' tag. 
    * 
    * @param array $options An array of key-value pairs where the array 
    * key is the radio value, and the array value is the radio text. 
    * 
    * @param string $listsep When disabled, use this list separator string 
    * between list values. 
    * 
    * @return string The select tag and options XHTML. 
    */ 
    public function formSelectDy($name, $value = null, $attribs = null, 
     $options = null, $listsep = "<br />\n") 
    { 

     $info = $this->_getInfo($name, $value, $attribs, $options, $listsep); 
     extract($info); // name, id, value, attribs, options, listsep, disable 

     // force $value to array so we can compare multiple values to multiple 
     // options; also ensure it's a string for comparison purposes. 
     $value = array_map('strval', (array) $value); 

     // check if element may have multiple values 
     $multiple = ''; 

     if (substr($name, -2) == '[]') { 
      // multiple implied by the name 
      $multiple = ' multiple="multiple"'; 
     } 

     if (isset($attribs['multiple'])) { 
      // Attribute set 
      if ($attribs['multiple']) { 
       // True attribute; set multiple attribute 
       $multiple = ' multiple="multiple"'; 

       // Make sure name indicates multiple values are allowed 
       if (!empty($multiple) && (substr($name, -2) != '[]')) { 
        $name .= '[]'; 
       } 
      } else { 
       // False attribute; ensure attribute not set 
       $multiple = ''; 
      } 
      unset($attribs['multiple']); 
     } 

     // now start building the XHTML. 
     $disabled = ''; 
     if (true === $disable) { 
      $disabled = ' disabled="disabled"'; 
     } 

     // Build the surrounding select element first. 
     $xhtml = '<select' 
       . ' name="' . $this->view->escape($name) . '"' 
       . ' id="' . $this->view->escape($id) . '"' 
       . $multiple 
       . $disabled 
       . $this->_htmlAttribs($attribs) 
       . ">\n "; 

     // build the list of options 
     $list  = array(); 
     $translator = $this->getTranslator(); 
     foreach ((array) $options as $opt_value => $opt_label) { 

      if (is_array($opt_label) 
        && (!isset($opt_label['attribut_spe'])) 
        ) { 
       $opt_disable = ''; 
       if (is_array($disable) && in_array($opt_value, $disable)) { 
        $opt_disable = ' disabled="disabled"'; 
       } 
       if (null !== $translator) { 
        $opt_value = $translator->translate($opt_value); 
       } 
       $opt_id = ' id="' . $this->view->escape($id) . '-optgroup-' 
         . $this->view->escape($opt_value) . '"'; 
       $list[] = '<optgroup' 
         . $opt_disable 
         . $opt_id 
         . ' label="' . $this->view->escape($opt_value) .'">'; 
       foreach ($opt_label as $val => $lab) { 
        $list[] = $this->_build($val, $lab, $value, $disable); 
       } 
       $list[] = '</optgroup>'; 
      } else { 
       if (isset($opt_label['attribut_spe']) 
        && !(is_null($opt_label['attribut_spe'])) 
        && (is_array($opt_label['attribut_spe'])) 
         ) 
       $list[] = $this->_buildattribut($opt_label['attribut_spe'], $value, $disable); 
       else 
       $list[] = $this->_build($opt_value, $opt_label, $value, $disable); 
      } 
     } 

     // add the options to the xhtml and close the select 
     $xhtml .= implode("\n ", $list) . "\n</select>"; 

     return $xhtml; 
    } 

    /** 
    * Builds the actual <option> tag 
    * 
    * @param string $value Options Value 
    * @param string $label Options Label 
    * @param array $selected The option value(s) to mark as 'selected' 
    * @param array|bool $disable Whether the select is disabled, or individual options are 
    * @return string Option Tag XHTML 
    */ 
    protected function _build($value, $label, $selected, $disable) 
    { 
     if (is_bool($disable)) { 
      $disable = array(); 
     } 

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

     // selected? 
     if (in_array((string) $value, $selected)) { 
      $opt .= ' selected="selected"'; 
     } 

     // disabled? 
     if (in_array($value, $disable)) { 
      $opt .= ' disabled="disabled"'; 
     } 

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

     return $opt; 
    } 
    /** 
    * Builds the actual <option> tag 
    * 
    * @param array $attribut_spe Option's attributs 
    *       attribut_spe['affiche'] = 'xxx' => <option ...>'xxx'</option> 
    *       si attribut_spe['affiche'] n'existe pas => <option ...>attribut_spe["label"]</option> 
    *       si attribut_spe["not_escape_affiche"] = true alors <option ...>'xxx'</option> 
    *                  sinon <option ...>$this->view->escape('xxx')</option> 
    *       Toutes les autres valeurs de attribut_spe seront dans les attributs du tag 'option' 
    *       ex.: attribut_spe["class"] = 'toto' et attribut_spe["style"] = 'width:143px;' 
    *          => <option class='toto' style='width:143px;'> ... </option> 
    * @param array $selected The option value(s) to mark as 'selected' 
    * @param array|bool $disable Whether the select is disabled, or individual options are 
    * @return string Option Tag XHTML 
    */ 
    protected function _buildattribut($attribut_spe, $selected, $disable) 
    { 
     if (is_bool($disable)) { 
      $disable = array(); 
     } 

     $opt = '<option'; 
     foreach ($attribut_spe as $cle => $valeur){ 
      if ($cle == "affiche") continue; 
      if ($cle == "not_escape_affiche") continue; 
      $opt .= ' ' . $cle . '="' . $valeur . '"'; 
     } 

     // selected? 
     if (in_array($attribut_spe["value"], $selected)) { 
      $opt .= ' selected="selected"'; 
     } 

     // disabled? 
     if (in_array($attribut_spe["value"], $disable)) { 
      $opt .= ' disabled="disabled"'; 
     } 

     $opt .= '>'; 

     if (isset($attribut_spe["affiche"]) && !empty($attribut_spe["affiche"])) 
      $affiche = $attribut_spe["affiche"]; 
     else 
      $affiche = $attribut_spe["label"]; 

     if (isset($attribut_spe["not_escape_affiche"]) 
      && is_bool($attribut_spe["not_escape_affiche"]) 
      && $attribut_spe["not_escape_affiche"]){ 
      $opt .= $affiche; 
     } 
     else{ 
      $opt .= $this->view->escape($affiche); 
     } 

     $opt .= "</option>"; 

     return $opt; 
    } 
} 

这是相同的帮手比Zend_View_Helper_FormSelect我的新帮手My_View_Helper_FormSelectDy,我添加_buildattribut()方法。

要使用它,你可以这样做:

$locale = new Zend_Locale('en_US'); 
$territories = Zend_Locale::getTranslationList('territory', $locale, 2); 

$territories2 = []; 
foreach($territories as $code => $territory){ 
    $territories2[] = array('attribut_spe'=> 
           array('value'=> $code, 
             'label'=> $territory, 
             'data-image' => "Graphics/Fruits/$territory.png" 
            ) 
           );  
} 

$territories_select = new Zend_Form_Element_Select('territories'); 
$territories_select->setMultiOptions($territories2); 

$territories_select->addDecorators(array(array('ViewHelper', array('helper' => 'formSelectDy'))));  

如果你还没有自己的库,不要忘了在你application.ini

resources.view.helperPath.My_View_Helper = "My/View/Helper" 

当然添加My库帮手,使其适应您的数据图像属性具有良好的价值。

希望它能帮到你


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)