I have successfully installed jquery-rails in my rails 3 test app. I already had it working. Can somebody pls point out to me how to make this jquery code I got from Sreekumar yesterday to work on my rails 3 test app.
Here is the jquery code and I placed this on application.js
:
$(document).ready(function() {
$('#table_list').find('tr>td').hover(function() {
//$(this).css("background-color", "green");
$('table_list').addClass('highlight');
}, function() {
//$(this).css("background-color", "inherit");
$('table_list').removeClass('highlight');
});
$('#table_list tr:even').addClass('even');
$('#table_list tr:odd').addClass('odd');
});
The objective of this code is to make the tables row color alternate and highlight the selected row. Alternating the row color works perfectly fine, however, the highlight is not working.
Here is the rails 3 code in index.html.erb
:
<h1>Listing Ninjas</h1>
<table id="table_list">
<tr>
<th>Name</th>
<th>Rank</th>
<th>Village</th>
<th>Country</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @ninjas.each do |ninja| %>
<tr>
<td><%= ninja.name %></td>
<td><%= ninja.rank %></td>
<td><%= ninja.village %></td>
<td><%= ninja.country %></td>
<td><%= link_to 'Show', ninja %></td>
<td><%= link_to 'Edit', edit_ninja_path(ninja) %></td>
<td><%= link_to 'Destroy', ninja, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= lin开发者_开发技巧k_to 'New Ninja', new_ninja_path %>
And here is my css code which is in a separate file application.css
:
#table_list{
border: solid 1px #666;
border-collapse: collapse;
margin: 10px 0;
}
#table_list th{
font-size: 12px;
color: #FFF;
background-color: #404C99;
padding: 4px 8px;
padding-bottom: 4px;
text-align: left;
}
#table_list .highlight {
background-color: yellow;
}
#table_list td {
font-size: 12px;
padding: 2px 6px;
}
#table_list .even td {
background-color: #E3E6FF;
}
#table_list .odd td {
background-color: #D1D8F6;
}
All of the 3 codes are in separate files.
I'm kinda new to rails and jquery. Im still a beginner. Pls be patient...
This might not answer your question, but there is a much simpler way to achieve even/odd color table, using the cycle command
@items = [1,2,3,4]
<table>
<% @items.each do |item| %>
<tr class="node <%= cycle("even", "odd") -%>">
<td>item</td>
</tr>
<% end %>
</table>
Hope this at introduces you to a cool Rails utility
link : Converting Rails 3 to Rails 2: helpers with blocks
once u are able to add odd/even to tr , you dont have to use jquery for this purpose simple css is enough
.odd{
background-color:#cccccc;
}
.even{
background-color:#ffffff;
}
.node:hover{
background-color:#ff0000;
}
$(document).ready(function(){
$("#table_list tr").mouseover(
function() {
$(this).addClass("highlight");
}).mouseout(
function() {
$(this).removeClass("highlight");
});
$("#table_list tr:even").addClass("even");
$("#table_list tr:odd").addClass("odd");
});
For the css:
#table_list .highlight {
background-color: yellow;
}
#table_list .even {
background-color: #E3E6FF;
}
#table_list .odd {
background-color: #D1D8F6;
}
Hope this works ^^
精彩评论