Am trying to do ajax pagination without using any pagination gems for menus (which is like template). For example to display brand menu on mouse hover of the brand menu name am using “menu_brand” render because when page loads at that time only am getting brands from the database. for this rendered view I have to do pagination function. Am successfully getting the current page contents in the console log.
In controller am doing like this
def menu_brand
$cnt=Brand.count
end
def menu_brand_page
$st=0
if !(params[:startingRow].nil?)
$st = params[:startingRow].to_i
end
$brands=Brand.limit(24).offset($st)
end
In _menu_brand.html.erb
<% if !($cnt.nil?) %>
<% @pg = ($cnt / 24).ceil %>
<div class="paginator">
<%
for j in 0..@pg %>
<%= link_to "#{j+1}", "javascript:GotoPage('#{j*24}')", %>
<% end
%>
</div>
<table width="700px"; align="center" id="menupage">
<%= render "menu_brand_page"%>
</table>
<% end %>
<script type="text/javascript">
function GotoPage(startrow) {
var theURL = '/menu_brand_page/'+<%= params[:deptid] %>
+'/' + startrow;
$.ajax({
url : theURL
});
}
</script>
In menu_brand.js.erb
$('#bval').html('<%= escape_javascript render ('menu_brand') %>');
In _menu_brand_page.html.erb
<% $brands.each do |brand| %>
<%= brand.name %>
<% end %>
In menu_brand_page.js.erb
$('#menupage').html('<%= escape_javascript render ('menu_brand_page') %>');
In layout
<div id="bval">
<%= render "menu_brand" %>
</div>
<script type="text/javascript">
$(document).ready(function() {
var theBrand = '/menu_brand/' + <%= params[:deptid] %>;
$.ajax({
url : theBrand
});
});
</script>
My router.rb
match "menu_brand_page/:deptid/:startingRow" => "product_details#menu_brand_page"
match "menu_brand/:deptid" => 'product_details#menu_brand'
console log output
$(‘#menupage’).html(‘\n\n \nBC\n</td>\n\n \nBC Footwear\n</td>\n\n \nBCBG\n</td>\n</tr>\n\n\n \nBCBGeneration\n</td>\n\n \nBCBGMAXAZRIA\n</td>\n\n \nBCX\n</td>\n</tr>\n\n\n \nBe Creative\n</td>\n\n \nBeach Bunny\n</td>\n\n \nBeach House\n</td>\n</tr>\n\n\n \nBeach House Woman\n</td>\n\n \nbebe\n</td>\n\n \nBecca\n</td>\n</tr>\n\n\n \nBed Stu\n</td>\n\n \nBellatrix\n</td>\n\n \nBELLE BY SIGERSON MORRISON\n</td>\n</tr>\n\n\n \nBelle Du Jour\n</td>\n\n \nBelle Noel\n</td>\n\n \nBen Amun\n</td>\n</tr>\n\n\n \nBENCH\n</td>\n\n \nBerek\n</td>\n\n \nBernardo\n</td>\n</tr>\n\n\n \nBERNIE OF NEW YORK\n</td>\n\n \nBETMAR\n</td>\n\n \nBetsey Johnson\n</td>\n</tr>\n\n’);
Please help me am not getting why contents are not changing in menu_brand view.
Thank you @agncleod. @agmcleod is right. I was calling render at document ready function and I have used the global variables a lot. Now am rendering on mouse hover event and am using instance variable. I corrected this mistake. I have changed all my global ($) variables to instance (@) variable. In layout am calling render function as I mentioned below.
In Layout