我正在尝试动态更改(如果它被点击)一个普通的表头(这是一个链接)到另一个定义的 CSS 类“th.hilite”。此链接只是对该列进行排序,每次用户对列表进行排序时,标题都应该突出显示。
应该更改相关类的 View ,如下所示:
%table#mytable
%thead
%tr
%th= link_to 'Title', mytable_path(:sort => 'title'), :id => 'title_header'
我的问题很简单:如果单击标题,我如何以及在何处将类动态设置为 %th.hilite?
最佳答案
您可以直接从 View 绑定(bind)它:
%th{:class => ("hilite"if @sort == "title")}= link_to '电影标题'...
但是,您还必须在 Controller 中声明 @sort
实例变量,并将其设置为 title 或 release,根据您正在排序的列。 (即 @sort = params[:sort]
)
关于css - 从 ruby Controller 有条件地设置 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9646815/