jQuery.tablesorter html表格排序插件

来源:互联网 发布:origin软件怎么安装 编辑:程序博客网 时间:2024/05/16 18:40


使用jQuery tablesort实现html表格方法:

1. 下载jQuery和tablesort脚本,并在html中引用该脚本文件:

<script type="text/javascript" src="../jquery.tablesorter.js"></script>

2. 格式化需要排序的html表格:

a. 给需要排序的表格指定CCS类:class="sorttable"

b. 使用thead和tbody标签来标记表格头和表格正文

c. 在表格头thead中需要使用th标签定义表头

3. 添加jQuery代码,启用html表格排序:

在body中对在第二步中指定的排序表格css类调用tablesorter()函数:

<script type="text/javascript">
    $(document).ready(function() {
        $(".sorttable").tablesorter(); 
    });
</script>

通过上面的3步,jQuery tablesort就可以实现对html表格的排序



===================



Getting started

To use the tablesorter plugin, include the jQuery library and the tablesorter plugin inside the <head> tag of your HTML document:

<script type="text/javascript" src="/path/to/jquery-latest.js"></script> <script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script> 

tablesorter works on standard HTML tables. You must include THEAD and TBODY tags:

<table id="myTable" class="tablesorter"> <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>     

Start by telling tablesorter to sort your table when the document is loaded:

$(document).ready(function()     {         $("#myTable").tablesorter();     } );     

Click on the headers and you'll see that your table is now sortable! You can also pass in configuration options when you initialize the table. This tells tablesorter to sort on the first and second column in ascending order.

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

NOTE! tablesorter will auto-detect most data types including numbers, dates, ip-adresses for more information see Examples





Configuration

