我在 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_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');
}
相反,它只是调用 parent 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');
将调用 parent JModelList::populateState()
(所以我不会在模态类中覆盖它)并设置$this->setState('list.ordering', $value);
. 但是由于某种原因,当我调用$this->state->get()
ingetListQuery()
来构建我的 SQL 查询时
protected function getListQuery()
{
$orderCol = $this->state->get('list.ordering', 'id');
$orderDirn = $this->state->get('list.direction', 'asc');
这个变量碰巧没有定义。
我错过了什么?我认为它以某种方式与适当的用户会话有关,但我没有任何证据。