我有一个Rails应用程序,在一个表中显示员工的工作班次。用户可以使用Best in Place gem直接在表中编辑shiftstart
和shiftend
。
我得到了数据库工作的更新,但DIV
需要重新加载,因此显示轮班持续时间的栏根据开始和结束时间的变化而扩展或收缩。
我想我需要一些javascript来做到这一点,它可能很简单,但我不能弄清楚:)
视图<div class="shift-data">
<% @myShifts.where(:shiftdate => date).order(:shiftstart).each do |shift| %>
<div id="shift_<%= shift.id %>" class="shift-item span" style="width: <%= (shift.shiftend - shift.shiftstart) / 60 / 60 / 24 * 100 %>%; left: <%= shift.shiftstart.seconds_since_midnight / 60 / 60 / 24 * 100 %>%; top: 0px;">
<div class="hider" style="background-color: #<%= shift.type.color %>;">
<p>
<i class="icon gear"></i>
<%= best_in_place shift, :shiftstart, :type => :input, :display_with => :format_time_bip %> - <%= shift.shiftend.strftime("%H.%M") %> <span class="tag"><%= shift.type.name.upcase %></span>
</p>
</div>
</div>
<% end %>
</div>
APPLICATION.JS
$(document).ready(function() {
/* Activating Best In Place */
jQuery(".best_in_place").best_in_place();
});
$(function() {
jQuery(".best_in_place").bind("ajax:success", function() {
/* It's here I need some help .... */
});
});
编辑:呈现的HTML:
<div class="shift-data">
<div id="shift_3" class="shift-item span" style="width: 58.333333333333336%; left: 8.333333333333332%; top: 0px;">
<div class="hider" style="background-color: #df5c64;">
<p>
<i class="icon gear"></i>
<span id="best_in_place_shift_3_shiftstart" class="best_in_place" data-original-content="2000-01-01 02:00:00 +0100" data-type="input" data-attribute="shiftstart" data-object="shift" data-url="/shifts/3">02.00</span>
- 16.00
<span class="tag">FERIE</span>
</p>
</div>
</div>
</div>
ajax成功回调应该为您提供ajax请求的响应:
$(function() {
jQuery(".best_in_place").bind("ajax:success", function(data) {
var id = $(this).data('url').split('shifts/')[1]; // Maybe you don't need this
$('#shift_' + id).css({
'left' : 'xx',
'width': 'xx'
});
});
});