javascript - How to refresh a div without reloading the entire page? -
i have div
, want refresh content without reloading entire page.
i tried many times many methods (ajax call, javascript, etc...), don't have did it.
my div displays entire content of modelname.all
, in particular div, can add new entities (thanks form) of model use.
i display div rails partial view.
currently, use when user submit form :
$('#articlesdiv').html("<%= escape_javascript (render partial: 'articles') %>");
but seems partial view doesn't have up-to-date model. because before add new entity, see example 5 elements, , after add new entity, see again 5 elements. test purposes, use javascript alert
displays modelname.all.count
and alert displays 5
! when @ database content (before alert), can see 6 elements ! , when refresh page manually, div
displays 6 elements.
code explain :
the main view :
<div id="articlesdiv"> <%= render(:partial => "articles") %> </div>
_articles (the partial view) :
<script> alert('<%=article.all.count%>'); </script> <% article.all.each |article|) %> <strong><%= article.name %></strong><br /> <% end %>
my goal have smooth interface doesn't reload entire page times add other article.
you can have contents of div
in partial , load content using url say, /divcontents
. can refresh <div>
using .load()
:
$('#articlesdiv').load("/divcontents");
Comments
Post a Comment