简体   繁体   English

在通过所有页面获取数据后,如何循环排序来自 ajax 调用的数据?

[英]How to loop sort data coming from ajax calls after data has already been fetched through ALL pages?

I'm having trouble for sorting the array through loop.我无法通过循环对数组进行排序。 It works partially every 20 element (sorting page by page ).它每 20 个元素部分工作(逐页排序)。

Is it possible to loop through all the pages in a website?是否可以循环浏览网站中的所有页面?

It can be around 10 Ajax call to get all data which is (2000 items).大约需要 10 次 Ajax 调用来获取所有数据(2000 项)。

in the below code my query to fetch the data through Ajax and it works fine:在下面的代码中,我查询通过 Ajax 获取数据,它工作正常:

<script language="javascript">
var current_page = 1, count = 100;

$(function(){
setTimeout(function(){
    newSearch();
}, 0);
});

function newSearch() {
count = 0;
current_page = 1;
$('#main').html('');
doSearch();
}

function doSearch(){
 $.ajax({
    
 url:'https://live.ksmobile.net/live/boys?'  
 data: {
        page_size: 900,
        page_index: current_page
    },
    cache: false,
    type: 'GET',
    dataType: 'json',
    success: function(r){

        var max = $('#limit').val();
        if (r.data.video_info.length < max) max =r.data.video_info.length;

        for (index = 0; index < max; index++) {
            var entry = r.data.video_info[index];
            
                            var level = parse Int(entry.level);

            if ((level > $('#min').val()) && (level < $('#max').val())) {
              count++;
              var HTML = '<div class="entry ' + '"><h="' + entry + '">';
              HTML += '<h3>Level: <span>' + entry.level + '</span> <span>' + entry.name + '' + '</span>id:<span>' + entry.heat;
              HTML += '</div>';
              $('#main').append(HTML);
              
     
                

The issue in the below sorting function,it works only for each page while i need to sort all elements by (level) in the main page.下面排序 function 中的问题,它仅适用于每个页面,而我需要在主页中按(级别)对所有元素进行排序。

function arr(a,b) {
  return parseInt(a.level, 10) - parseInt(b.level, 10);
}
r.data.video_info.sort(arr);

}
if ((current_page < 100) && (count < $('#limit').val() )) {
            current_page++;
            setTimeout(function(){
                doSearch();
            },0);
        }

    }
});
}

I want to display all of the website's pages (sorted) as parts of my main page.我想将网站的所有页面(已排序)显示为我的主页的一部分。 Your help is highly appreciated.非常感谢您的帮助。

Sorting of paged data shouldn't be done in javascript.不应该在 javascript 中对分页数据进行排序。 You need to edit the backend query to sort it from there.您需要编辑后端查询以从那里对其进行排序。 Also, it is a very bad idea to fetch the data of all pages just for the sorting since it will defeat the purpose of pagination and will create a massive lag to your API calls especially if you have a lot of data to fetch.此外,仅为了排序而获取所有页面的数据是一个非常糟糕的主意,因为它会破坏分页的目的,并且会给您的 API 调用造成巨大的延迟,尤其是在您有大量数据要获取的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM