【问题标题】:How to update or refresh table using Rails Actioncable?如何使用 Rails Actioncable 更新或刷新表?
【发布时间】:2017-11-24 01:52:03
【问题描述】:

我的行动电缆工作正常,它向我显示数据内容的警报(尽管它显示在所有页面中)。

这是我的情况:具有 ADMIN 角色的用户可以访问带有显示销售列表的表格 (html id=tablaventas) 的 url (http://localhost:3000/ventas/)。每次用户(角色 SELLER)在数据库中插入新的销售时,该表必须自动刷新。

这个角色为卖主的用户有一个 url (http://localhost:3000/ventas/new),其中包含一个向数据库插入新销售的表单。 Actioncable 实现工作正常,但我有两个问题:

1- 如何从 ventas.coffee 刷新表:

//here i am stuck
 received: (data) ->
   alert data.name  //this works ok
   //...AND HERE I WANT TO UPDATE or refresh THE ADMIN TABLE

2- 另外,例如,如何仅向管理员特定视图显示警报消息?在这种情况下,仅在视图 ventas/index.html.erb 中。

【问题讨论】:

    标签: javascript jquery ruby-on-rails coffeescript actioncable


    【解决方案1】:
    1. 您可以在表格中再添加一行吗?

    $('#tablaventas').append("<tr><td>#{data.name}</td>...other tds...</tr>")

    如果您需要刷新整个表格 - 您需要在表格容器元素上调用 .html(data.table_html)。这也意味着您必须通过render_to_string('table') 或其他方式在控制器中呈现 table_html。

    1. 我认为您的问题是您在全球范围内订阅了 action cable 更新,而您只需要在索引页面上订阅。这个blog post 应该可以帮助您解决这个问题。

    希望以上内容有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 2015-03-03
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多