【发布时间】:2014-04-15 21:57:00
【问题描述】:
页面使用这个 javascript 函数来改变表格中行的背景颜色:
application.js
$(function () {
$('.table tr').click(function () {
var self = this;
var classes = ['new', 'placed', 'pickedUp', 'enroute'];
var className;
$.each(classes, function(key, val){
if($(self).hasClass(val)){
className = val;
$(self).removeClass(val);
}
})
var newClass = classes[($.inArray(className, classes) + 1) % classes.length];
$(self).addClass(newClass);
});
});
orders.css.scss
.new{
background: white;
}
.placed{
background: #3498db;
}
.pickedUp{
background: #f1c40f;
}
.enroute{
background: #2ecc71;
}
index.html.erb
<div class = "jumbotron">
我的订单
<table class="table">
<thead>
<tr class = "id= row">
<th>Name  </th>
<th>Location  </th>
<th>Phone Number  </th>
<th>Food  </th>
<th>Subtotal</th>
<th>Total</th>
<th>Notes</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @orders.each do |order| %>
<tr>
<td><%= order.created_at.time.asctime %> </td>
<td><%= order.name %> </td>
<td><%= order.location %> </td>
<td><%= order.phone_number %> </td>
<td><%= order.food %> </td>
<td><%= best_in_place order,
:subtotal,
:type => :textarea,
:html_attrs => { :cols => '5', :rows => '1' }
%> </td>
<td> <%= best_in_place order,
:total,
:type => :textarea,
:html_attrs => { :cols => '5', :rows => '1' }
%> </td>
<td> <%= best_in_place order,
:notes,
:type => :textarea,
:html_attrs => { :cols => '20', :rows => '3' }
%> </td>
<% if Time.now < order.created_at.time + 30.seconds %>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Order', new_order_path %>
我知道如何使用迁移向订单添加不同的属性。当背景颜色更改时,我希望将更改保存到服务器(与将文本字段中的最佳就地编辑保存到服务器的方式相同)。请帮助我了解如何做到这一点。谢谢。
【问题讨论】:
标签: javascript jquery ruby-on-rails postgresql activerecord