javascript - 按所有列标题排序

标签 javascript jquery jquery-plugins

我有一个在单击搜索按钮时动态生成的表格,如下所示:

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]]} ); 
    } 
); 

最佳答案

您可以使用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这是一个非常基本的版本,其中所有表格列都可以排序,但没有图形,例如箭头或背景。

关于javascript - 按所有列标题排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10819220/

相关文章:

javascript - Highchart,一列作为另一列的背景

javascript - 使用javascript从外部页面获取html元素

javascript - 如何创建简单的 jQuery 插件?

javascript - 如何在 jQuery 插件的用户定义方法中调用元素

javascript - 时间戳二月显示为三月

javascript - 如何在按钮点击时切换元素并在外部点击时关闭?

javascript - 修改 Javascript 中的文本

jquery - Laravel DELETE 表单外请求 - 获取数据

javascript - 比较 jQuery 中的 2 个元素(按内容)

php - 使用 jQuery-File-Upload、Apache 和 PHP 上传 4GB HTTP 文件