简体   繁体   English

使用jQuery根据单元格值更改表行颜色

[英]Change Table Row Color Based On Cell Value With jQuery

I don't have much experience with jQuery, so I'm not sure how close I am to this. 我在jQuery方面没有太多经验,所以不确定我离这有多近。 I have a table in my Rails app and I want the rows where the score.test_type value == true. 我的Rails应用程序中有一个表,我想要score.test_type值== true的行。 (when it == true I have it display as the word "practice" if that matters) My table ID is scores and I have a CSS class of .score_table_color with a background-color. (当它== true时,如果有问题,我将其显示为单词“ practice”)我的表ID是成绩,并且我具有.score_table_color CSS类,具有背景色。 In my code below I don't reference score.test_type at all, which I'm not sure is an issue. 在下面的代码中,我根本没有引用score.test_type,我不确定这是一个问题。 Here is the jQuery code that I have so far 这是到目前为止我拥有的jQuery代码

$(function(){
$("#scores tr").keyup(function() {
  var textValue = $(this).val();
  if(textValue == false){
    // add css class or any manipulation to your dom.
    $("#scores tr").addClass('score_table_color');
  }
 });
});

I have this pasted in the users.js file, because the table appears on the user's page. 我将其粘贴在users.js文件中,因为该表出现在用户页面上。 (the user has_many scores) (用户has_many得分)

UPDATE: 更新:

I've updated my code to this: 我已经将代码更新为:

$(function(){
$("#scores tr").ready(function() {
  var textValue = $(this).val();
  if(textValue == false){
    // add css class or any manipulation to your dom.
    $("#scores tr").addClass("score_table_color");
  }
 });
});

This makes the entire table blue. 这会使整个表变成蓝色。 The problem with my code is that I seem to be saying that something in the table is false, not necessarily the score.test_type This is the way that cell is coded: 我的代码存在的问题是,我似乎在说表中的某些内容为假,而不一定是score.test_type。这是单元格的编码方式:

<td>
 <% if score.test_type == true %>
  <%= 'Practice' %>
 <% else %>
  <%= 'Actual' %>
 <% end %>      
</td>

