首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过populateState方法对joomla中的列进行排序

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

Stack Overflow用户
提问于 2012-10-14 15:26:58
回答 2查看 5.5K关注 0票数 5

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

正如我们所看到的,建议覆盖populateState方法并手动获取排序选项。

代码语言:javascript
复制
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中显式地设置这些选项。

代码语言:javascript
复制
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()包含以下内容:

代码语言:javascript
复制
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的代码。因此我假设

代码语言:javascript
复制
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查询时,

代码语言:javascript
复制
protected function getListQuery()
{

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

这个变量碰巧没有定义。

我遗漏了什么?我假设它以某种方式与适当的用户会话有关,但我没有任何证据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-10 15:57:17

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

代码语言:javascript
复制
if (!in_array($value, $this->filter_fields))

如果你查看com_content,你会在模型类的顶部看到这个部分(在你的例子中是models/articles.php):

代码语言:javascript
复制
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类知道“排序”字段在白名单中。显然,将这些字段替换为您希望筛选的字段。

票数 4
EN

Stack Overflow用户

发布于 2012-11-13 22:56:54

Joomla JModelListpopulateState的定义如下

代码语言:javascript
复制
protected function populateState($ordering = null, $direction = null)

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

所以,最低限度你需要的是在你的类中进行插值

代码语言:javascript
复制
protected function populateState($ordering = null, $direction = null) {
    parent::populateState('id', 'ACS');
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12880159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档