Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 通过populateState方法对joomla中的列进行排序_Sorting_Joomla_Getstate - Fatal编程技术网

Sorting 通过populateState方法对joomla中的列进行排序

Sorting 通过populateState方法对joomla中的列进行排序,sorting,joomla,getstate,Sorting,Joomla,Getstate,我正在Joomla后端对表列进行排序。我根据需要调整设置 正如我们所见,建议重写populateState方法并手动获取排序选项 public function populateState() { $filter_order = JRequest::getCmd('filter_order'); $filter_order_Dir = JRequest::getCmd('filter_order_Dir'); $this->setState('filter_ord

我正在Joomla后端对表列进行排序。我根据需要调整设置

正如我们所见,建议重写
populateState
方法并手动获取排序选项

public function populateState() {
    $filter_order = JRequest::getCmd('filter_order');
    $filter_order_Dir = JRequest::getCmd('filter_order_Dir');

    $this->setState('filter_order', $filter_order);
    $this->setState('filter_order_Dir', $filter_order_Dir);
}
但是我注意到本机组件
com\u content
没有在模型文件
administrator/components/com\u content/models/articles.php
中明确设置这些选项

protected function populateState($ordering = null, $direction = null)
{
    // Initialise variables.
    $app = JFactory::getApplication();
    $session = JFactory::getSession();

............................................
............................................
............................................

    // List state information.
    parent::populateState('a.title', 'asc');
} 
相反,它只调用父级
populateState
。事实上,
JModelList::populateState()
包括:

protected function populateState($ordering = null, $direction = null)
{
    // If the context is set, assume that stateful lists are used.
    if ($this->context) {
        $app = JFactory::getApplication();

.....................................
.....................................
.....................................

        $value = $app->getUserStateFromRequest($this->context.'.ordercol', 'filter_order', $ordering);
        if (!in_array($value, $this->filter_fields)) {
            $value = $ordering;
            $app->setUserState($this->context.'.ordercol', $value);
        }
        $this->setState('list.ordering', $value);

        // Check if the ordering direction is valid, otherwise use the incoming value.
        $value = $app->getUserStateFromRequest($this->context.'.orderdirn', 'filter_order_Dir', $direction);
        if (!in_array(strtoupper($value), array('ASC', 'DESC', ''))) {
            $value = $direction;
            $app->setUserState($this->context.'.orderdirn', $value);
        }
        $this->setState('list.direction', $value);
    }
    else {
        $this->setState('list.start', 0);
        $this->state->set('list.limit', 0);
    }
}
所以我试着模仿本地
com\u内容的代码。因此,我认为

class CompViewData extends JView
{

function display($tpl = null)
{
    $this->state = $this->get('State');
将调用父级
JModelList::populateState()
(因此我不会在模态类中重写它)并设置
$this->setState('list.ordering',$value)。但是由于某种原因,当我在
getListQuery()
中调用
$this->state->get()
来构建带有顺序的SQL查询时

protected function getListQuery()
{

    $orderCol   = $this->state->get('list.ordering', 'id');
    $orderDirn  = $this->state->get('list.direction', 'asc');
这些变量碰巧没有定义


我错过了什么?我假设它与适当的用户会话有某种联系,但我没有任何证据。

Joomla
jModelist
这样定义
populateState

protected function populateState($ordering = null, $direction = null)
这意味着,如果类中没有
populateState
重写,则将调用它,但它不会得到任何值。如果要使用排序,最低要求是设置默认值。如果您根本不打算使用排序,可以从类中完全删除此方法

所以,你们最起码需要在你们的类中插值

protected function populateState($ordering = null, $direction = null) {
    parent::populateState('id', 'ACS');
}

否则您将无法在
$state->get()
$this->state->get()
中获取任何内容,除非您单击ordering列。然后父类的
populateState
将从请求中获取变量。

在遇到同样的问题后,我发现,正如您所说,超类populateState()确实定义了行为。但是,它也会进行检查,以确保您的字段在“白名单”中

如果您查看com_内容,您将在模型类的顶部看到这一部分(在您的例子中是models/articles.php):


您需要包括此部分,以便ModelList类知道“ordering”字段在白名单中。显然,用您希望筛选的字段替换这些字段。

这些
$value=$app->getUserStateFromRequest($this->context.'.ordercol',filter_order',$ordering)$value=$app->getUserStateFromRequest($this->context.'.orderdirn','filter\u order\u Dir',$direction)从状态变量获取数据,尽管$ordering和$direction为null?我没有对白名单部分给予足够的关注。谢谢你指出!我也是!谢谢@moo2u2
if (!in_array($value, $this->filter_fields))
public function __construct($config = array())
{
    if (empty($config['filter_fields']))
    {
        $config['filter_fields'] = array(
            'id', 'a.id',
            'title', 'a.title',
             //...(more fields here)
            'publish_up', 'a.publish_up',
            'publish_down', 'a.publish_down',
        );

        $app = JFactory::getApplication();
        $assoc = isset($app->item_associations) ? $app->item_associations : 0;
        if ($assoc)
        {
            $config['filter_fields'][] = 'association';
        }
    }

    parent::__construct($config);
}