I ended up just solving this using just Ruby instead of jQuery. 我最终仅使用Ruby而不是jQuery解决了这个问题。 (I'd still like to figure out how to get the jQuery to work properly. Here's how I made it work: (我仍然想弄清楚如何使jQuery正常工作。这是我的工作方式:

Below is the code showing the beginning of my loop that iterates through the user's scores to create the table. 下面的代码显示了循环的开始,该循环迭代用户的分数以创建表。 Al I really did was enclose the in an if/else statement that says "if score.test_type == false, then apply the css class score_table_color, else just leave the alone. Then my first is the score.test_type, which is a boolean. If that is false, then the row background color changes based on my CSS. 我真正做过的事情是将if / else语句括起来,说“ if score.test_type == false,然后应用css类score_table_color,否则就别管它了。然后我的第一个是score.test_type,它是一个布尔值如果那是假的,那么行的背景颜色会根据我的CSS改变。

<% @scores.order('taken_date').where.not(:name => 'ACT').each do |score| %>
    <% if score.test_type == false %>
        <tr class="score_table_color">
    <% else %>
        <tr>
    <% end %>

          <td>
            <% if score.test_type == true %>
              <%= 'Practice' %>
            <% else %>
              <%= 'Actual' %>
            <% end %>       
          </td> 

Yes, the answer above definitely will work. 是的,上面的答案肯定会起作用。 But I like to post how I solved my own in a Job Board I created, so my answers will be a little detailed than just the task needed from the question, but my conclusion will strictly deal with using a simple if..else statement to answer the question. 但是我喜欢在自己创建的工作委员会中发布如何解决自己的问题,因此我的答案将比问题所需要的任务要详细一些,但是我的结论将严格使用简单的if..else语句处理。回答问题。

Firstly 首先

I defined a css to deal with my highlights, such that I have the following in my stylesheet pipeline: 我定义了一个css来处理我的突出显示,以便在样式表管道中包含以下内容:

/* For Row Highlighted */

.highlighted {
  background-color: #CFF3FF;
}

Note : You could use any color as you wish. 注意 :您可以根据需要使用任何颜色。

Secondly 其次

I added a column to my already existing table with a Boolean data type, by running rails g migration addHighlightedToJobs . 我通过运行rails g migration addHighlightedToJobs在我现有的表中添加了一个具有布尔数据类型的rails g migration addHighlightedToJobs In my own case highlighted is my ColumnName, and Jobs is my TableName, such that my migration looks as this: 在我自己的情况下, 突出显示的是我的ColumnName,而Jobs是我的TableName,这样我的迁移看起来像这样:

class AddHighlightedToJobs < ActiveRecord::Migration
  def change
    add_column :jobs, :highlighted, :boolean
  end
end

Then, I did run rake db:migrate to migrate my new migration. 然后,我确实运行rake db:migrate来迁移新迁移。

Note : TableName must be Plural , if you are generating migrations as I have stated. 注意 :如果要生成迁移(如我所述),TableName必须为Plural

Thirdly 第三

I added a check_box to my form just alternate between true and false when it is either checked or unckecked on my column name called highlighted 我在表单中添加了一个check_box,当在我的列名(突出显示)上选中或取消选中时,它在truefalse之间交替

<%= f.check_box :highlighted %>

Lastly 最后

To come to an answer to the question asked, since I have a check_box on my form, if checked, it posts to be true on my DB Table, and if unchecked, it posts to be false on my DB Table. 为了回答这个问题,由于我的表单上有一个check_box,如果选中,它将在我的数据库表中发布为true ,如果未选中,则它将在我的数据库表中发布为false

So, to change my table row color in my rails application, I used a simple if..else statement to call highlighted from my stylesheet with that such that I have: 因此,要在rails应用程序中更改表行的颜色,我使用了一个简单的if..else语句来调用样式表中的高亮显示,使我具有:

<% @jobs.each do |job| %>

    <% if job.highlighted == true %>
      <section class="container">
        <div class="row jobs highlighted">
        <div class="col-md-4" style="font-size: 20px;"><strong><%= job.company_name %></strong></div>
        <div class="col-md-4" style="font-size: 20px;"><%= link_to(job.title, job) %></div>
        <div class="col-md-4 text-right" style="font-size: 20px; float: left;"><%= job.created_at.strftime('%d %B %y') %></div>
        </div>
      </section>
    <% else %>
      <section class="container">
        <div class="row jobs">
        <div class="col-md-4" style="font-size: 20px;"><strong><%= job.company_name %></strong></div>
        <div class="col-md-4" style="font-size: 20px;"><%= link_to(job.title, job) %></div>
        <div class="col-md-4 text-right" style="font-size: 20px; float: left;"><%= job.created_at.strftime('%d %B %y') %></div>
        </div>
      </section>
    <% end %>

<% end %>

I really believe my conclusion is DRY-which violates Rails Paradigm. 我真的相信我的结论是DRY,它违反了Rails范例。 We could rewrite it such that it becomes: 我们可以将其重写为:

<% @jobs.each do |job| %>
      <section class="container">
        <% if job.highlighted == true %>
          <div class="row jobs highlighted">
        <% else %>
          <div class="row jobs">
        <% end %>
        <div class="col-md-4" style="font-size: 20px;"><strong><%= job.company_name %></strong></div>
        <div class="col-md-4" style="font-size: 20px;"><%= link_to(job.title, job) %></div>
        <div class="col-md-4 text-right" style="font-size: 20px; float: left;"><%= job.created_at.strftime('%d %B %y') %></div>
        </div>
        </div>
      </section>
<% end %>

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

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