从下拉列表中过滤“子模型”实例

时间:2015-12-07 15:59:27

标签: php templates pagination typo3 extbase

我用kickstarter创建了一个新的Typo3扩展 我的Cities模型有多个street模型 在模板中,street的{​​{1}}模型列在下拉菜单中,如下所示:

enter image description here

<小时/> 模板代码:

city

现在我需要使用下拉列表上方的第一个字符过滤条目
(A,B,C) 例如。点击<f:section name="main"> <h1>Single View for City</h1> <f:flashMessages renderMode="div" /> <f:render partial="City/Properties" arguments="{city:city}" /> Filter:<br> <a href='<f:uri.action controller="City" action="showFiltered" arguments="{city:city, char : 'a'}" />'>A</a> <a href='<f:uri.action controller="City" action="showFiltered" arguments="{city:city, char : 'b'}" />'>B</a> <a href='<f:uri.action controller="City" action="showFiltered" arguments="{city:city, char : 'c'}" />'>C</a> <br> <f:debug>{city}</f:debug> <select onchange="location = this.options[this.selectedIndex].value;"> <option>Select street</option> <f:for each="{city.streets}" as="street"> <option value='<f:uri.action controller="Street" action="show" arguments="{street : street}" />' >{street.name}</option> </f:for> </select> <br> <f:link.action action="list">Back to list</f:link.action><br /> <br> <f:link.action action="new">New City</f:link.action> </f:section> 后,只显示AAviation ...

当我转出Automation时,我得到了这个:enter image description here

但是我在city行动中不知道如何进入街道:

showFiltered

建立这样的东西:

public function showFilteredAction(\Vendor\CollectionPlan\Domain\Model\City $city) {
        $char = $this->request->getArgument('char');

        $this->view->assign('city', $city);
        $this->view->assign('char', $char);
    }

TL; DR
如何在我的控制器操作中访问城市模型的街道以检查它们是否以给定密钥开头?

1 个答案:

答案 0 :(得分:1)

您在流体控制器中通过点表示法访问的每个字段都有getter和setter(至少如果您使用Extension Builder创建它),除了持久性管理器将删除它们之外,您不应该从集合中删除street,而是添加到您的城市模型 transient 类型为数组,我们将其命名为$streetsFilteredTransient并创建一个将过滤街道的吸气剂:

只需将其添加到您的城市模型中,然后修复 GeneralUtility::_GP方法的参数

/**
 * The transient field in model is that which haven't a declaration
 * in TCA and field in SQL file
 * so it's ideal for *processing* other fields...
 *
 * @var array
 */
protected $streetsFilteredTransient = array();

/**
 * Returns array of street objects which names begins with char,
 * or all streets if no char is given
 *
 * @return array Array of objects
 */
public function getStreetsFilteredTransient() {

    // As model has no access to request you need to fetch the `char` argument *manually*
    $arguments = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_flysites_show');
    $char = (!is_null($arguments) && isset($arguments['char'])) 
        ? htmlspecialchars($arguments['char']) 
        : false;

    foreach ($this->kinds as $street) {
        if (!$char || $this->startsWith($street->getName(), $char))
            $this->streetsFilteredTransient[] = $street;
    }
    return $this->streetsFilteredTransient;
}


private function startsWith($haystack, $needle) {
    $haystack = strtolower($haystack);
    $needle = strtolower($needle);
    $length = strlen($needle);
    return (substr($haystack, 0, $length) === $needle);
}

并在视图中使用它:

<f:for each="{city.streetsFilteredTransient}" as="street">
    ...
</f:for>