Links in CGridView

I use the following code in a number of grids to create a link for the entire row.


'selectionChanged'=>'function(id){ location.href = 

   "'.$this->createUrl('donation/view/id').'/"+$.fn.yiiGridView.getSelection(id);}',

I have a grid where I would like the appended link of donation/view/id to come through an id in a relation instead. Is this possible using this format (keeping the row as a link)?

Notice that the value of an PHP expression is executed at the time of rendering the page (HTML)… so if you use the same value for all the rows it will work…

But if you want to use an expression that would change from row to row like an attribute value of the current row… that would not work…

For this you need to have the value you need in the HTML file (in the grid)… so that you can get that value with jQuery…

mdomba, I thought I understood the separation you mentioned between HTML and jQuery but then reading the third statement it seems confusing.

So right now it will go to DonationId (jQuery), if I want it to go to $model->organization->OrganizationId instead can that happen if I have the OrganizationId in my grid then?

It’s very simple… you can set selectionChanged with a javascript function that will be called after the row selection is changed…

So at that time when this function is executed… you are not in PHP anymore… you are on the client… .and you cannot use a PHP expression like $model->relation->name… but you can use jQuery to read any value you need from the data that is already on the page…

You have two options here

  • first is to have the data on the page and read it with jQuery…

  • second is to call the server to get the data you need (ajax)

“It’s very simple…”

I always laugh when you start off like this. Sure it is simple for you but obviously not for me.

Well in this respect I would need to do an ajax call to get what I need. With that being said I do not understand how to incorporate (or what to incorporate for that matter) into what I have.

Would appreciate help.

When you clearly understand the separation of server side content (PHP) and client side content (HTML/jQuery) … then it’s simple :D

So you would like to make an ajax request on selectionChanged… first of all you need to learn (if you already don’t know) how to make an ajax call from jQuery - http://api.jquery.com/jQuery.ajax/… and you need to have an action that will return the data you need - this action will be called with ajax…

To make the ajax call you can use something like




'selectionChanged'=>'function(id){ $.ajax({

      url: "...",

      type: "post",

      data: {...}

      dataType: "json",

      success: function(data){}

   });

};'



This is just a template… for the url parameter you set the URL of the action that will return the data you need…

data parameters are the parameters you pass to the action (like ID of the selected row)…

success is the callback function that will be executed on ajax success and this function can access the results from the server…

If you need more help… please explain a bit more what exactly you need to do…

Thanks, I am assuming you mean this link or better yet here? I need to do some more reading on this material and then go from there. Thanks for pushing me along and the extra info.