如何在网格视图中显示表单

问题描述 投票:0回答:1

我正在尝试使用网格视图。我显示不同的内容,因此用户可以拥有类似仪表板的内容。

请看截图:

enter image description here

notizen区域应该是搜索表单。

这是我的控制器动作的一部分:

 $view = new ViewModel();

    $wiedervorlageView = new ViewModel(['wvs' => $this->wvTable->fetchAll()]);
    $wiedervorlageView->setTemplate('layout/template/wiedervorlage');

    $notizenView = new ViewModel(['notizen' => $this->notizenTable->fetchAll()]);
    $notizenView->setTemplate('layout/template/notizen');

    $geburtstageview = new ViewModel();
    $geburtstageview->setTemplate('layout/template/geburtstageview');

    $sidebarBlockView = new ViewModel(['aps' => $this->ansprechpartnerTable->getGeburtstage()]);
    $sidebarBlockView->setTemplate('layout/template/block');

    $geburtstageview->addChild($sidebarBlockView, 'block');

    $view->addChild($wiedervorlageView, 'wiedervorlage')
    ->addChild($notizenView, 'notizen')
    ->addChild($geburtstageview, 'geburtstage');

    return $view;

在这里搜索视图:

<?php
$title = 'Suche';      
$this->headTitle($title);
?>
<h1><?= $this->escapeHtml($title) ?></h1>
<?php

$suche= $form->get('suche');
$suche->setAttribute('class', 'form-control');
$suche->setAttribute('placeholder', 'suche');

$suchtyp= $form->get('suchtyp');
$suchtyp->setAttribute('class', 'form-control');
$suchtyp->setAttribute('placeholder', 'suchtyp');


$submit = $form->get('submit');
$submit->setAttribute('class', 'btn btn-primary');

$form->prepare();

echo $this->form()->openTag($form);
?>

<div class="form-group">
    <?= $this->formLabel($suche) ?>
    <?= $this->formElement($suche) ?>
    <?= $this->formElementErrors()->render($suche, ['class' => 'help-block']) ?>
</div>

<div class="form-group">
    <?= $this->formLabel($suchtyp) ?>
    <?= $this->formElement($suchtyp) ?>
    <?= $this->formElementErrors()->render($suchtyp, ['class' => 'help-block']) ?>
</div>

<?php
echo $this->formSubmit($submit);
echo $this->form()->closeTag();

我的问题是如何更改notizen区域以使其与表单一起使用?我尝试了不同的可能性,但我得出的结论是,我不理解这种情况下的逻辑。任何帮助或解释表示赞赏。

解:

好吧,在我想尝试使用所描述的概念来看看局部的区别之前,我改为局部。我之前和偏爱一起工作过,我在这里的挑战实际上只是形式,现在很有希望。但无论哪种方式,部分概念都更加方便和可读。

所以,我改变了建议的视图脚本,这里完成了一个带有以下形式的视图:

<div class="row">
<h2> Suche </h2>
<?php
    $suche= $form->get('suche');
    $suche->setAttribute('class', 'form-control');
    $suche->setAttribute('placeholder', 'suche');

    $suchtyp= $form->get('suchtyp');
    $suchtyp->setAttribute('class', 'form-control');
    $suchtyp->setAttribute('placeholder', 'suchtyp');

    $submit = $form->get('submit');
    $submit->setAttribute('class', 'btn btn-primary');

    $form->prepare();

    echo $this->form()->openTag($form);
    ?>

   <div class="form-group">
        <?= $this->formRow($suche) ?>
        <?= $this->formElementErrors()->render($suche, ['class' => 'help-block']) ?>
        <?= $this->formRow($suchtyp) ?>
        <?= $this->formElementErrors()->render($suchtyp, ['class' => 'help-block']) ?>  

        <?php
        echo $this->formSubmit($submit);
        echo $this->form()->closeTag(); ?>

   </div>
</div>

