2012-05-30 41 views
0

我有动态后点击搜索按钮如下生成表:排序的所有列标题

puts "<table class=\"resultsTable\">" 
     puts "<tr><th colspan=\"10\" class=\"head\">Search Results</th></tr>" 
     puts "<tr>" 
     puts "<th></th>" 
     puts "<th>App</th>" 
     puts "<th>Name</th>" 
     puts "<th>Region</th>" 
     puts "<th>Market</th>" 
     puts "<th>Language</th>" 
     puts "<th>Function</th>" 
     puts "<th>LOB</th>" 
     puts "<th>Term</th>" 
     puts "<th>Call</th>" 
     puts "</tr>" 
     puts "<tr>" 
     puts "<td id=\"$cellID\">" 
     puts "<img src=\"images/magnifier.gif\" style=\"cursor:pointer\" onclick=\"showRouting({'spec':'${specific}', 'id':'${mkt_id}', 'name':'${mkt_name}', 'xfer':'${xfertype}', 'cell':'${cellID}'})\"</img>" 
     puts "</td>" 
     puts "<td>$level</td>" 
     puts "<td>$name</td>" 
     puts "<td>$reg_name</td>" 
     puts "<td>$link</td>" 
     puts "<td>$lang</td>" 
     puts "<td>$func</td>" 
     puts "<td>$lob</td>" 
     puts "<td>$term</td>" 
     puts "<td>$call</td>" 
     puts "</tr>" 

我能有某种排序启用,这样我可以通过每列进行排序(由应用程式,姓名等)?我看过某种jQuery表格分拣机和其他东西,但是我无法用我的代码做到这一点。有人能告诉我如何去做这件事吗?

[0,0] [1,0]是什么意思?我将如何修改我的代码,因此?:

$(document).ready(function() 
    { 
     $("#myTable").tablesorter({sortList: [[0,0], [1,0]]}); 
    } 
); 
+1

你必须使用jQuery或什么的。 Twitter Bootstrap内置了表格排序功能。我会建议检查一下。 –

+0

正如baudday所说..真的没有办法使用jQuery/JavaScript对表进行排序。 –

回答

2

您可以使用http://datatables.net/http://tablesorter.com/docs/#Demo

对于建立这样

台分拣机使用外挂
// call the tablesorter plugin 
    $("table").tablesorter({ 
     // sort on the first column and third column, order asc 
     sortList: [[0,0],[2,0]] 
    }); 

其中sortList:[[0,0],[2,0]]包含列索引(基于0)和排序顺序。

因此[[0,0],[2,0]]表示第一列和第三列是可排序的,最初它们将按升序排序。

检查此working fiddle选项已被正确评论。

here是一个非常基本的版本,其中所有表格列可排序但没有图形例如箭头或背景。

+0

我是否必须下载该插件并引用我的本地路径或可以链接到外部路径?你能否告诉我还有什么其他jquery路径需要引用? – newbie

+0

我刚刚编辑了关于tablesorter的问题 – newbie

+0

最好将js下载到本地机器并链接到它们。你可以链接到他们的js进行快速设置,但这并不好。 –