更新操作后,如何在rails上刷新数据? [英] How can I refresh a datatable on rails after an update action?

查看:317
本文介绍了更新操作后,如何在rails上刷新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Rails新手,我有一个完整的数据表(感谢 railstasts#340 )。

I'm a Rails newbie and I have a full working datatable (thanks to railscasts #340).

在我的数据视图中,我设置了2个按钮:

In my datatable view I set up 2 buttons:

<button id="Refresh" type="button">Refresh</button>
<td><%= button_to 'dogroup', dogroup_utyord_path(format: "json"), remote: true %></td>

<table id="utyords" class="display" data-source="<%= utyords_url(format: "json") %>" width="100%">
...
</table>

这是我的工作咖啡:

jQuery ->
  oUtyordTable = $('#utyords').dataTable
  sPaginationType: "full_numbers"
  bJQueryUI: true
  bProcessing: true
  bServerSide: true
  sAjaxSource: $('#utyords').data('source')

$("#Refresh").click ->
  oUtyordTable.fnDraw()

在我的控制器中:

def index
  @utyords = Utyord.all
  respond_to do |format|
    format.html 
    format.json { render json: UtyordsDatatable.new(view_context) }
  end
end

def dogroup
  ddett = Utyord.all
  ddett.update_all "grp = 1"
  respond_to do |format|
    format.html 
    format.json 
  end
end

我按下dogroup按钮,数据库中的动作更新记录正确:当我按下刷新按钮时,我可以看到更新的datatable。

I push the "dogroup" button and the action updates records on the database correctly: when I push the "refresh" button I can see the datatable updated.

一切工作正常,但....当我推动小组按钮时,是否有一种方式更新datatable,而不必推动刷新按钮?

Everything works fine, but.... is there a way the datatable updates when I push the "dogroup" button without have to push the "refresh" button?

我知道我想念的东西...

I know I miss something...

推荐答案

可以像这样简单吗?

$('input[value="dogroup"]').mouseup ->
    oUtyordTable.fnDraw()

尝试... Mouseup,因为我们希望它运行点击后。

Try it... Mouseup because we want it to run after click.

如果没有,请在表单中添加一个标识符:

If it doesn't, then add an identifier to the form:

<%= button_to 'dogroup', dogroup_utyord_path(format: "json"), 
                         remote: true, form_class: "dogroup_form" %>

然后提交表单后,运行脚本:

And then submit the form and after than run a script:

$(".dogroup_form").submit(e)->
     e.preventDefault()         #to stop the form
     $(this).submit()           #to manually submit it and
     oUtyordTable.fnDraw()      #to run your function

更新:从这个帖子:要使用表单的ajax请求中没有被你自己的代码处理的响应,你可以绑定一个你的表单的事件(简单的 javascript ,而不是 coffeescript ):

UPDATE: From this post: to use a response from a form's ajax request that has not been handled by your own code, you can bind an event to your form (in plain javascript, not coffeescript):

丢弃我建议的 $(。dogroup_form)。submit(e) - > 功能,并添加:

Discard my proposed $(".dogroup_form").submit(e)-> functionality and add this:

$(".dogroup_form").bind('ajax:complete', function() {
    oUtyordTable.fnDraw();
});

在线,这可以是相当于以上的 CoffeeScript

From online, this could be the CoffeeScript equivalent to the above:

$(".dogroup_form").bind "ajax:complete", ->
    oUtyordTable.fnDraw()
    return

这篇关于更新操作后,如何在rails上刷新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