I have a Rails app displaying the workshifts of employees in a table. The user can edit the shiftstart
and shiftend
directly in the table with the Best In Place gem.
I got the update of the database working, but the DIV
needs to be reloaded so the bar showing the duration of the shift either expands or contracts depending on the change in the start and end time.
I guess I need some javascript to do this and it might be simple, but I can't figure it out :)
VIEW
<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 .... */
});
});
EDIT:
The rendered 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>
The ajax success callback should give you the response of the ajax request:
$(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'
});
});
});