Javascript Ajax没有';在向下滚动页面时,在django动态数据检索中执行成功功能
我试图在初始加载中加载35条记录,当页面向下滚动时,接下来的35条记录应该会被填充。但不幸的是,初始加载的35条记录被填充,当我向下滚动页面时,什么也没有发生(接下来的35条记录没有被填充)-我只能看到消息和加载程序图像“我们正在加载更多的票证”。。请帮忙,谢谢 Views.py:Javascript Ajax没有';在向下滚动页面时,在django动态数据检索中执行成功功能,javascript,jquery,ajax,django,Javascript,Jquery,Ajax,Django,我试图在初始加载中加载35条记录,当页面向下滚动时,接下来的35条记录应该会被填充。但不幸的是,初始加载的35条记录被填充,当我向下滚动页面时,什么也没有发生(接下来的35条记录没有被填充)-我只能看到消息和加载程序图像“我们正在加载更多的票证”。。请帮忙,谢谢 Views.py: def table_row(request, start=0, count=35): #set count here for how many ticket to load each call. Set it so
def table_row(request, start=0, count=35): #set count here for how many ticket to load each call. Set it so it loads slightly more than enough to fill a page.
if request.method == 'POST':
filter = request.POST.get('category', False)
if filter:
tickets = Ticket.objects.filter(category=filter)
else:
tickets = Ticket.objects.all()
start = int(start)
return render_to_response('table-view.html',
{
'tickets':tickets[start:start+count], #slices the index of tickets to return
}, RequestContext(request))
url(r'^table-view/$', 'app.views.table_row', name='table-view'),
url.py:
def table_row(request, start=0, count=35): #set count here for how many ticket to load each call. Set it so it loads slightly more than enough to fill a page.
if request.method == 'POST':
filter = request.POST.get('category', False)
if filter:
tickets = Ticket.objects.filter(category=filter)
else:
tickets = Ticket.objects.all()
start = int(start)
return render_to_response('table-view.html',
{
'tickets':tickets[start:start+count], #slices the index of tickets to return
}, RequestContext(request))
url(r'^table-view/$', 'app.views.table_row', name='table-view'),
表格视图.html
<html>
<head>
<title></title>
<style type="text/css">
#main-window{
overflow: auto;
}
.hidden {
display:none;
}
</style>
</head>
<body>
<div id='filter-bar'>
<select>
<option value='bug'>Bugs</option>
<option value='estimate'>Estimates</option>
</select>
</div>
<div id='main-window'>
<table id='full-table'>
<thead>
<tr>
<th>Ticket ID</th>
<th>Ticket Title</th>
<th>User</th>
<th>Description</th>
<th>Date</th>
</tr>
</thead>
<tbody id='table-body'>
{% for ticket in tickets %}
<tr class='ticket'>
<td>{{ ticket.id }}</td>
<td>{{ ticket.title }}</td>
<td>{{ ticket.username }}</td>
<td>{{ ticket.description}}</td>
<td>{{ ticket.date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<!--The loading animation. Appears on first page load and every time the scrolling reaches the bottom -->
<div id='text-loading'>
We are loading more tickets
<img src="../../static/app/img/ajax-loader.gif" />
</div>
</div>
<script type="text/javascript">
var processing;
function loadTickets(){
//the ajax call to load more tickets into the table
var ticket_count= $('tr.ticket').length; //grabs the number of data results returned
$.ajax({
type:'post',
url:'{% url 'app.views.table_row' %}'+ticket_count+'/'+,
data:{
'csrfmiddlewaretoken':getCookie('csrftoken'), //This is the ajax csrf protection function
},
success:function(data){
$('#text-loading').toggleClass('hidden',true); //hides the loading animation when ajax call returns tickets
$('#table-body').append(data); //adds data to the end of the table
processing = false; // the processing variable prevents multiple ajax calls when scrolling
}
});
}
$(document).ready(function(){
loadTickets(); //initial ticket load
$('#main-window').scroll(function(e){
if (processing){
return false;
}
if ($('#content-window').scrollTop() >= ($('#full-table').height() -$('#content-window').height())){
processing = true; //prevent multiple scrolls once first is hit
$('#text-loading').toggleClass('hidden', false); //show the loading animation
loadTickets();
}
});
});
</script>
</body>
</html>
#主窗口{
溢出:自动;
}
.隐藏{
显示:无;
}
漏洞
估计
票号
票名
使用者
描述
日期
{票证%中的票证%
{{ticket.id}
{{ticket.title}
{{ticket.username}
{{ticket.description}}
{{ticket.date}
{%endfor%}
我们正在装更多的票
var处理;
函数loadTickets(){
//将更多票证加载到表中的ajax调用
var ticket_count=$('tr.ticket').length;//获取返回的数据结果数
$.ajax({
类型:'post',
url:“{%url”app.views.table_行“%}”+票证计数+'/'++,
数据:{
'csrfmiddlewaretoken':getCookie('csrftoken'),//这是ajax csrf保护函数
},
成功:功能(数据){
$(“#文本加载”).toggleClass('hidden',true);//在ajax调用返回票据时隐藏加载动画
$(“#表体”).append(data);//将数据添加到表的末尾
processing=false;//处理变量在滚动时阻止多个ajax调用
}
});
}
$(文档).ready(函数(){
loadTickets();//初始票证加载
$(“#主窗口”)。滚动(函数(e){
如果(处理){
返回false;
}
如果($(“#内容窗口”).scrollTop()>=($(“#完整表格”).height()-$(“#内容窗口”).height()){
processing=true;//第一次点击时防止多个滚动
$(“#文本加载”).toggleClass('hidden',false);//显示加载动画
加载票证();
}
});
});
您应该以某种方式计算您拥有的票证,以便将它们传递给php,如果成功实现,当前逻辑将在第一次和第二次加载35条记录时加载相同的35条记录(这意味着正在捕获票证计数)。但是我的页面向下滚动事件不起作用,我想。。我不确定javascript中的逻辑是否正确?($(“#内容窗口”).scrollTop()>=($(“#完整表格”).height()-$(“#内容窗口”).height()){…}内容窗口当前未就位。。我应该在主窗口之后添加它,还是这个Ajax元素是预定义的?