简体   繁体   English

使用jQuery刷新DIV成功

[英]Refresh DIV on success with jQuery

I have a Rails app displaying the workshifts of employees in a table. 我有一个Rails应用程序,在桌子上显示员工的工作班次。 The user can edit the shiftstart and shiftend directly in the table with the Best In Place gem. 用户可以使用Best In Place gem直接在表格中编辑shiftstartshiftend

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. 我得到了数据库的更新工作,但需要重新加载DIV ,因此显示转换持续时间的条形可以根据开始和结束时间的变化进行扩展或收缩。

I guess I need some javascript to do this and it might be simple, but I can't figure it out :) 我想我需要一些javascript来做这个,它可能很简单,但我无法弄清楚:)

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 的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: 呈现的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: 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'
    });

  });
});

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

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