当我发布这个时,我想我需要使用搜索脚本重定向到控制器Action。现在我将它复制到相同的控制器动作。所以它有效,对我来说没问题。这是对具有相同理解问题的任何人更改控制器操作的一部分:

  $form = new SearchForm(NULL);
        $form->get('submit')->setValue('suche');

        return new ViewModel([
            'wvs' => $this->wvTable->fetchAll(),
            'form' => $form,
            'aps' => $this->ansprechpartnerTable->getGeburtstage()
        ]);
zend-framework zend-form zend-framework3 zend-view
1个回答
2
投票

你应该学习create partials。你现在正在做的是混合问题。

如果/何时必须重构,你在控制器中所做的事情会给你带来巨大的麻烦。

有部分你会做这样的事情。

文件:notizen-partial.phtml

<?php
/** @var array $notizen */
?>

<table>
    <tr>
        <th>datum</th>
        <th>text</th>
    </tr>
    <tr>
        <td><?= $notizen['datum'] ?></td>
        <td><?= $notizen['text'] ?></td>
    </tr>
</table>

因此,上面需要一个类型数组的变量$notizen(在顶部的PHP中暗示)。您可以添加一些数组实际存在的验证,包含值等,但这取决于您。

现在,使用以下配置在模块的配置中注册此部分:

'view_manager'    => [
    'template_map'        => [
        'partial/notizen-partial' => __DIR__ . '/../view/partials/notizen-partial.phtml',
    ],
],

确保你纠正你的情况的路径!

对数据处理的每个小“容器”重复上述操作(wvsaps等)。


接下来,让Action将您需要的数据返回给View:

// ... create/get data

return [
    'wvs'     => $this->wvTable->fetchAll(),
    'notizen' => $this->notizenTable->fetchAll(),
    'aps'     => $this->ansprechpartnerTable->getGeburtstage(),
];

这是“action-view.phtml”,或者其他什么。此文件处理“将数据放在需要的位置”。

根据你的截图,视图的布局将是这样的(我假设你使用Bootstrap 4. *):

<div class="row">
    <div class="col-12">
        <!-- That top row - full width -->
        <!-- ## notizen here ## -->
    </div>
</div>

<div class="row">
    <div class="col-8">
         <!-- second row - two thirds of width -->
         <!-- Wiedervorlagen here -->
    </div>
    <div class="col-4">
         <!-- second row, second column - one thirds of width -->
         <!-- Geburtstage here -->
    </div>
</div>

现在,您需要使用其他部分来在正确的位置显示数据。例如,您的'notizen'数据现在是此视图中的$notizen变量。但是,我们在这个视图中没有使用它,所以让我们把它传递给我们之前创建的部分,如下所示:

<?= $this->partial('partial/notizen-partial, ['notizen' => $notizen]) ?>

这里发生了很多事情:

  • 调用partial ViewHelper(查看此文件的文件夹以查看其他ViewHelpers已经可用!)
  • 将它传递给我们之前在配置中设置的partial的名称
  • 传递键/值对。键将是变量名,值将是它们的值(您可以添加任意数量/需要)

将此调用放在您的操作视图中,如下所示:

<div class="row">
    <div class="col-12">
        <!-- That top row - full width -->
        <?= $this->partial('partial/notizen-partial, ['notizen' => $notizen]) ?>
    </div>
</div>

而且你已经完成了。


你现在有:

  • 单个部分显示数据集合
  • 使用ViewHelpers,您可以在整个应用程序中的不同位置回收相同的视图
  • 您的代码分开关注(保持在phtml模板文件中显示,在Controller中保存数据收集/处理,在存储库/模型/实体中保留逻辑)

我刻意忽略了你的粘贴表格。原理是相同的,你可以将$form传递给部分,并让部分包含表单的显示。创建可重用的东西。

如果您想了解有关自定义Form ViewHelpers的更多信息(例如FormRow,FormElement,FormNumber等),请参阅answers I've given others,例如this one

© www.soinside.com 2019 - 2024. All rights reserved.