简体   繁体   English

从Rails视图更改Haml表的CSS属性

[英]Changing CSS properties of a Haml table from Rails view

I want to change the table header background color from a view, so that when I click on the table header URL, its background color changes. 我想从视图中更改表格标题的背景颜色,以便在单击表格标题URL时其背景颜色发生变化。

I have this resumed view in Haml: 我在Haml中有以下恢复的视图:

%table#movies
  %thead
   %tr
    %th= link_to "Movie Title", movie_path, :id => "title_header" -#this is the table header

And this the CSS style I want to apply to the table header once “Movie Title” gets clicked on: 这是我想在单击“电影标题”后应用于表格标题的CSS样式:

table#movies th {
  background-color: yellow;
}

I don't know how to bind a click with the table header background color. 我不知道如何将点击与表格标题背景色绑定在一起。 This problem was taken from a online course it's a homework, and we can't use javascript 这个问题来自在线课程,它是一项家庭作业,我们不能使用javascript

This is the solution I've found in some Git repository 这是我在一些Git存储库中找到的解决方案

A helper method is requerid for checking if the %th is the class I need to apply the style 重新请求一个辅助方法来检查%th是否是我需要应用样式的类

def helper_class(field)
 if(params[:sort].to_s == field)
   return 'hilite'
 else
   return nil
 end
end

Previous method is called in the view just in the %th tag 仅在%th标签中的视图中调用了上一个方法

%table#movies
  %thead
    %tr
      %th{:id => 'title_header', :class => helper_class('title') }= link_to "Movie Title", movies_path(:sort=>'title', :ratings =>params[:ratings]), :id => 'title_header'
      %th Rating
      %th{:id => 'release_date_header', :class => helper_class('release') }= link_to "Release Date", movies_path(:sort=>'release',:ratings =>params[:ratings]), :id => 'release_date_header'
      %th More Info
  %tbody
    - @movies.each do |movie|
      %tr
        %td= movie.title 
        %td= movie.rating
        %td= movie.release_date
        %td= link_to "More about #{movie.title}", movie_path(movie)

= link_to 'Add new movie', new_movie_path

With this code the table header which have as class :class => hilite will be changed to the this style that I put on the question. 使用此代码,具有:class => hilite类的表头将更改为我在问题中提出的这种样式。

Thanks to you all for helping me with this, I am new in web development. 感谢大家的帮助,我是Web开发的新手。

Coffeescript: CoffeeScript的:

$('#title_header').click( ()->
  $(this).toggleClass('active')
)

CSS: CSS:

#title_header.active {
  background-color: yellow;
}

Make sure you have only one #title_header – otherwise, change it to a class. 确保只有一个#title_header –否则,将其更改为一类。

Update 更新
If you don't want to use javascript like you say in your comment: 如果您不想像在评论中说的那样使用javascript:

%table#movies
  %thead
   %tr
    %th= link_to "Movie Title", movie_path, :id => "title_header", onclick: "$(this).css('background-color', 'yellow')"

You can use the :visited pseudo selector. 您可以使用:visited伪选择器。

#movies th a:visited {
  background-color: yellow;
}

The styles above are applied for visited links. 上面的样式适用于访问的链接。 However, note that the styles are applied on a , not th . 但是,请注意,样式应用于a ,而不是th

%th{:class => ("hilite" if params[:id]== "title_header")}= link_to 'Movie Title', movies_path(id: 'title_header')

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

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