【问题标题】:table sorter not working表格分拣机不工作
【发布时间】:2014-02-01 22:16:52
【问题描述】:

我正在尝试使用表格排序器对表格进行排序,但它无法正常工作

<html>
<head>
     <link rel="stylesheet" href="blueStyle.css" type="text/css" media="print, projection, screen" />
    <script src="http://tablesorter.com/jquery-latest.js" type="text/javascript"></script> 
    <script src="http://tablesorter.com/__jquery.tablesorter.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function() {      
        $("#myTable").tablesorter();
    }); 
    </script>
</head>
<body>
<table id="myTable" class="tablesorter" border="0" cellpadding="0" cellspacing="1"> 
<thead> 
<tr> 
    <th>Last Name</th> 
    <th>First Name</th> 
    <th>Email</th> 
    <th>Due</th> 
    <th>Web Site</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
    <td>Smith</td> 
    <td>John</td> 
    <td>jsmith@gmail.com</td> 
    <td>$50.00</td> 
    <td>http://www.jsmith.com</td> 
</tr> 
<tr> 
    <td>Bach</td> 
    <td>Frank</td> 
    <td>fbach@yahoo.com</td> 
    <td>$50.00</td> 
    <td>http://www.frank.com</td> 
</tr> 
<tr> 
    <td>Doe</td> 
    <td>Jason</td> 
    <td>jdoe@hotmail.com</td> 
    <td>$100.00</td> 
    <td>http://www.jdoe.com</td> 
</tr> 
<tr> 
    <td>Conway</td> 
    <td>Tim</td> 
    <td>tconway@earthlink.net</td> 
    <td>$50.00</td> 
    <td>http://www.timconway.com</td> 
</tr> 
</tbody> 
</table>
</body> 
 </html>   

样式似乎有效,但没有排序。

这就是当前表格的样子。

【问题讨论】:

    标签: javascript jquery html tablesorter


    【解决方案1】:

    你忘了关闭脚本标签&lt;/script&gt;

    <script src="http://tablesorter.com/__jquery.tablesorter.min.js" type="text/javascript"/>
    

    正确的标记:

    <script src="http://tablesorter.com/__jquery.tablesorter.min.js" type="text/javascript"/></script>
    

    我复制了你的代码,添加了&lt;/script&gt;,它对我有用。同样来自http://tablesorter.com/jquery-latest.js 的 jquery 文件确实已经过时(2008 年),请考虑使用其他 cdn,例如 //code.jquery.com/jquery-1.10.2.min.js 或 googleapis

    【讨论】:

    • 即使我正确关闭也没有任何反应,结果相同
    • 这很奇怪,你能把完整的代码粘贴过来,让我复制粘贴再检查一遍吗?
    • 我只是复制了您的固定代码,无需更改任何内容,它对我有用。有和没有 CSS
    • 现在我将我的 css 链接更改为网站链接,它突然起作用了,:S
    • 我遇到了同样的问题,结果我错过了在我的表格中使用 thead 和 tbody html 标签。显然 tablesorter 依赖于它们。
    【解决方案2】:

    您需要使用 tablesorter 选项进行自定义。这是示例http://jsfiddle.net/dhana36/BKgue/184/

    $('.tablesorter').tablesorter({
        // *** Appearance ***
        // fix the column widths
        widthFixed : true,
        // include zebra and any other widgets, options:
        // 'uitheme', 'filter', 'stickyHeaders' & 'resizable'
        // the 'columns' widget will require custom css for the
        // primary, secondary and tertiary columns
        widgets    : [ 'uitheme', 'zebra' ],
        // other options: "ddmmyyyy" & "yyyymmdd"
        dateFormat : "mmddyyyy",
    
        // *** Functionality ***
        // starting sort direction "asc" or "desc"
        sortInitialOrder : "asc",
        // These are detected by default,
        // but you can change or disable them
        headers : {
            // set "sorter : false" (no quotes) to disable the column
            0: { sorter: "text" },
            1: { sorter: "digit" },
            2: { sorter: "text" },
            3: { sorter: "url" }
        },
        // extract text from the table - this is how is
        // it done by default
        textExtraction : {
            0: function(node) { return $(node).text(); },
            1: function(node) { return $(node).text(); }
        },
        // forces the user to have this/these column(s) sorted first
        sortForce : null,
        // initial sort order of the columns
        // [[columnIndex, sortDirection], ... ]
        sortList : [[3,0]],
        // default sort that is added to the end of the users sort
        // selection.
        sortAppend : null,
        // Use built-in javascript sort - may be faster, but does not
        // sort alphanumerically
        sortLocaleCompare : false,
        // Setting this option to true will allow you to click on the
        // table header a third time to reset the sort direction.
        sortReset: false,
        // Setting this option to true will start the sort with the
        // sortInitialOrder when clicking on a previously unsorted column.
        sortRestart: false,
        // The key used to select more than one column for multi-column
        // sorting.
        sortMultiSortKey : "shiftKey",
    
        // *** Customize header ***
        onRenderHeader  : function() {
            // the span wrapper is added by default
            $(this).find('span').addClass('headerSpan');
        },
        // jQuery selectors used to find the header cells.
        selectorHeaders : 'thead th',
    
        // *** css classes to use ***
        cssAsc        : "headerSortUp",
        cssChildRow   : "expand-child",
        cssDesc       : "headerSortDown",
        cssHeader     : "header",
        tableClass    : 'tablesorter',
    
        // *** widget css class settings ***
        // column classes applied, and defined in the skin
        widgetColumns : { css: ["primary", "secondary", "tertiary"] },
        // find these jQuery UI class names by hovering over the
        // Framework icons on this page:
        // http://jqueryui.com/themeroller/
        widgetUitheme : { css: [
            "ui-icon-arrowthick-2-n-s", // Unsorted icon
            "ui-icon-arrowthick-1-s",   // Sort up (down arrow)
            "ui-icon-arrowthick-1-n"    // Sort down (up arrow)
            ]
        },
        // pick rows colors to match ui theme
        widgetZebra: { css: ["ui-widget-content", "ui-state-default"] },
    
        // *** prevent text selection in header ***
        cancelSelection : true,
    
        // *** send messages to console ***
        debug : false
    });
    

    【讨论】:

    • widgetColumnswidgetUItheme 选项不是原始版本的 tablesorter (v2.0.5) 的一部分;并在我的fork of tablesorter 中弃用
    • 嗯,不,你不需要它的选项来工作。它将使用默认设置。
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    相关资源
    最近更新 更多