2012-11-06 43 views
0

我希望有一個文本框,2個下拉菜單和一個單選按鈕(在cakephp中)的搜索表單。但是這是用分頁來完成的。搜索和分頁工作單獨罰款,但不能在一起。當我選擇過濾條件並單擊搜索按鈕時,結果在第一頁顯示得很好,但是如果我點擊任何分頁鏈接,過濾條件將丟失,並且分頁數據不顯示任何過濾器。我如何解決這個問題? 控制器代碼:在cakephp中使用搜索過濾器的分頁奇怪行爲

private function _index() { 
    $this->genre_list(); 
    $this->language_list(); 
    $conditions = array(); 
    debug($this->postConditions($this->data)); die; 
    if (!empty($this->request->data['Artist'])) { 
     foreach ($this->request->data['Artist'] as $name => $record) { 
      if (isset($record) && !empty($record)) { 
       if ($name == 'name') { 
        $conditions = array(
         $this->modelClass->User. 'User.name LIKE' => '%' . $record . '%', 
        ); 
       } else { 
        $conditions[$this->modelClass . '.' . $name] = $record; 
       } 
      } 
     } 
    }; 

    $this->paginate = array(
     'contain' => array (
      'User' => array(

       'City',// => array('name'), 
       'State',// => array('name'), 
       'Country',// => array('name'), 
      ), 
      'Genre',// => array('name'), 
      'Language',// => array('language'), 
     ), 
     'limit' => 3, 
     'conditions' => $conditions 
    ); 


    $data = $this->paginate($this->modelClass); 
    //~ debug($data); 
    $this->set(compact('data')); 
} 

觀點:

<?php 
echo $this->Form->create('Search', array(
    'type' => 'file', 
    'inputDefaults' => array(
     'format' => array(
      'label', 'between', 'input', 'error', 'after' 
     ) 
    ), 
    'class' => 'form-horizontal' 
)); 
echo $this->Form->input('Artist.name', array(
    'div' => 'control-group', 
    'label' => array(
     'class' => 'control-label', 
     'text' => 'Artist Name' 
    ), 
    'between' => '<div class="controls">', 
    'after' => '</div>', 
    'placeholder' => 'Name', 
    'error' => array(
     'attributes' => array(
      'wrap' => 'div', 
      'style' => 'color:#B94A48' 
     ) 
    ) 
)); 
echo $this->Form->input('Artist.genre_id', array(
    'div' => 'control-group', 
    'empty' => 'All', 
    'label' => array(
     'class' => 'control-label', 
     'text' => 'Genre' 
    ), 
    'between' => '<div class="controls">', 
    'after' => '</div>', 
    'error' => array(
     'attributes' => array(
      'wrap' => 'div', 
      'style' => 'color:#B94A48' 
     ) 
    ) 
)); 

echo $this->Form->input('Artist.language_id', array(
    'div' => 'control-group', 
    'empty' => 'All', 
    'label' => array(
     'class' => 'control-label', 
     'text' => 'Select Lanuage' 
    ), 
    'between' => '<div class="controls">', 
    'after' => '</div>', 
    'error' => array(
     'attributes' => array(
      'wrap' => 'div', 
      'style' => 'color:#B94A48' 


    ) 
     ) 
    )); 
?> 
<?php echo $this->element('pagination'); 

與會話編輯的代碼

private function _index() { 
    $this->genre_list(); 
    $this->language_list(); 
    $conditions = array(); 

    //~ foreach($this->request->params['named'] as $key => $record) { 
     //~ debug($this->request->params['named']); 
      //~ $this->request->data['Search'][$key] = $record; 
    //~ } 

    if (!empty($this->request->params['named']['page'])) { 
    // use session data for conditions 
    $conditions = (array)$this->Session->read('_indexConditions'); 
    } else { 
    // delete session data 
    $this->Session->delete('_indexConditions'); 
    } 

    $conditions = array(); 
    if (!empty($this->request->data)) { 
    // new search! use the data to make conditions, 
    // like you did, and save the conditions 
    //~ if (!empty($this->request->data['Artist'])) { 
     foreach ($this->request->data['Search'] as $name => $record) { 
      if (isset($record) && !empty($record)) { 
       if ($name == 'name') { 
        $conditions = array(
         $this->modelClass->User. 'User.name LIKE' => '%' . $record . '%', 
        ); 
       } else { 
        $conditions[$this->modelClass . '.' . $name] = $record; 
       } 
      } 
     } 
    //~ } 
    $this->Session->write('_indexConditions', $conditions); 
    } 

    $this->paginate = array(
     'contain' => array (
      'User' => array(

       'City', 
       'State', 
       'Country', 
      ), 
      'Genre', 
      'Language', 
     ), 

     'limit' => 3, 
     'conditions' => $conditions 
    ); 


    $data = $this->paginate('Artist'); 
    $this->set(compact('data')); 
} 

回答

0

得到了解決,而不在控制器中使用會話

if (!empty($this->request->data) || $this->request->params['named']) { 
     foreach($this->request->params['named'] as $key => $record) { 
      if($key!= 'page') 
       $this->request->data['Search'][$key] = $record; 
     } 
     foreach ($this->request->data['Search'] as $name => $record) { 
      if (isset($record) && !empty($record)) { 
       $this->request->params['named'][$name] = $record; 
       if ($name == 'name') { 
        $conditions[$this->{$this->modelClass}->User. 'User.name LIKE'] = '%' . $record . '%'; 
       } else { 
        $conditions[$this->modelClass . '.' . $name] = $record; 
       } 
      } 
     } 
    } 

,並考慮提供控制器和行動明確:

echo $this->Form->create('Search', array(
    'type' => 'file', 
    'inputDefaults' => array(
     'format' => array(
      'label', 'between', 'input', 'error', 'after' 
     ) 
    ), 
    'url' => array(
     'controller' => 'artists', 
     'action' => 'index', 
    ), 
    'class' => 'form-horizontal' 
)); 

解決了這個問題。

0

答案很簡單:保存在會話或cookie,並使用搜索條件這些條件如果新的沒有發送。

爲了簡單起見,我省略了大部分代碼。像這樣的東西應該工作。

private function _index() { 
    // check if this is a pagination request without data 
    if (!empty($this->request->params['named']['page']) { 
    // use session data for conditions 
    $conditions = (array)$this->Session->read('_indexConditions'); 
    } else { 
    // delete session data 
    $this->Session->delete('_indexConditions'); 
    } 

    $conditions = array(); 
    if (!empty($this->request->data)) { 
    // new search! use the data to make conditions, 
    // like you did, and save the conditions 
    $this->Session->write('_indexConditions', $conditions); 
    } 

    $this->paginate = array(
    'conditions' => $conditions 
); 
    $data = $this->paginate($this->modelClass); 
    $this->set(compact('data')); 
} 

它應該被包裝在一個組件中。在我的一個項目中,我有一個自動執行此操作的組件,並將控制器數據替換爲更自動化的過程。

+0

嘿我試過這個,但它仍然無法正常工作,我已編輯我的文章,包括修改的代碼。我在哪裏錯了? – z22

0

在2.x中,您可以將表單設置爲使用GET而不是POST。

更改您的形式從文件類型爲GET(順便說一句,你爲什麼用文件?)

echo $this->Form->create('Search', array(
'type' => 'get', 

當然的,而不是使用數據陣列從請求

$this->request->data['Search']['name'] 

而且您將使用這樣的查詢數組:

if (!empty($this->request->query['name'])) {