gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:20:48 25 4
gpt4 key购买 nike

我正在 Joomla 后端对表格列进行排序。我根据this tutorial调整设置.

正如我们所见,建议覆盖 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_content没有在模型文件中明确设置这些选项 administrator/components/com_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_content .因此我假设
class CompViewData extends JView
{

function display($tpl = null)
{
$this->state = $this->get('State');

将调用父 JModelList::populateState() (所以我没有在模态类中覆盖它)并设置 $this->setState('list.ordering', $value); .但是出于某种原因,当我调用 $this->state->get() 时在 getListQuery()使用排序构建我的 SQL 查询
protected function getListQuery()
{

$orderCol = $this->state->get('list.ordering', 'id');
$orderDirn = $this->state->get('list.direction', 'asc');

这个变量碰巧没有定义。

我错过了什么?我认为它与正确的用户 session 有某种联系,但我没有任何证据。

最佳答案

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

if (!in_array($value, $this->filter_fields))

如果您查看 com_content,您将在模型类的顶部看到此部分(在您的示例中为 models/articles.php):
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);
}

您需要包含此部分,以便 ModelList 类知道“排序”字段在白名单中。显然,用您希望过滤的字段替换这些字段。

关于sorting - 通过 populateState 方法对 joomla 中的列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12880159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com