2016-06-15 26 views
0

我有这个小项目,以获得每个网站的多个Alexa排名。jQuery的排序表后ajax响应

我想排序表使用jquery.tablesorter.js但它不工作,因为我发送每个URL的ajax请求,然后显示它。

这是我jQuery和Ajax请求:

<script> 
$(document).ready(function(){ 

    $('#btnurls').click(function(){ 
     $('#loadingmessage').show(); 
     var arrayOfLines = $('#websiteurls').val().split('\n'); 

     $.each(arrayOfLines, function(index, item) { 
       $.ajax({ 
       type: "POST", 
       url: "ajax_pages/ajax_alexa.php", 
       data: "textposted=" + item, 
       cache: false, 
       success: function(html){ 
          $('#loadingmessage').hide(); 
          for (var i = 1; i <= 1; i++) {     
           $("#content table").delay(1000) 
            .queue(function (nxt) { 
            $(this).append(html);        
            nxt(); 
           }); 
          } 
          $("#websiteurls").val(""); 
        } 
       }); 

     }); 
    }); 

$("#textarea_reset").click(function(){ 
    $("#websiteurls").val(""); 
}); 

}); 
</script> 

alexa_rank.php

<?php $line = $_POST['textposted'];?> 
<tr> 
<td><?php if (!filter_var($line, FILTER_VALIDATE_URL) === false) {echo $line;} else {echo "$line";}?></td> 
<td> 
<?php 

     $xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$line); 
    $rank = isset($xml->SD[1]->POPULARITY)?$xml->SD[1]->POPULARITY->attributes()->TEXT:0; 
    $country_rank=isset($xml->SD[1]->COUNTRY)?$xml->SD[1]->COUNTRY->attributes()->RANK:0; 
    $country_name=isset($xml->SD[1]->COUNTRY)?$xml->SD[1]->COUNTRY->attributes()->NAME:0; 
    echo $rank; 
?> 
</td> 
<td><?php echo $country_rank; ?></td> 
<td><?php echo $country_name; ?></td> 
</tr> 

现场测试https://www.bulkalexarankchecker.com/

+0

您在您提供的实时测试网站中没有使用jquery.tablesorter.js。 – apokryfos

+0

我已经添加了他们 – user3709241

回答

0

基于AJAX提供与jQuery的例子的tablesorter你需要数据更新时触发表上的update事件。它可能会是这样的:

$.each(arrayOfLines, function (index, item) { 
    $.ajax({ 
     type: "POST", 
     url: "ajax_pages/ajax_alexa.php", 
     data: "textposted=" + item, 
     cache: false, 
     success: function (html) { 
      $('#loadingmessage').hide(); 
      for (var i = 1; i <= 1; i++) { 
       $("#content table").delay(1000) 
         .queue(function (nxt) { 
          $(this).append(html); 
          $(this).trigger("update"); 
          nxt(); 
         }); 
      } 
      $("#websiteurls").val(""); 
     } 
    }); 

}); 

理想情况下,你会等到所有数据被取出,但你没有为当前的代码这样的事件所以这可能会工作得同样好。

+0

非常感谢你的工作 – user3709241