I need to build CGridView that will do exactly the same as now happens, when user clicks currently on View in CButtonColumn, no matter where within row user clicks.
Is there any other way to do it except changing each CDataColumn to CLinkColumn? Cause this way I can only reacts on clicking on links / values / cell contents and ideal situation for me would be to catch HTML’s td onClick and make it work just as clicking on View now works.
Let me just remind for others, that in current implementation of Yii you can’t set htmlOptions per each table row and currently the only option to achieve, what you want to do is to use CSS file and add into it line:
.grid-view table.items tr td {cursor: pointer}
I found this solution in other disscussion and pasting it here, so others wouldn’t have to search for it.
I added a particular class to this table so that it won’t affect tables in ‘admin’ view which uses grid-view as well. And in my css, I only wanted the <tbody> to have the pointer cursor. Giving the header the pointer cursor might give the user the idea that the table can be sorted by clicking any part of the header, and might disappoint them that it’s not.
Your solution is using CGridView’s internal jQuery function for obtaining ID of clicked row. After slight changes to your code I’m getting addresses I need in one of my modules, for example:
Where value of first and only parameter (79011714729 in this example) is taken from particular cell in clicked row. This feature I need in another module of my application.
I’ve tried solution mdomba once gave me for generating in CGridView’s CButtonColumn delete button click a delete confirmation including some value from some of cell in the row, where user clicked on delete button, i.e.:
But either I’m missing something or this does not works here (however works brilliantly in deleteConfirmation - tested and confirmed). It returns empty string, not current cell value (however trimming .text() off displays “[object] [object[” alert message, therefore object exists I only can’t access its properties somehow).
But, your solution requires to either add this class to any CGridView, where you want to have hand-cursor, while my solution (slightly updated after you - remaining on original class name):
.grid-view tbody:hover {cursor: pointer}
Will add you hand-cursor to every CGridView, as every CGridView is build by default upon .grid-view class.
But maybe this was you intention to add hand-cursor only to a specific one CGridView, then your solution is very good.
Right, I want to control the tables which will only have this feature. I particularly does not want the ‘admin’ view file to be viewable with the click of the row, since they also use ‘grid-view’ class.
@Trejder… note that my solution was specific to the button column… if you want to use it in another column you need to remove one parent() call… or better said… you need to calculate how many parents() you need to go up to get to the row object…
Yes, you’re right I missed that it this situation I’m clicking in the column, not on the button. Therefore - as you mentioned - I need to remove one .parent(). Which actually doesn’t explain why code produced this way:
to display contents of clicked cell. But this results in no alert and "elems is undefined in /assets/739c509d/jquery.js, line 3422" error drops into JS console after clicking anything in CGridView.
For the very same purpose you can also use id of CGridView, which is declared most of the time (and as every ID, always points only to current, the only one), as it is used in gridview’s update JavaScript code. There is no need to add additional class name for distinguishing grid view.
I use your method, and like it, but noticed, that it works only in first page, but when I click on any other page, redirection is not working. When I return to page it worked for the first time, then it also fails. Maybe there are some jquery conflicts, because CGridView is using jquery pagination?
Is there an easy way to extend your custom JS code, so it would NOT react on click anywhere in CGridView’s row if there would be only one row saying “No results”? Just by an accident I noticed that click reacts also if CGridView contains no data and this produces unpredictable results.