1

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
  • Are u using content or fields in your view?
    – davidgmar
    Commented Feb 14, 2012 at 17:04
  • I am using fields from Content Types I created.
    – Punchy
    Commented Feb 14, 2012 at 17:06

2 Answers 2

1

If your view is setup to show specific fields and not content:

  1. Go in advanced > theming information.
  2. Decide after or before which field your javascript should be.
  3. Create a template file with the last suggestion name for that field in yourtheme/templates
  4. In theming information, click on that field. Copy the whole code. (this is the base code for the template you just created.)
  5. Open your template file, write something outside of php tags and save it.
  6. Go in configuration > development > performance and clear all caches.
  7. 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!

10
  • 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.
    – Punchy
    Commented Feb 14, 2012 at 17:18
  • Add a new field, click on it and look inside Rewrite results or Style settings. I could be wrong.
    – davidgmar
    Commented Feb 14, 2012 at 17:25
  • 1
    Then I know that the absove answer will resolve your problem :) I can give you more assistance if needed.
    – davidgmar
    Commented Feb 14, 2012 at 18:31
  • 1
    In 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.)
    – davidgmar
    Commented Feb 14, 2012 at 18:55
  • 1
    It'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. Try print_r($row);. For instance, you can access the node title with print $row->node_title; and the nid with print $row->nid;.
    – davidgmar
    Commented Feb 14, 2012 at 21:18
3

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

3
  • Could you give a hint how to use jQuery for that? Sorry, I am still on the learning curve. A short pointer will do..
    – Punchy
    Commented Feb 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.
    – Jimajamma
    Commented 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.
    – Punchy
    Commented Feb 14, 2012 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.