2014-11-02 62 views
0

這裏ajax搜索工作正常,但問題是相同的數據顯示兩次,因爲我給了分頁限制2.如果我使它3,同樣的結果顯示3倍。CakePHP Ajax搜索:顯示相同的數據兩次分頁

這裏是我的index.ctp

$("#search").keyup(function() { 

    var value=$('#search').val(); 

     $.get("<?php echo Router::url(array('controller'=>'userTypes','action'=>'search'));?>",{search:value},function(data){ 
     $('.search_data').html(data); 
    }); 
}) 

這裏是我的搜索行動

public function search() 
    { 
     $search=$_GET['search']; 
     $request=$this->UserType->find('all', array(
     'conditions' => array('UserType.name LIKE' => "%$search%") 
     )); 
     $this->set('usertype',$request); 
    } 

這裏是search.ctp

<?php foreach($usertype as $usertypes) { ?> 
    <tr> 
      <td><?php echo $usertypes['UserType']['id']; ?></td> 
      <td><?php echo $usertypes['UserType']['name']; ?></td> 
      <td><?php echo $usertypes['UserType']['description']; ?></td> 

    </tr> 
<?php } ?> 

分頁限制在AppController的

parent::beforeFilter(); 
      $this->Paginator->settings = array(
       'limit'=>2 
     ); 

結果正顯示出我這個樣子

enter image description here

五月人幫助我解決呢?

回答

1

如果你想使用分頁,你的搜索方法應該是這樣的:

public function search() 
{ 
    if(isset($this->request->query['search'])){ 
     $search = $this->request->query['search']; 
    }else{ 
     $search = ''; 
    ) 
    $this->Paginator->settings = array(
     'conditions' => array('UserType.name LIKE' => "%$search%"), 
     'limit' => 2 
    ); 
    )); 
    $this->set('usertype',$this->Paginator->paginate()); 
}