【问题标题】:Conditionally add a class if a specific string is found Rails CoffeeScript如果找到特定字符串,则有条件地添加一个类 Rails CoffeeScript
【发布时间】:2015-04-19 21:28:46
【问题描述】:

我有一个简单的 ruby​​ on rails 应用程序,如果它们的状态为“待定”,我正在尝试为表格行着色。下面的代码来自我的 application.js 咖啡脚本文件,但不起作用。

    $(document).ready ->
        if $('td:contains(\'Pending\')')
            @parent('tr').addClass 'warning'
        return

【问题讨论】:

  • 评论已取消。我很抱歉。
  • 好的,所以另一位开发人员建议我在 index.html.erb 文件中的 中添加一个类,然后相应地添加 css 样式。现在我有了代码:<tr class="<%= order_summary.status.underscore %>"> 但我收到了这个错误:undefined method `underscore' for nil:NilClass

标签: css ruby-on-rails coffeescript formatting conditional


【解决方案1】:

我建议输入如下:

$(document).ready ->
  $("td:contains('Pending')").parent().addClass('warning')

编辑

好的,通过以下操作:

addWarningClassToPending = ->
  $("td:contains('Pending')").parent().addClass('warning')

$(document).ready(addWarningClassToPending)

您可以将addWarningClassToPending 设置为另一个事件的回调函数。

【讨论】:

  • 哇,效果很好。唯一的问题是它在页面第一次加载时起作用。如果我导航到一个新对象并返回,除非我刷新页面,否则样式不会显示。不过感谢您的帮助。绝对是正确的方向。
  • 这似乎对我有用:$(document).bind 'DOMSubtreeModified', (e) -> $('td:contains(\'Pending\')').parent().addClass 'warning' return
猜你喜欢
  • 2021-01-21
  • 2015-08-15
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 2023-03-20
相关资源
最近更新 更多