cakePHP Combobox在分页时失去选择

时间:2013-03-11 18:59:31

标签: cakephp combobox pagination

这是我用cakePHP创建的第一页,我有以下问题/场景:

我正在使用不同的模型创建一个高分页面,因为给定的数据库提供了不同表格中的信息。我正在使用分页和带有两个组合框的表单来选择特殊属性。设置过滤条件我正在使用beforeFilter() - 函数并动态设置条件(和连接)。所有条件也存储在会话中,因为它们保留了分页。这一切都很好。

我现在的问题如下:当我提交表单时,结果是正确的,并且使用我刚刚选择的选项预选了组合框。当我现在切换到page2 / 3 / ...时,组合框松散,选择,并且是空的选项。

如何通过分页保留选择?

我的控制器beforeFilter() - 函数

// Highscorefilter
public function beforeFilter()
{
  parent::beforeFilter();

  // Dropdown für Rassen
  $this->set('racelist', $this->Race->getRaceList());

  // Dropdown für Galaxien
  $this->set('galaxylist', $this->Galaxy->getGalaxyList());


  // Initialisierung
  $filter_conditions = array();
  $joins = array();
  $filter = FALSE;

  if($this->request->is('post'))
  {

    // Um die Filter richtig zu erkennen brauchen wir den Modelnamen :/
    switch($this->action)
    {
      case "imperium":
        $model = "HighscoreImperiumUser";
        break;
      case "quest":
        $model = "HighscoreQuestUser";
        break;
      case "war":
        $model = "HighscoreWarUser";
        break;
      case "trade":
        $model = "HighscoreTradeUser";
        break;        
      case "overall":
      default:
        $model = "HighscoreOverallUser";
        break;
    }    

    // Filter auslesen und setzen

    $data = $this->request->data;

    if(isset($data[$model]['race_id']) && !empty($data[$model]['race_id']))
    {
      // WHERE-Bedingung race_id setzen
      $tmpstr = sprintf("%s.race_id", $model);
      $filter_conditions[$tmpstr] = $data[$model]['race_id'];

      // Joins setzten wir in diesem Fall auf ein leeres array. Das wird ggf. nachher überschrieben
      $joins = array();
    }

    if(isset($data[$model]['gala']) && !empty($data[$model]['gala']))
    {
      // WHERE-Bedingung gala setzen
      $tmpstr = sprintf("ug.gala", $model);
      $filter_conditions[$tmpstr] = $data[$model]['gala'];        

      // WHERE-Bedingung für Join bauen und Join konstruieren
      $tmpstr = sprintf("ug.userid = %s.id", $model);
      $join = array( 'alias' => 'ug',
                     'table' => 'user_gala',
                     'type' => 'INNER',
                     'conditions' => $tmpstr);
      $joins[] = $join;

    }

    // Filterinformationen für Seitenwechsel in Session schreiben
    $this->Session->delete('_indexConditions');
    $this->Session->delete('_indexJoins');
    $this->Session->write('_indexConditions', $filter_conditions);
    $this->Session->write('_indexJoins', $joins);

    $filter = TRUE;

  }
  else if(!empty($this->request->params['named']['page']))
  { // Filterinformationen aus der Session holen
    $filter_conditions = (array)$this->Session->read('_indexConditions');
    $joins = (array)$this->Session->read('_indexJoins');

    $filter = TRUE;
  }

  if($filter)
  {      
    // set filter conditions of each pagination model
    $this->paginate = Hash::insert($this->paginate, '{s}.conditions', $filter_conditions);
    $this->paginate = Hash::insert($this->paginate, '{s}.joins', $joins);           
  }
}

其中一个“真正的”控制器功能(它们都是straith-forwand,只是使用不同的模型)

// Overall (Default) Highscore
public function overall()
{
  // Paginate der Highscore
  $highscoreusers = $this->paginate('HighscoreOverallUser');
  $this->set('highscoreusers', $highscoreusers);

  // Variablen für View setzen
  $this->set('model', 'HighscoreOverallUser');
  $this->set('title', __d('highscore', 'Overall score'));

  // Index-View benutzen
  $this->render('index');
}    

我的观点表格

  = $this->Form->create($model, array('type' => 'post', 'url' => array('action' => $this->request['action'], 'controller' => $this->request['controller'])))
  %table
    %tr
      %td{:width => "200px"}
        = $this->Form->label('race_id', __d('highscore', 'Only show race:'))
      %td{:width => "100px"}
        = $this->Form->select('race_id', $racelist, array('legend' => false, 'class' => 'select', 'empty' => __d('highscore', 'All races') ))
      %td{:width => "200px"}
        = $this->Form->label('gala', __d('highscore', 'Only show players in galaxy:'))
      %td{:width => "100px"}
        = $this->Form->select('gala', $galaxylist, array('legend' => false, 'class' => 'select', 'empty' => __d('highscore', 'All galaxies') ))

  = $this->Form->end(__d('highscore', 'Activate filter'))

1 个答案:

答案 0 :(得分:0)

问题是在用分页器切换页面后,没有设置cakePHP的数据值(例如“data [HighscoreOverallUser] [race_id]”)。

所以必须手动设置它们。以下代码对我有用。当然你必须小心不要覆盖设置数据,但据我现在可以说,在使用分页器之后,数据总是没有设置

if(!$this->data->HighscoreOverallUser)
{
  $data['HighscoreOverallUser']['race_id'] = $filter_conditions['HighscoreOverallUser.race_id'];
  $this->data = $data;
}