Yii Framework Forum: Bring User Back To Where He Was In Cgridview - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Bring User Back To Where He Was In Cgridview Rate Topic: -----

#1 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 01 May 2013 - 05:06 AM

In default, auto-generated Yii application, when user click "edit" in CGridView on any other page that first, he or she is returned page to page one, after edit is done.

Is there any nice way to remember somehow, on which page user was and after edit (or any other operation) to bring him or her back to that page?

I can't do this by simple redirect to previous URL, because in default implementation page and sorting operations are done via AJAX update, so previous URL doesn't change and always points to first page.

Has anyone ever deal with that and have any nice solution? Is changing CGridView to use non-AJAX paging and sorting and then re-using previous URL the only solution here?

Finally, the design question -- do you think, this functionality should be implemented? Have you ever met customer complying, that he always returns to first page, while he was editing record on page forty for example?
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,601
  • Joined: 04-March 10
  • Location:UK

Posted 01 May 2013 - 05:55 AM

I tend to cheat by simply opening up the edit page in a new tab, which the user can close when they've finished. A nicer solution might be something like this:

    array(
        'class'=>'CButtonColumn',
        'updateButtonUrl'=>function($data){
            return Yii::app()->createUrl('/edit/path',
                    array('id'=>$data->id, 'returnUrl'=>Yii::app()->request->url));
        },
        ....
    ),


The return URL should remain in the URL bar of the edit page as the user edits the record, so when you redirect, you can use it to send them back to the grid view page. The button in the grid should be updated every time the grid is reloaded, so the return URL should hold the details of the filters and page number that the user had applied.

The only downside is that your edit page will have an ugly URL.

EDIT

To clarify, the redirection will look something like this:
        if ($model->save())
        {
            $this->redirect(
                Yii::app()->request->getQuery('returnUrl', $this->createUrl('/grid/path'));
        }

1

#3 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 01 May 2013 - 08:58 AM

Dear Friend

The following is one example.

I hope this is what you expect.

Model:Wage,
Controller:WageController.

WageController.php.
public function actionAdmin()
{
        $model=new Wage('search');
        $model->unsetAttributes();  // clear any default values         
        if(isset($_GET['Wage']))
        {
                $model->attributes=$_GET['Wage'];
        }
        Yii::app()->user->setState('grid',$_GET);//saving all the get parameters into session.

        $this->render('admin',array(
                        'model'=>$model,
                ));
}


views/wage/view.php.
Just append the following in view.php
if(Yii::app()->user->hasState("grid"))
{       
        $grid=Yii::app()->user->getState('grid');               
        $route=array("wage/admin");
        echo CHtml::link("Return to Grid",array_merge($route,$grid));
        Yii::app()->user->setState("grid",null);
}


Now you will exactly land from where you came.
It retains previous sorting and page as well.
The only drawback here is ugly looking url.

Regards.
1

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 01 May 2013 - 09:03 AM

And, you may consider using 'enableHistory' property of CGridView.
http://www.yiiframew...eHistory-detail
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 01 May 2013 - 10:25 AM

Maybe my xcrudcontroller extension is an option for you. It helps you to return to the same grid page after a view/edit action.
1

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 02 May 2013 - 07:20 AM

@Mike
Inspiring extension. I love the idea of "searchModel".
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 02 May 2013 - 08:01 AM

View Postsoftark, on 02 May 2013 - 07:20 AM, said:

@Mike
Inspiring extension. I love the idea of "searchModel".


Thanks. I've used it many times to build very advanced search pages. It's a really powerful and yet simple pattern.
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 02 May 2013 - 08:10 AM

I agree. It will surely help us think clear and better with the models.
Probably you've already tweaked gii to generate the searchModel skeleton along with the main model.
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 02 May 2013 - 09:54 AM

No, sorry. I almost never use gii. :)
0

#10 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 02 May 2013 - 09:57 AM

