2009-06-28 6 views
1

アイテムを一覧表示するインデックスビューがあり、長いリストなので、Paginatorを使用してアイテムを50個まで表示することができます。CakePHPでリダイレクトした後、どのようにページ番号を維持していますか?

各項目には、入力/検証/などの編集ビューに移動する「編集」リンクがあります。そのフォームが送信されると、インデックスビューに戻ります。

これまでのところは良いが、ここでこするです:ユーザーは、インデックスのページNであると彼らは私が彼らがインデックスのページNに戻ってリダイレクトする、[編集]をクリックして、アイテムを編集する場合

。ページ番号を知っていれば、URLの最後に "/ page:N"を付けることができますが、ページ番号をどのように取得できるかわかりません。 (Nは任意のページ番号ですが、特に> = 2)

アイデアがあります。

+0

いくつかのコードが参考になります。 「編集」リンクにページ番号を入力する必要があります。 – slosd

答えて

2

ページ番号は、リストビューの$ params varの一部である必要があります。編集リンクの最後に貼り付けてそこから処理してください。編集ページでは、オプションのページ番号を取り込み、フォームの提出中に保存し、同じページ番号のリストに戻す方法が必要です。

+0

これはまあまあですが、私はちょうどCakePHPがすでに組み込まれているよりよい方法を望んでいたことに感謝します。 –

2

セッションにページを保存するコンポーネントを作成しました。次に、app_controller.phpで、使用されている特定のモデルのセッション内に何かがあるかどうかを確認し、それをurlに追加します。コンポーネントのコードに興味がある場合は、私にメッセージしてください。ユーザーが編集前にインデックスページのソート順を変更した場合は、注文を保存します。

は、ソースは、ここを参照してください:ここで http://github.com/jimiyash/cake-pluggables/blob/a0c3774982c19d02cfdd19a2977eabe046a4b294/controllers/components/memory.php

は私がやっているものの要旨です。

//controller or component code 
if(!empty($params['named']) && !empty($params['controller']) && $params['action'] == 'admin_index'){ 
    $this->Session->write("Pagem.{$params['controller']}", $params['named']); 
} 

//app_controller.php 
    $redirectNew = ""; 
    if(is_array($redirectTo)){ 
     if(!empty($params['prefix']) && $params['prefix'] == 'admin'){ 
      $redirectNew .= '/admin'; 
     } 
     if(!empty($params['controller'])){ 
      $redirectNew .= "/" . $params['controller']; 
     } 
     if(!empty($redirectTo['action'])){ 
      $redirectNew .= "/" . $redirectTo['action']; 
     } 
    } else { 
     $redirectNew = $redirectTo; 
    } 

    $controller = $params['controller']; 
    if($this->Session->check("Pagem.$controller")){ 
     $settings = $this->Session->read("Pagem.$controller"); 
     $append = array(); 
     foreach($settings as $key=>$value){ 
      $append[] = "$key:$value"; 
     } 
     return $redirectNew . "/" . join("/", $append); 
    } else { 
     return $redirectNew; 
    } 
+0

ジェイソン私はこれについて言及していませんでしたが、私はこのセッションを使用したくないでしょう。私はまだそれを調べます。 –

+1

私はソリューションが気に入っています。なぜならこれは非常に一般的な問題であり、ユーザーはページネーションが永続的であることを期待しているからです。 – jimiyash

2

私が正しく理解していれば、上記は編集には問題ありませんが、追加することはできません。このソリューションは、両方の状況のた​​めに働く必要があります:あなたのコントローラや、あなたの/app/app_controller.phpで

、追加するためにこのようなものに入れ:

$insertID = $this->{$this->modelClass}->getLastInsertID(); 
$page = $this->{$this->modelClass}->getPageNumber($insertID, $this->paginate['limit']); 
$this->redirect("/admin/{$controllerName}/index/page:{$page}"); 

を...と編集のためにこのような何か:

この中に入れて、あなたの/app/app_model.phpで
$page = $this->{$this->modelClass}->getPageNumber($id, $this->paginate['limit']); 
$this->redirect("/admin/{$controllerName}/index/page:{$page}"); 

、:

/** 
* Work out which page a record is on, so the user can be redirected to 
* the correct page. (Not necessarily the page she came from, as this 
* could be a new record.) 
*/ 

    function getPageNumber($id, $rowsPerPage) { 
    $result = $this->find('list'); // id => name 
    $resultIDs = array_keys($result); // position - 1 => id 
    $resultPositions = array_flip($resultIDs); // id => position - 1 
    $position = $resultPositions[$id] + 1; // Find the row number of the record 
    $page = ceil($position/$rowsPerPage); // Find the page of that row number 
    return $page; 
    } 

希望に役立ちます!

+0

getPageNumberがパフォーマンスに問題がある可能性があります。大規模なシステムでは使用しません。 – Martin

1

は、単純な

$this->redirect($this->referer()); 

作品をしていますか?ページネータとビューで

+0

:(ちょうどそれを試した。 – mikermcneil

0

<?php 
if ($this->Paginator->hasPage(null, 2)) { 
$pag_Start = $this->Paginator->counter('{:start}'); 
$pag_End = $this->Paginator->counter('{:end}'); 
if($pag_Start == $pag_End){ 
$pageToRedirect = $this->Paginator->current('Posts'); 
}else{ 
$pageToRedirect= ''; 
}}?> 

は次に、ページにコントローラで

<?php 
echo $this->Form->postLink(
'Edit', 
array('action' => 'edit', $subscription['Post']['id'])); 
?> 

を編集するためにリンクされています

​​
関連する問題