CakePHP でリダイレクト後にページ番号を維持するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1055359

質問

項目をリストするインデックス ビューがありますが、リストが長いため、Paginator を使用して項目を 1 つのビューに 50 に制限しています。

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

ここまでは順調ですが、問題点は次のとおりです。

ユーザーがインデックスの N ページにいて、[編集] をクリックして項目を編集した場合、インデックスの N ページにリダイレクトして戻したいと考えています。ページ番号がわかっていれば、URL の末尾に「/page:N」を付ければよいのですが、どうすればページ番号を取得できるのかわかりません。(N には任意のページ番号を指定できますが、特に >=2)

あらゆるアイデアを歓迎いたします。

役に立ちましたか?

解決

ページ番号がリストビュー内の$のparams VARの一部である必要があります。ただ、編集リンクの最後にそれをタックし、そこからそれを扱います。編集ページでは、オプションのページ番号に取る任意のフォーム送信時にそれを保存し、同じページ番号のリストに戻って転送する方法が必要になります。

他のヒント

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

ソースのためにここに参照してください。 http://github.com/jimiyash/cake- pluggables /ブロブ/ a0c3774982c19d02cfdd19a2977eabe046a4b294 /コントローラ/コンポーネント/ 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;
    }

私の理解が正しければ、上記は編集には問題ありませんが、追加には問題ありません。この解決策は、次の両方の状況で機能するはずです。

コントローラーまたは /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}");

...編集用に次のようなものを追加します。

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

/app/app_model.php に次のように入力します。

/**
 * 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;
  }

お役に立てば幸いです!

簡単ない。

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

作品?

ページネータと眺めてます:

<?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']));
?>

コントローラで

public function edit($post_id, $pageToRedirect = false){

    //after all editing its done redirect

    if($pageToRedirect){
    // if record was last in pagination page redirect to previous page
    $pageToRedirect = $pageToRedirect -1;
    return $this->redirect(array('action' => 'index/page:'.$pageToRedirect ));
    }else{
    // else redirect to the same pagination page
    $this->redirect($this->referer());          
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top