【发布时间】:2012-10-04 12:20:19
【问题描述】:
我在 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);。但是由于某种原因,当我在getListQuery() 中调用$this->state->get() 来构建我的带有排序的SQL 查询时
protected function getListQuery()
{
$orderCol = $this->state->get('list.ordering', 'id');
$orderDirn = $this->state->get('list.direction', 'asc');
这个变量碰巧没有定义。
我错过了什么?我认为它以某种方式与正确的用户会话有关,但我没有任何证据。
【问题讨论】: