简体   繁体   English

Drupal 7 - 如何将Javascript添加到视图中

[英]Drupal 7 - How to add Javascript to a View

Basically what I'm trying to do is add a line of javascript code to each row iteration in a view. 基本上我要做的是在视图中为每行迭代添加一行javascript代码。

In Drupal 6 this used to be accomplished with Views Custom Field which has now been integrated into Drupal 7. 在Drupal 6中,这曾经使用Views Custom Field完成,现在已经集成到Drupal 7中。

When I try to use the "Global: Custom text" field it strips out my script tags. 当我尝试使用“全局:自定义文本”字段时,它会删除我的脚本标记。

Any ideas? 有任何想法吗?

Please note : I don't want to use drupal_add_js as this needs to be dynamically inserted and I'd prefer to not use it inline ( messy ). 请注意 :我不想使用drupal_add_js,因为这需要动态插入,我宁愿不使用内联( 凌乱 )。 A unique tooltip will popup based on the JS provided and I need the actual row ID. 将根据提供的JS弹出一个唯一的工具提示,我需要实际的行ID。

Thanks in Advance!! 提前致谢!!

Responded on Twitter, but for the benefit of others who run into the same situation (and for the sake of clarifying) I thought I'd repost here. 在推特上做出回应,但为了其他遇到同样情况(为了澄清)的人的利益,我想我会在这里转发。

Generally, I wouldn't recommend views_php - there are numerous downsides to enabling/using PHP filters that have already been discussed at length , and it adds the bloat of another module when you really don't need it. 一般来说,我不推荐使用views_php - 启用/使用已经详细讨论过的 PHP过滤器有许多缺点,当你真的不需要它时,它会增加另一个模块的膨胀。 You can still use drupal_add_js() in a preprocess function in this case and keep things plenty dynamic. 在这种情况下,您仍然可以在预处理函数中使用drupal_add_js()并保持动态。

In short, you have a list of elements that you need to bind the same event to. 简而言之,您有一个元素列表,您需要将相同的事件绑定到。 Event delegation immediately comes to mind as a possible solution. 事件授权立即成为可能的解决方案。 Instead of n event handlers, you now have one for your parent container, which will make your page more responsive. 您现在拥有一个父容器,而不是n个事件处理程序,这将使您的页面更具响应性。 You don't need jQuery for this, but if you prefer look into using .delegate() 你不需要jQuery,但如果你更喜欢使用.delegate()

As for how to get the ID from this external JS file, simple: make sure it's in the source markup you're grabbing. 至于如何从这个外部JS文件中获取ID,很简单:确保它在您抓取的源标记中。 Assuming you're looking for the row ID, you can typically find it in the class "views-row-N" for each row. 假设您正在查找 ID,通常可以在每行的“views-row-N”类中找到它。 Otherwise, you'll want to add the class-id to the template by some other means. 否则,您将希望通过其他方式将class-id添加到模板中。 Inside the event handler for the target element (your row, an element matching .views-row within your parent element), parse the ID from the class you intend to use and execute the rest of your script based on the value you find from there. 在目标元素的事件处理程序内(您的行,在您的父元素中匹配.views-row的元素),从您打算使用的类中解析ID,并根据您从那里找到的值执行脚本的其余部分。

This approach has a number of benefits: it's one less module, it's one less source of security headaches, your JS is all in one place, you're binding fewer event handlers, your markup is lighter and easier to read, and event delegation means this technique will still work even if elements are added to the DOM dynamically later for some reason. 这种方法有很多好处:它只减少了一个模块,它减少了一个安全问题的来源,你的JS在一个地方,你绑定的事件处理程序更少,你的标记更轻松,更容易阅读,事件委托意味着即使元素由于某种原因动态地添加到DOM中,这种技术仍然可以工作。 As for the specifics, it would help to know exactly what you intend to do with the popup. 至于细节,它将有助于准确知道你打算用弹出窗口做什么。 Is it making an AJAX call to another node? 它是否正在对另一个节点进行AJAX调用? Is it doing something based on the position in the list? 它是根据列表中的位置做某事吗? I'd be happy to clarify my answer from there. 我很乐意从那里澄清我的答案。

You're probably just looking for one of these. 你可能只是在寻找其中一个。 It may be a bit overkill but it will get the job done: http://drupal.org/project/views_php 这可能有点矫枉过正,但它会完成工作: http//drupal.org/project/views_php

Install the module and you will have the options for a global: php field. 安装模块,您将拥有global:php字段的选项。 You can post just javascript into the field output and it will not get filtered out. 您只需将javascript发布到字段输出中,它就不会被过滤掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM