Question

I have an index view that lists items, and it's a long list so I use Paginator to limit items to 50-to-a-view.

Each item has an "edit" link that goes to an edit view with inputs/validations/etc. When that form is submitted, I redirect the use back to the index view.

So far so good, but here's the rub:

If a user is on page N of the index and they click edit and edit an item, I want them to be redirected back to page N of the index. If I knew the page number, I could just stick "/page:N" on to end of the URL, but I have no idea how I can get the page number. (N could be any page number, but especially >=2)

Any ideas would be appreciated.

Was it helpful?

Solution

The page number should be part of the $params var in the list view. Just tack it onto the end of the edit link and handle it from there. On the edit page you will need a way to take in the optional page number, store it during any form submission, and forward back to the list with the same page number.

OTHER TIPS

I created a component that saves the page in the session. Then in the app_controller.php, I check to see if there is anything in the session for the particular model being used and then add that to the url. If you are interested in the code for the component, message me. I also store the order, if the user changed the sort order in the index page before editing.

See here for the source: http://github.com/jimiyash/cake-pluggables/blob/a0c3774982c19d02cfdd19a2977eabe046a4b294/controllers/components/memory.php

Here is the gist of what I am doing.

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

If I understand correctly, the above is fine for editing, but not for adding. This solution should work for both situations:

In your controllers or your /app/app_controller.php, put in something like this for adding:

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

...and something like this for editing:

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

In your /app/app_model.php, put in this:

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

Hope that helps!

Does a simple

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

works?

In view with paginator:

<?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= '';
}}?>

Then link to edit page

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

In controller:

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());          
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top