使用Jquery对表进行排序


Table sort with Jquery

我已经尝试了其他线程的所有解决方案,但似乎都不起作用。我正在尝试显示权重分数递减的表格(这是我表格的最后一列)。

我已经编写了以下代码,这些代码不适用于PHP提供的表,但完全适用于html页面。

视图源:http://hidden.com/diversefund.php

它工作的html页面:http://hidden.com/new.html

我正在使用数据库提供的表,所以这是一个问题。

我的php脚本中的重要代码行:

<style>
#form {
border: 5px solid violet;
}
input[type=submit] {padding:5px 15px; background:white; border:1px solid    blue;
cursor:pointer;
-webkit-border-radius: 5px;
 border-radius: 5px; }
 </style>
 <script type='text/javascript' src='http://code.jquery.com/jquery-compat-git.js'></script>
  <script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>

   <link rel="stylesheet" type="text/css" href="/css/result-light.css">
 <script type='text/javascript'>
$(window).load(function(){
var $tbody = $('table tbody');
$tbody.find('tr').sort(function (a, b) {
var tda = parseFloat($(a).find('td:eq(7)').text()); //the column to sort by
var tdb = parseFloat($(b).find('td:eq(7)').text()); //the column to sort by
// if a < b return 1
return tda < tdb ? 1
// else if a > b return -1
:
tda > tdb ? -1
// else they are equal - return 0    
:
0;
 }).appendTo($tbody);
 }); </script>
  $queryx1 = mysql_query("select * from analytics_divequ where aum >= '$aum1' AND fiveyr >= '$fiveyr1' AND oneyr >= '$lastyr1' ", $linkx); 
echo '<table cellpadding="5" border="1px" align="center" id="caltbl"><thead><tr><th><b>Mutual Fund Scheme</b></th><th><b>AUM</b></th><th><b>Last Year %</b></th><th><b>Two Years %</b></th><th><b>Three Years %</b></th><th><b>5 Years %</b></th><th><b>Weight Score</b></th></tr></thead><tbody>';
$i=1;
while ($row2x = mysql_fetch_array($queryx1)) {
$mfsc=$row2x['mfscheme'];
$aum=$row2x['aum'];
$fiveyr=$row2x['fiveyr'];
$lastyr=$row2x['oneyr'];
$threeyr=$row2x['threeyr'];
$twoyr=$row2x['twoyr'];
$wscore=(($aum*$aumc1)+($fiveyr*$fiveyrc1)+($lastyr*$oneyrc1))/100;
?>
<tr><td><? echo $mfsc ?></td><td><? echo $aum ?></td><td><? echo $lastyr ?></td><td><? echo $twoyr ?></td><td><? echo $threeyr ?></td><td><? echo $fiveyr ?></td><td class="sortnr"><? echo $wscore ?></td></tr>
echo '</tbody></table> <br>';
?>

此外,我不能在这里使用Mysql,因为您可以看到最后一列不是数据库的一部分。

你能使用插件吗?

或者PHP来做你需要做的事情?如果数据存储在数据库中,您可以使用不同的MySQL语句,使用"ORDER BY"来完成所需的任务。。或者创建一个新的PHP数组,该数组将以正确的顺序包含数据,然后使用循环显示数据。(http://php.net/manual/en/function.usort.php)

对于表管理(排序、搜索等),我建议使用数据表(https://datatables.net)。从上次我搜索的时候起,这是市场上最好的表jquery插件。。。然而,根据我的经验,数据表无法处理大量数据,一旦有1000多行,页面加载可能需要10-20秒。

First Multi Dimensional用您的Mysql数据创建一个数组,并推送该数组中的最后一列值。之后,您可以用任何语言使用该数组进行排序。如果您将该数组更改为JSON,则可以使用JQUERY 轻松排序