我想创建一个多级组合框,如下例所示:
<select style="white-space: pre;">
<option value="0">1 - categoria 0</option>
<option value="1"> 1.1 - categoria 1</option>
<option value="2"> 1.1.1 - categoria 2</option>
<option value="3"> 1.1.1.1 - categoria 3</option>
<option value="4"> 1.1.1.1.1 - categoria 4</option>
<option value="5"> 1.1.1.1.1.1 - categoria 5</option>
</select>
结果应该是:
1 - categoria 0
1.1 - categoria 1
1.1.1 - categoria 2
1.1.1.1 - categoria 3
1.1.1.1.1 - categoria 4
1.1.1.1.1.1 - categoria 5
我正在创建我的 ZF 1.11 表单,如下所示:
class Admin_Form_Category extends Zend_Form
{
public $elementDecorators2 = array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'elementSelect')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
);
public function __construct($options = null)
{
//parent::__construct($options);
$view = new Zend_View();
$baseUrl = $view->baseUrl();
// Translating the form
$translate = Zend_Registry::get('translate');
$this->setName('formcategory');
$this->setAttrib('accept-charset', Zend_Registry::get('config')->resources->view->encoding);
$this->setMethod('post');
$this->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
/* HERE IS MY COMBOBOX */
$parent = new Zend_Form_Element_Select('parent');
$parent->addErrorMessage($translate->_('You must select an parent'));
$parent->setLabel($translate->_('Parent'))
->setDecorators($this->elementDecorators2)
->setRequired(false)
->addFilter('StripTags')
->setValue( isset($options[ $parent->getName() ]) ? $options[ $parent->getName() ] : '');
$model = new App_Models_Category();
$data = $model->fetchAll();
$parent->addMultiOption('','');
foreach($data as $row){
$itemLevel = str_repeat(" ",$row['level']);
$parent->addMultiOption($row['category'],$itemLevel.$row['name']);
}
$this->addElement($parent);
/* ... */
}
}
但是,由于某种原因,组合框是这样创建的:
<select id="parent" name="parent">
<option selected="selected" label="" value=""></option>
<option label="teste" value="14">teste</option>
<option label="test3" value="16">test3</option>
<option label="test4" value="17">test4</option>
<option label="&nbsp;final" value="23">&nbsp;final</option>
<option label="&nbsp;&nbsp;final2" value="24">&nbsp;&nbsp;final2</option>
</select>
结果是:
teste
test3
test4
final
final2
然后,不显示空格...
有人可以帮助我吗?!
我忘了说,我的 ZF 表格有这个 css:
选择{空白:pre;}