tablesorter has many options you can pass in at initialization to achieve different effects
TIP! Click on the link in the property column to reveal full details (or toggle|show|hide all) or double click to update the browser location.
Property
Type
Default
Description
Link
PropertyTypeDefaultDescriptionLinkcancelSelectionBooleantrueIndicates if tablesorter should disable selection of text in the table header (TH). Makes header behave more like a button. cssAscString""Additional CSS class applied to style the header with a ascending sort (v2.11). cssChildRowString"tablesorter-childRow"Add this css class to a child row that should always be attached to its parent. Click on the "cssChildRow" link to toggle the view on the attached child row. Previous default was "expand-child" (Modified v2.4).1 2cssDescString""Additional CSS class applied to style the header with a descending sort (v2.11). cssHeaderString""Additional CSS class applied to style the headers (v2.11). cssHeaderRowString""Additional CSS class applied to style the header row (v2.11). cssIconString"tablesorter-icon"The CSS style used to style the header cell icon (modified v2.7). cssIconAscString""The CSS style added to the header cell icon when the column has an ascending sort (v2.18.3). cssIconDescString""The CSS style used to style the header cell icon when the column has a descending sort (v2.18.3) cssIconNoneString""The CSS style used to style the header cell icon when the column does not have a sort applied (v2.18.3) cssIgnoreRowString"tablesorter-ignoreRow"Class name to add to a table header row if you want all cells within this row to be ignored (v2.18.4). cssInfoBlockString"tablesorter-infoOnly"All tbodies with this class name will not have its contents sorted. (v2.2). cssNoneString""Additional CSS class applied to style the header when no sort is applied (v2.15). cssNoSortString"tablesorter-noSort"Class name added to element inside header. Clicking on that element, or any elements within it won't cause a sort. (New v2.20.0). cssProcessingString""Additional CSS class applied to style the header cell while it is being sorted or filtered (v2.4; v2.11). dateFormatString"mmddyyyy"Set the date format. Here are the available options. (Modified v2.0.23).ExampledebugBooleanfalseBoolean flag indicating if tablesorter should display debuging information useful for development.ExampledelayInitBooleanfalseSetting this option to true will delay parsing of all table cell data until the user initializes a sort. This speeds up the initialization process of very large tables, but the data still needs to be parsed, so the delay is still present upon initial sort.ExampleemptyToString"bottom"Option indicating how tablesorter should deal with empty table cells. (Modified v2.1.16, v2.16.2).ExampleheadersObjectnullAn object of instructions for per-"header cell" controls in the format: headers: { 0: { option: setting }, ... } (v2.17.1docs updated1 2 3 4 5headerTemplateString"{content}"This is a template string which allows adding additional content to the header while it is being built (v2.7; v2.17.8).ExampleignoreCaseBooleantrueWhen true, text sorting will ignore the character case. If false, upper case characters will sort before lower case. (v2.2). imgAttrString"alt"Used by the image parser to grab the image attribute content (v2.17.5; moved to tablesorter core in v2.18.0; see config.parsers). initializedFunctionnullThis callback fires when tablesorter has completed initialization. (v2.2). initWidgetsBooleantrueApply widgets after table initializes (v2.3.5). namespaceStringundefinedThis option should contain a unique namespace for each table; it is used when binding to event listeners (v2.15.7). numberSorterFunctionnullReplace the default number sorting algorithm with a custom one using this option (v2.12). onRenderHeaderFunctionnullThis function is called after content is to the TH tags (after the template is procressed and added). You can use this to modify the HTML in each header tag for additional styling (v2.18.0).ExampleonRenderTemplateFunctionnullThis function is called after the template string has been built, but before the template string is applied to the header and before theonRenderHeader function is called (v2.7).ExampleresortBooleantrueWhen this option is true any applied sort on the table will be reapplied after an update method (New v2.19.0). selectorHeadersString"> thead th, > thead td"jQuery selectors used to find cells in the header. selectorRemoveString"tr.remove-me"This CSS class name can be applied to all rows that are to be removed prior to triggering a table update. (v2.1). selectorSortString"th, td"jQuery selector of content within selectorHeaders that is clickable to trigger a sort (v2.4).ExampleserverSideSortingBooleanfalseSet to true if the server is performing the sorting. The ui and events will still be used (v2.5.3). showProcessingBooleanfalseShow an indeterminate timer icon in the header when the table is sorted or filtered. Please note that due to javascript processing, the icon may not show as being animated. I'm looking into this further and would appreciate any feedback or suggestions with the coding (v2.4).ExamplesortAppendArraynullUse to add an additional forced sort that will be appended to the dynamic selections by the user.ExamplesortForceArraynullUse to add an additional forced sort that is prepended to sortList.ExamplesortInitialOrderString"asc"This sets the direction a column will sort when clicking on the header for the first time. Valid arguments are "asc" for Ascending or "desc" for Descending.
1 2sortListArraynullUse to add an initial sort to the table.ExamplesortLocaleCompareBooleanfalseBoolean flag indicating if certain accented characters within the table will be replaced with their equivalent characters. (Modified v2.2).ExamplesortMultiSortKeyString"shiftKey"The key used to select more than one column for multi-column sorting. Defaults to the Shift key. The other options are "ctrlKey" or "altKey"(reference).ExamplesortResetBooleanfalseSetting this option to true will allow you to click on the table header a third time to reset the sort direction. (v2.0.27).ExamplesortResetKeyString"ctrlKey"The key used to reset sorting on the entire table. Defaults to the control key. The other options are "shiftKey" or "altKey" (reference). sortRestartBooleanfalseSetting this option to true will start the sort with the sortInitialOrder when clicking on a previously unsorted column. (v2.0.31).ExamplesortStableBooleanfalseSetting this option to true and sorting two rows with exactly the same content, the original sort order is maintained (v2.14). stringToString"max"A key word indicating how tablesorter should deal with text inside of numerically sorted columns. (v2.1.16).ExampletabIndexBooleantrueAdd a tabindex to the headers for keyboard accessibility; this was previously always applied (v2.14). tableClassString""Additional CSS class applied to style the table (v2.11). textAttributeString"data-text"This data-attribute can be added to any tbody cell and can contains alternate cell text (v2.16.0). textExtractionMultiple*"basic"Defines which method is used to extract data from a table cell for sorting (Updated v2.19.0)ExampletextSorterFunctionnullReplace the default sorting algorithm with a custom one using this option (v2.12) - *NOTE* The parameters have changed!!.1 2themeString"default"This option will add a theme css class name to the table "tablesorter-{theme}" for styling (v2.4; v2.18.0).ExampleusNumberFormatBooleantrueIndicates how tablesorter should deal with a numerical format: (v2.1.3).ExamplewidgetClassString'widget-{name}'When the table has a class name that matches the template and a widget id that matches the {name}, the widget will automatically be added to the table (v2.18.0) widgetOptionsObject{ }In version 2.1, all widget options have been moved into this option. This is a move to store all widget specific options in one place so as not to polute the main table options. All current widgets have been modified to use this new option. (v2.1).ExamplewidgetsArray[ ] (empty array)Initialize widgets using this option ( e.g. widgets : ['zebra'], or custom widgets widgets: ['zebra', 'myCustomWidget'];, see this demo on how to write your own custom widget ).ExamplewidthFixedBooleanfalseIndicates if tablesorter should apply fixed percentage-based widths to the table columns (modified v2.4).ExampleUtility OptionsdataObject, ArrayundefinedStorage for processed table build widget data (array, object, string) (v2.11).ExampledateRangeNumeric50Used by the two digit year parser to set the date range (v2.14).ExamplefixedUrlStringundefinedThis option was added to set a specific page when storing data using the $.tablesorter.storage code (v2.12). Deprecated/Removed OptionscssAllowClicksString This option was removed! It has been replaced by cssNoSort which does the opposite of what this class name was supposed to do. widgetColumns  This option is being deprecated! It has been replaced by widgetOptions.columns; but is still available for backwards compatibility. widgetUitheme  This option is being deprecated! It has been replaced by widgetOptions.uitheme; but is still available for backwards compatibility. widgetZebra  This option is being deprecated! It has been replaced by widgetOptions.zebra; but is still available for backwards compatibility. 
Property
Type
Default
Description
Link


0 0
原创粉丝点击