新手问题 局部刷新问题

ckl · 2014年04月03日 · 最后由 ckl 回复于 2014年04月04日 · 3030 次阅读

我有一个 index 页面有一个目录:

<ul>
  <li>衣服</li>
  <li>裤子</li>
</ul>
<table>
  <tr>
     <th>名称</th>
     <th>价格</th>
  </tr>
  <%= render :partial => "goods", :collection =>Good.all, :as => :phy_item %>
</table>

我这里的功能是:点击衣服时显示衣服的 table,隐藏裤子的 table。反之。我现在 update 裤子的一条记录,我先实现局部刷新: 方案一:刷新裤子的整个 table,及重新 rander(只 rander 裤子) 方案二:更新裤子 table 中一条记录及 update 的那条记录

请问:那个方案好?怎么解决?谢谢!

用 jq 的 load

#1 楼 @badboy 我把我的问题说明白了,请问你的意思?

还是有点不明晰,具体是怎么更新? 方案一:只需要给 table 加个 id,服务器返回结果然后替换掉就可以了,简单,不过 table 很大的话效率不如人意。

<ul>
  <li>衣服</li>
  <li>裤子</li>
</ul>
<table id="will----render">
  <tr>
     <th>名称</th>
     <th>价格</th>
  </tr>
  <%= render :partial => "goods", :collection =>Good.all, :as => :phy_item %>
</table>
$('#will----render').replaceWith('<%= j render('table') %>');

方案二: _goods.html.erb 里面加上唯一标识比如 id,服务器同理替换

<tr id="<%= phy_item.id %>"></tr>

#3 楼 @saiga 谢谢!但是我现在碰到一个问题:

$("#tpl_item_edit_modal_confirm").click (event) ->
    #edit_tpl_item_modal.submit()
    $.ajax
      type: "Patch",
      url: update_url,
      dataType: "script",
      data: {"_method":"update_item"},
      complete: ->
        location.reload()
    event.preventDefault();

我这个表单数据需要用 json 的方式写到 date 吗?怎么写?那在 controller 中怎么获取这个 json 数据呢?

我知道了!

$.ajax
      type: "Patch",
      url: update_url,
      dataType: "script",
      data: $("#edit_tpl_item_modal").serialize()
      complete: ->
        location.reload()
    event.preventDefault();
需要 登录 后方可回复, 如果你还没有账号请 注册新账号