I would like to place a link or button in each row of a Views table, and have custom javascript code executed, with field content taken from that particular row as input. The Javascript code already works fine outside of a Views table. How can I add such a custom Javascript link/button? Is this possible ?
2 Answers
If your view is setup to show specific fields and not content:
- Go in advanced > theming information.
- Decide after or before which field your javascript should be.
- Create a template file with the last suggestion name for that field in yourtheme/templates
- In theming information, click on that field. Copy the whole code. (this is the base code for the template you just created.)
- Open your template file, write something outside of php tags and save it.
- Go in configuration > development > performance and clear all caches.
- If the text you typed in your template shows in your view page. It worked. Simply paste your javascript instead.
You just learned how to modify views field templates! Hope that helped!
-
Thanks. I have never themed before, but will try this. But I am also keen to know whether there is some way to do this with a custom field, for example.– PunchyFeb 14, 2012 at 17:18
-
Add a new field, click on it and look inside Rewrite results or Style settings. I could be wrong. Feb 14, 2012 at 17:25
-
1Then I know that the absove answer will resolve your problem :) I can give you more assistance if needed. Feb 14, 2012 at 18:31
-
1In your view page interface, to the right, click on "Advanced". "Theme: Information" is at the bottom of the list. You can also access it with this url :
example.com/admin/structure/views/nojs/display/[my_view]/[type]/analyze-theme
where my_view is the name of the view and type is the display type (page, block, etc.) Feb 14, 2012 at 18:55 -
1It's my pleasure to help you, I don't mind the questions :). You should be able to view all data from the fetched row in the
$row
object. Tryprint_r($row);
. For instance, you can access the node title withprint $row->node_title;
and the nid withprint $row->nid;
. Feb 14, 2012 at 21:18
You can use a Global Text field to create the link, and, since Views wraps everything in all sorts of divs, use jQuery to grab any of the other fields there for your js
-
Could you give a hint how to use jQuery for that? Sorry, I am still on the learning curve. A short pointer will do..– PunchyFeb 14, 2012 at 17:31
-
I thought you said your js worked already? Without knowing what you are trying to do exactly there is no easy hint. But jQuery allows you to easily find things like $(this) and $(this).parent() or $(this).parent().find( ... ) to traverse the DOM to grab any information out of the document you need or want. Feb 14, 2012 at 17:44
-
My js works.. but it doesnt use jQuery.. I will sit down and read more before asking anything else.– PunchyFeb 14, 2012 at 17:46