0

我在渲染Zend表单元素时遇到了一些问题。我们目前正在用Zendframework 2构建一个调查管理平台,因此需要以矩阵样式呈现的问题类型(如链接的图像)。ZF2将矩阵渲染为矩阵

(这也可能只是一个表中的问题,而不是2 - 所以一切都在2条蓝线的右侧可能被丢弃,而在另一个表添加)

https://imageshack.com/i/ndynulp

我目前不知道如何最好地解决这个问题,因为表单中的所有元素当然都是从数据库动态添加的。

什么是最好的解决方案来显示这样的多个单选按钮?

目前我正在尝试编写自定义表单元素,但我并不完全不介意如何让视图助手呈现我的元素,如下图所示(或者甚至我如何告诉他如何呈现我的元素)。

感谢您的帮助:-)

回答

1

好吧我设法通过编写我自己的视图助手,感谢帮助@Sina。 :-)

对于其他人面临这样的问题,我解决它类似如下:

表单元素:(大小属性约是要呈现单选按钮和表头的量)。

<?php 

/** 
* @namespace 
*/ 

namespace ExecuteSurvey\Form\Element; 

/** 
* @uses Zend\Form\Element 
*/ 
use Zend\Form\Element; 

/** 
* Custom Form Element that is here for rendering a Matrix of Radio Elements 
* @category ExecuteSurvey 
* @package ExecuteSurvey_Form 
* @subpackage ExecuteSurvey_Form_Element 
* @author Dominik Einkemmer 
*/ 
class RadioMatrix extends Element { 

    protected $attribute = array(
     'type' => 'radiomatrix' 
    ); 

    private $labels = array(); 

    private $size; 

    public function setSize($size){ 
     if($size == 0 || !is_numeric($size)){ 
      throw new Exception("Size can't be 0 and has to be numeric"); 
     } 
     $this->size = $size; 
    } 

    public function setLabels(array $labels){ 
     if(!is_array($labels)){ 
      throw new \Zend\Form\Exception\InvalidArgumentException("Array expected but ".gettype($labels)." given."); 
     } 
     foreach($labels as $label){ 
      $this->labels[] = $label; 
     } 
    } 

    public function getLabels() { 
     return $this->labels; 
    } 

    public function getSize() { 
     return $this->size; 
    } 
} 

这是视图助手类:

<?php 

/** 
* @namespace 
*/ 

namespace ExecuteSurvey\Form\View\Helper; 

/** 
* @uses Zend\Form\ElementInterface 
* @uses Zend\Form\View\helper\AbstractHelper 
*/ 
use Zend\Form\ElementInterface; 
use Zend\Form\View\Helper\AbstractHelper; 

/** 
* Class FormRadioMatrix which renders the actual element through a view 
* @category ExecuteSurvey 
* @package ExecuteSurvey_Form 
* @subpackage ExecuteSurvey_Form_View 
* @subpackage ExecuteSurvey_Form_View_Helper 
*/ 
class FormRadioMatrix extends AbstractHelper { 

    /** 
    * Path to the .phtml file 
    * @var string 
    */ 
    protected $script = 'execute-survey/form-element/radiomatrix'; 

    /** 
    * Method that renders the View Element 
    * @param \Zend\Form\ElementInterface $element 
    * @return \Zend\View\Renderer\RendererInterface 
    */ 
    public function __invoke(ElementInterface $element) { 
     return $this->getView()->render($this->script, array(
        'element' => $element 
     )); 
    } 

} 

这是将被用来渲染元素的radiomatrix.phtml:

<div class="table-responsive"> 
    <table class="table"> 
     <thead> 
      <tr> 
       <th></th> 
       <?php for ($i = 1; $i <= $element->getSize(); $i++): ?> 
        <th><?php echo $i ?></th> 
       <?php endfor; ?> 
      </tr> 
     </thead> 
     <tbody> 
      <?php foreach ($element->getLabels() as $key => $label): ?> 
        <tr> 
         <td><?php echo $label?></td> 
         <?php for($j = 1;$j<=$element->getSize();$j++):?> 
         <td><input type="radio" value="<?php echo $j?>" name="<?php echo $label.'X'.$key?>" id="<?php echo $element->getName().'X'.$key.'X'.$j?>"></td> 
         <?php endfor;?> 
        </tr> 
      <?php endforeach; ?> 
     </tbody> 
    </table> 
</div> 
+0

很高兴你将它固定<3 –

0

这不能使用默认ZF2视图渲染器PHTML文件

你必须使用你的PHTML内部代码和使用使其你的自我

完成的foreach在你的表单字段进行迭代和建立这样一个复杂的表格

默认$这个 - > formElement()在ZF2视图不能帮助

+0

所以,如果我写我自己的看法我使用$ this-> formMatrix()在我的phtml文件中使用渲染器,这可能吗?如果是的话,写这样一个视图帮助者的最佳解决方案是什么?感谢您的快速回答:-) – Velixir