如何在数据库中用ajax一点一点地加载数百万行 [英] how to load millions rows, little by little with ajax in datatables

查看:206
本文介绍了如何在数据库中用ajax一点一点地加载数百万行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到数据表我无法实现这个目标。现在我可以用不同的方式加载它们,使用ajax(通过使用服务器端)不同的行和不同的顺序,但是现在我无法加载行,例如在4或5次中进行多个请求ajax(我想使用滚动加载一点点)。

Seeing datatables I cannot achieve this goal. For now I can load them in different ways, with ajax (by using the server side) different rows and with different orders, but for now I cannot load rows for example in 4 or 5 times making multiple request ajax ( I want to use the scroll to load little by little).

我该怎么做?

推荐答案

ajax加载x行数,从第一个开始,然后当最后一个条目的顶部偏移量进入视图时,您可以在同一个文件上触发另一个ajax事件,但是使用x来知道最后一行的加载方式它会知道从哪个行开始加载?

What about modifying your ajax to load x number of rows, starting with the first, then when the top offset of the last entry comes into view you could fire another ajax event on the same file, but use x to know what the last row was to be loaded so it will know which row to start loading from?

所以说它最初加载行0-5,当5被看到时,它会触发一个将通过的事件相同的ajax但是值为5,所以它将从6开始并加载下一个6行,等等你可以使用相同的函数进行初始和后续加载,它只会被附加到html的主要元素

So say it loads rows 0-5 initially, and when 5 comes into view, it fires an event that would pass through the same ajax but with the value 5, so it would start from 6 and load the next 6 rows, and so on You would be able to use the same function for the initial and following loads, it would just get appended to the html of the main element.

编辑
好​​的,所以我们需要一个php文件来拉和格式化数据,包含ajax的html文件,并显示所有内容。

EDIT Okay, so we'll need a php file that pulls and formats the data, and an html file that contains the ajax and displays everything.

为PHP,如:

$offset = 0;
$sql = "SELECT * FROM table ORDER BY id LIMIT 20, ".$offset;

然后运行一个循环来格式化行,希望它们如何。

Then run a loop to format the rows how you would like them to be.

这将首先填充页面的20个帖子,默认为0。

This will populate the page initially with 20 posts from the offset, which is 0 by default.

然后对于Ajax,那里'两个部分。首先是在最后一个项目进入时创建一个动作:

Then for the Ajax, there're two parts. First is create an action when the last item comes into view:

var go = true;
function hitbottom() {
    var sh = $(window).scrollTop();
    var wh = $(window).height();
    var ih = $('.item').last().offset().top;
    if ((sh+wh)<ih && go) {
        //run ajax
    }
}

当最后一个项目的顶部触摸屏幕底部时,应该滚动来触发。

This should be run on scroll to trigger when the top of the last item hits the bottom of the screen.

第二个是创建ajax表单,需要知道显示的最后一行的id。它将使用该值来更改php文件中的偏移量变量的值,然后将获取接下来的20行并将其发送回jquery以追加到回调中的html,等等...

The second is to create the ajax form, which needs to know the id of the last row shown. It will use that value to change the value of the offset variable in the php file, which will then get the next 20 rows and send them back to jquery to append to the html in the callback, and so on...

ajax部分应该设置为false,然后在回调中将其设置为true,以便重新开始。

The ajax part should set go to false, then set it back to true on the callback to start it all over again.

要附加数据,您可以使用以下内容:

To append the data, you can use something like this:

$('#element').append(data);

这是未经测试的,只是一个想法。我可以想像,它类似于你的facebook feed加载。一旦你碰到底部,它弹出加载图标,并加载下一组帖子。只有这样,它才会在你打到底部之前开始加载,所以除非你立即下注,否则应该可以很快地拉出新数据。

This is untested, and just an idea. I would imagine it's similar to how your facebook feed loads. Once you hit the bottom it pops up the loading icon, and loads the next set of posts. Only with this, it will begin loading just before you hit the bottom, so unless you jet down right away, it should be able to pull the new data pretty quickly.

这也允许您逐渐增加html文件的内容,而不是一次加载大量的内容并减缓事情。可能还有一个限制,但它应该相当大。

This also allows you to gradually increase the html file content, without loading a lot of content at once and slowing things down. There may still be a limit, but it should be pretty large.

这篇关于如何在数据库中用ajax一点一点地加载数百万行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