1

在一个自定义组件中,在站点视图中,我显示了一个国家列表,每个国家都作为指向另一个页面的链接,显示居住在该国家的人。

这是一个链接:

index.php?option=com_example&view=persons&country=1&Itemid=131

缺少什么:
打开人员页面时,会列出 所有人员。

我在找什么:在上面的示例中,我只想
显示链接中的国家/地区的人。1

我试图在model-filesof中添加这个条件persons,但是失败了。

+++编辑++++

感谢接受的答案,我能够完成我所需要的。不幸的是,这似乎会产生副作用:

Fatal error: Call to a member function getPagesCounter() on a non-object 
in .../view/persons/tmpl/default.php` (...)

抛出该错误的代码是

<?php echo $this->pagination->getPagesCounter(); ?>

注释掉该行时,此代码将出现相同的错误:

<?php echo $this->pagination->getPagesLinks(); ?>

这是怎么发生的,我该怎么办?试图追查这个问题,但不知道从哪里开始。

+++编辑+++

Wasnm 还不能解决这个问题。做了一个var_dump($this->pagination);,这是输出:

array(1) {
  [0]=>
  object(stdClass)#150 (20) {
    ["id"]=>
    string(1) "6"
    ["name"]=>
    string(11) "Fleur Leroc"
    ["country"]=>
    string(1) "2"
    (...)    
    ["ordering"]=>
    string(1) "6"
    ["state"]=>
    string(1) "1"
    ["checked_out"]=>
    string(3) "615"
    ["checked_out_time"]=>
    string(19) "2013-10-10 10:53:14"
    ["created_by"]=>
    string(10) "Super User"
    ["editor"]=>
    string(10) "Super User"
    ["countriestrainers_country_828045"]=>
    string(6) "France"
    ["countriestrainers_flag_828045"]=>
    string(28) "images/trainers/flags/fr.gif"
  }
}

所以对象确实存在,不是吗?

4

1 回答 1

1

您正在关闭编辑模型文件。在您的 Persons 模型 (ExampleModelPersons) 中,您需要确保具有以下元素:

将过滤器名称列入白名单:

<?php
public function __construct($config = array())
    {
        if (empty($config['filter_fields'])) {
            $config['filter_fields'] = array(
                'country',
                // other not standard filters
            );
        }
        parent::__construct($config);
    }
?>

自动填充状态过滤器:

<?php
protected function populateState($ordering = null, $direction = null)
{

    $country = $this->getUserStateFromRequest($this->context.'.filter.country', 'country', '',  null, false);
    $this->setState('filter.country', (int) $country);
        // ....Other states
{
?>

上下文的存储 id:

<?php
protected function getStoreId($id = '')
{
    $id .= ':'.$this->getState('filter.country');
    // Other states
}
?>

而最重要的一项,数据库查询

<?php
protected function getListQuery()
{
    // ... Other parts of the querty
    if ($country = $this->getState('filter.country'))
        $query->where("country = ". (int) $country);
}
?>

如果您不需要在用户会话中保存状态,则可以轻松地将其剥离为数据库查询中的两个衬里。

<?php
    // ... Other parts of the querty
    if ($country = $app->input->getInt('country'))
        $query->where("country = ". (int) $country);
?>
于 2013-10-11T07:33:01.170 回答