Oh, really? So you say that you create your CActiveRecord model file manually?
0

#11 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,601
  • Joined: 04-March 10
  • Location:UK

Posted 02 May 2013 - 10:02 AM

I also never use Gii, but I do use yiic to generate active record models. Using Gii means I have to configure the directories to allow write access to the PHP user, which I'm not a fan of.
0

#12 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 02 May 2013 - 10:09 AM

View Postsoftark, on 02 May 2013 - 09:57 AM, said:

Oh, really? So you say that you create your CActiveRecord model file manually?


Yup. Or i copy an existing one. I'm a fast typer ;).

But we should no longer capture Trejders thread with OT stuff i think.
0

#13 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 02 May 2013 - 10:15 AM

View PostKeith, on 02 May 2013 - 10:02 AM, said:

I also never use Gii, but I do use yiic to generate active record models. Using Gii means I have to configure the directories to allow write access to the PHP user, which I'm not a fan of.

I am a total fan of that on my local development machine. ;)
"Less noise - more signal"
0

#14 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,601
  • Joined: 04-March 10
  • Location:UK

Posted 02 May 2013 - 10:19 AM

View Postjacmoe, on 02 May 2013 - 10:15 AM, said:

I am a total fan of that on my local development machine. ;)


Do you grant write access to the PHP user, make the directory world writable or change the owner of the directory? Just curious in case I do start using Gii in future.
0

#15 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 02 May 2013 - 10:26 AM

Oh, come on, jacmoe. You are an experienced moderator. Please don't continue the OT talking but bring Trejder back to the thread which he has started.

/* I agree with you on Gii. */
0

#16 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 02 May 2013 - 10:39 AM

View PostKeith, on 02 May 2013 - 10:19 AM, said:

Do you grant write access to the PHP user, make the directory world writable or change the owner of the directory? Just curious in case I do start using Gii in future.

I change the group and give group write access. Because I am in the www-data group too, all is well. Then they don't need to be world writable.
Only for my dev machine, of course.

<edit>
I don't use Gii very often either.
</edit>
"Less noise - more signal"
0

#17 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 02 May 2013 - 10:41 AM

Er. sorry Softark :lol:

I agree that enabling HTML5 history would be a good idea, and passing a returnUrl in the data would also work fine I think.
"Less noise - more signal"
0

#18 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 06 May 2013 - 07:15 AM

@everyone: Thanks for a wonderful conversation, even though it is a bit OT at some points. But it is always good to have some noise around own topic! :]

And thanks for all inspiring ideas and solutions. I've been out of computer for some time, but I surely evaluate each idea posted here, once I'm back to the stuff.

Thanks!
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#19 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 06 May 2013 - 03:15 PM

View Postsoftark, on 01 May 2013 - 09:03 AM, said:

And, you may consider using 'enableHistory' property of CGridView.
http://www.yiiframew...eHistory-detail

How this is supposed to be working? What "persist state of grid across page revisits" means?

I've set some non-default sorting and went to page two on my grid, then I called "index" action again (page revisit?) and there I'm back, on first page, with default sorting. How is "state persisted" in this case?

Again. I've set some non-default sorting and went to page three on my another grid (both with enableHistory turned on), then I clicked "edit" button in button column and then saved model, which redirected me back to index page with my grid. Again, I'm back on page one with default sorting.

Is this really working? The only thing I noticed, when using enableHistory, was drop of AJAX-based pagination and sorting. These operations seems to be based on page-refresh, not AJAX, when using enableHistory.

What am I missing here? I'm using Chrome 26 (newest). I think, it supports HTML5 (lol)!
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#20 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 06 May 2013 - 03:48 PM

What you think you are seeing is the grid without Ajax, but you are actually mistaken. :)
If you are using a slow db connection you can see that the spinner is spinning, indicating that it is Ajax/HTML5 which changes the url.
"Less noise - more signal"
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users