diff --git a/README.md b/README.md index 4afb217..4bc0782 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ -Plugins -======= +DataTables Plugins +================== -Plug-ins for DataTables \ No newline at end of file +This repository contains a collection of plug-ins for the jQuery [DataTables](http://datatables.net) table enhancer. These plug-ins are feature enhancing for the DataTables library, adding extra options to core functionality such as additional sort algorithms, API methods and pagination controls. The plug-ins should not be confused with DataTables "extras" which are more significant software libraries which add additional features to DataTables. + +* Sorting plug-ins +* \ No newline at end of file diff --git a/api/index.html b/api/index.html new file mode 100644 index 0000000..fc39dc3 --- /dev/null +++ b/api/index.html @@ -0,0 +1,36 @@ + +

Custom API functions

+ +

One of the most common interactions with DataTables for a developer (other than initialisation of the table of course!) is to make use of the API functions provided by DataTables. While allowing for a fairly extensive range of code interactions, the default API set can be greatly enhanced by making use of the functions provided below, as suitable for your application.

+ + + + + +

How to use DataTables plug-in API functions

+ +

To make use of one of the plug-in API functions below, you simply need to include it in the Javascript available for your page, after you load the DataTables library, but before you initialise the DataTable. After that, you will be able to initialise the table, and call the function on the resulting object. As an example the code below makes use of fnGetHiddenNodes saved into a file (live example):

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript" src="dataTables.fnGetHiddenNodes.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		var oTable = $('#example').dataTable();
+		
+		$('#button').click( function () {
+			var nHidden = oTable.fnGetHiddenNodes();
+			alert( nHidden.length +' nodes were returned' );
+		} );
+	} );
+</script>
+
+ +

Please note that DataTables will automatically pass the settings object as the first parameter. As such, you do not need to pass the settings object, which you will see if you look at the plug-in API's code.

+ + +

Plug-in API functions

+ + diff --git a/filtering/index.html b/filtering/index.html new file mode 100644 index 0000000..c16867e --- /dev/null +++ b/filtering/index.html @@ -0,0 +1,77 @@ + +

Filtering plug-in functions

+ +

The filtering plug-in options that DataTables provides are remarkably powerful, and and let you set almost any filtering criterion you wish for user based input. A couple of things to note for filtering, firstly you will likely need to customise the filtering presented on this page to match your specific needs. Secondly, if you are using server-side processing, DataTables doesn't do any client-side filtering, so these plug-ins will not have any effect (with server-side processing, all data manipulation is done by the server - so you would need to implement these filters there).

+ +

DataTables supports two different kinds of plug-in filtering methods:

+ + + + + +

How to use DataTables plug-in column type based filtering

+ +

To make use of the column (type) based filtering plug-in functions below, you need to include it in the Javascript available for your page, after you load the DataTables library, but before you initialise the DataTable. You must also set the column type for the column(s) that you wish to apply the filter to using sType.

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript" src="dataTables.htmlColumnFilter.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		var oTable = $('#example').dataTable({
+			"aoColumns": [
+				"sType": "html",
+				null
+			]
+		});
+	} );
+</script>
+
+ + + +

Plug-in column type filtering functions

+ + + + + + +

How to use DataTables plug-in row filtering functions

+ +

To add the functionality provided by the filtering functions below, you simply need to include it in the Javascript available for your page, after you load the DataTables library, but before you initialise the DataTable. These filters are global and will be applied whenever DataTables applies it's own filtering (for details please see the filters development page).

+ +

In the following example the range filtering (numbers) plug-in is saved to a file, and used in the DataTable which is initialised. Note also that event listeners are applied to the two inputs, which will cause the table to redraw, and thus filter the new data (live example):

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript" src="dataTables.rangeFilter.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		var oTable = $('#example').dataTable();
+		
+		/* Add event listeners to the two range filtering inputs */
+		$('#min').keyup( function() { oTable.fnDraw(); } );
+		$('#max').keyup( function() { oTable.fnDraw(); } );
+	} );
+</script>
+
+ + + +

Plug-in row filtering functions

+ + + + diff --git a/i18n/index.html b/i18n/index.html new file mode 100644 index 0000000..136c0a6 --- /dev/null +++ b/i18n/index.html @@ -0,0 +1,33 @@ + +

Internationalisation

+ +

Localisation of the presentation layer is important for any software package, and I aim to make this normally arduous task as easy as possible in DataTables. To this end, a number of contributors have kindly translated the language strings used is DataTables into various different languages. If you translate DataTables into any other languages, please let me know and I'll happily include the translation here.

+ + + + + +

How to use DataTables internalisation options

+ +

There are two methods by which you can include internalisation options in DataTables - loading the language file through an Ajax request, or at initialisation time using the oLanguage property. The following example shows how to include the German translation as an Ajax file (live example - a live example for oLanguage control is also available):

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		$('#example').dataTable( {
+			"oLanguage": {
+				"sUrl": "dataTables.german.txt"
+			}
+		} );
+	} );
+</script>
+
+ + + +

Translations

+ + diff --git a/integration/bootstrap/dataTables.bootstrap.css b/integration/bootstrap/dataTables.bootstrap.css new file mode 100644 index 0000000..d9993d4 --- /dev/null +++ b/integration/bootstrap/dataTables.bootstrap.css @@ -0,0 +1,48 @@ + +div.dataTables_length label { + float: left; + text-align: left; +} + +div.dataTables_length select { + width: 75px; +} + +div.dataTables_filter label { + float: right; +} + +div.dataTables_info { + padding-top: 8px; +} + +div.dataTables_paginate { + float: right; + margin: 0; +} + +table.table { + clear: both; + margin-bottom: 6px !important; +} + +table.table thead .sorting, +table.table thead .sorting_asc, +table.table thead .sorting_desc, +table.table thead .sorting_asc_disabled, +table.table thead .sorting_desc_disabled { + cursor: pointer; + *cursor: hand; +} + +table.table thead .sorting { background: url('images/sort_both.png') no-repeat center right; } +table.table thead .sorting_asc { background: url('images/sort_asc.png') no-repeat center right; } +table.table thead .sorting_desc { background: url('images/sort_desc.png') no-repeat center right; } + +table.table thead .sorting_asc_disabled { background: url('images/sort_asc_disabled.png') no-repeat center right; } +table.table thead .sorting_desc_disabled { background: url('images/sort_desc_disabled.png') no-repeat center right; } + +table.dataTable th:active { + outline: none; +} + diff --git a/integration/bootstrap/dataTables.bootstrap.js b/integration/bootstrap/dataTables.bootstrap.js new file mode 100644 index 0000000..bd594a4 --- /dev/null +++ b/integration/bootstrap/dataTables.bootstrap.js @@ -0,0 +1,107 @@ +/* Set the defaults for DataTables initialisation */ +$.extend( true, $.fn.dataTable.defaults, { + "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>", + "sPaginationType": "bootstrap", + "oLanguage": { + "sLengthMenu": "_MENU_ records per page" + } +} ); + + +/* Default class modification */ +$.extend( $.fn.dataTableExt.oStdClasses, { + "sWrapper": "dataTables_wrapper form-inline" +} ); + + +/* API method to get paging information */ +$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings ) +{ + return { + "iStart": oSettings._iDisplayStart, + "iEnd": oSettings.fnDisplayEnd(), + "iLength": oSettings._iDisplayLength, + "iTotal": oSettings.fnRecordsTotal(), + "iFilteredTotal": oSettings.fnRecordsDisplay(), + "iPage": Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ), + "iTotalPages": Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength ) + }; +}; + + +/* Bootstrap style pagination control */ +$.extend( $.fn.dataTableExt.oPagination, { + "bootstrap": { + "fnInit": function( oSettings, nPaging, fnDraw ) { + var oLang = oSettings.oLanguage.oPaginate; + var fnClickHandler = function ( e ) { + e.preventDefault(); + if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) { + fnDraw( oSettings ); + } + }; + + $(nPaging).addClass('pagination').append( + '' + ); + var els = $('a', nPaging); + $(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler ); + $(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler ); + }, + + "fnUpdate": function ( oSettings, fnDraw ) { + var iListLength = 5; + var oPaging = oSettings.oInstance.fnPagingInfo(); + var an = oSettings.aanFeatures.p; + var i, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2); + + if ( oPaging.iTotalPages < iListLength) { + iStart = 1; + iEnd = oPaging.iTotalPages; + } + else if ( oPaging.iPage <= iHalf ) { + iStart = 1; + iEnd = iListLength; + } else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) { + iStart = oPaging.iTotalPages - iListLength + 1; + iEnd = oPaging.iTotalPages; + } else { + iStart = oPaging.iPage - iHalf + 1; + iEnd = iStart + iListLength - 1; + } + + for ( i=0, iLen=an.length ; i'+j+'') + .insertBefore( $('li:last', an[i])[0] ) + .bind('click', function (e) { + e.preventDefault(); + oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength; + fnDraw( oSettings ); + } ); + } + + // Add / remove disabled classes from the static elements + if ( oPaging.iPage === 0 ) { + $('li:first', an[i]).addClass('disabled'); + } else { + $('li:first', an[i]).removeClass('disabled'); + } + + if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) { + $('li:last', an[i]).addClass('disabled'); + } else { + $('li:last', an[i]).removeClass('disabled'); + } + } + } + } +} ); diff --git a/pagination/index.html b/pagination/index.html new file mode 100644 index 0000000..00fc1e6 --- /dev/null +++ b/pagination/index.html @@ -0,0 +1,33 @@ + +

Custom pagination types

+ +

The style of the pagination options that Datatables presents to the end-user can greatly effect the look and feel of your table, as well as, of course, the interaction behaviour. Through the plug-in options you can define your own paging function to create the interaction that you are looking for.

+ + + + + +

How to use DataTables plug-in pagination functions

+ +

To use a pagination plug-in, you must include the pagination plug-in code from the plug-ins available below, after you load the DataTables library, but before you initialise the DataTable. When initialising the DataTable, you must also tell it to make use of this plug-in, rather than using the default built-in types, by setting the 'sPaginationType' to the value required by the plug-in. As an example the code below makes use of the scrolling pagination plug-in saved into a file (live example):

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript" src="dataTables.scrollingPagination.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		$('#example').dataTable( {
+			"sPaginationType": "scrolling"
+		} );
+	} );
+</script>
+
+ + + +

Plug-in pagination functions

+ + + diff --git a/sorting/index.html b/sorting/index.html new file mode 100644 index 0000000..33c6398 --- /dev/null +++ b/sorting/index.html @@ -0,0 +1,84 @@ + +

Sorting plug-ins

+ +

DataTables provides two APIs for sorting information in a table: type based sorting and custom data source sorting. They can be used together or independently, and are fully described on the sorting development page. By far the most commonly used of these two types is "type based sorting" and is the one you are most likely to want to use if just starting out with DataTables.

+ + + + + +

How to use DataTables plug-in sorting functions functions (type based)

+ +

To add the ability to sort specific data types, using the plug-in functions below, you simply need to include the plug-in's code in the Javascript available for your page, after you load the DataTables library, but before you initialise the DataTable. Then using the sType parameter for that column, set it to the value needed for the plug-in. If sType is not given for a column, DataTables will attempt to detect the type automatically. The following example shows how the numeric comma sorting plug-in (saved to a file) can be used with a DataTable, sorting on the fourth column (live example):

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript" src="dataTables.numericComma.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		$('#example').dataTable( {
+			"aoColumns": [
+				null,
+				null,
+				null,
+				{ "sType": "numeric-comma" },
+				null
+			]
+		} );
+	} );
+</script>
+
+ + + +

Sorting functions (type based column sorting)

+ +

The main DataTables package includes sorting functions for string, date and numeric data, but you may very well wish to order data in some other manner (for example currency, formatting numbers, multi-part data etc). The sorting function pairs below provide a wealth of different sorting methods.

+ +

It is also worth noting that sorting function go hand-in-hand with type detection functions, and many of the function pairs below has a corresponding type detection function to make installation very easy.

+ + + + + + +

How to use custom data source sorting plug-ins

+ +

Custom data source sorting plug-ins complement type based sorting by adding a method to DataTables to retrieve data live from the DOM just prior to the table being sorted. As such, you can use type based sorting, in-combination with custom data source sorting. This is particularly useful if dealing with DOM information in a table which can change dynamically, such as form inputs, but it can add a little extra overhead to the sorting.

+ +

To make use of the plug-ins below, you simply need to include the plug-in's code in the Javascript available for your page, after you load the DataTables library, but before you initialise the DataTable. You also need to specify the sSortDataType parameter for the column, to tell it which plug-in function to use.

+ +

The example below shows the use of multiple custom data source plug-ins, and also it's use in-combination with sType (live example):

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript" src="dataTables.dataSourcePlugins.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		$('#example').dataTable( {
+			"aoColumns": [
+				null,
+				null,
+				{ "sSortDataType": "dom-text" },
+				{ "sSortDataType": "dom-text", "sType": "numeric" },
+				{ "sSortDataType": "dom-select" },
+				{ "sSortDataType": "dom-checkbox" }
+			]
+		} );
+	} );
+</script>
+
+ + + + +

Custom data source sorting

+ +

The custom data source functions are used to update the cached data in DataTables, so sorting can occur on columns with user input information.

+ + + + diff --git a/type-detection/index.html b/type-detection/index.html new file mode 100644 index 0000000..9129164 --- /dev/null +++ b/type-detection/index.html @@ -0,0 +1,30 @@ + +

Type detection

+ +

When a DataTable is initialised, each column is scanned automatically for the type of data it contains, which in turn allows DataTables to apply the require type of sorting function. There are three built-in types (string, date and numeric) but this can readily be expanded using the functions below. This can make installed a sorting plug-in much easier since you need not specify the sType for the column - it will be picked up automatically.

+ + + + +

How to use DataTables plug-in type detection functions

+ +

To use of one of the plug-in type detections functions below, you simply need to include it and its counterpart sorting function, in the Javascript available for your page, after you load the DataTables library, but before you initialise the DataTable. Then all you need to do is initialise the DataTable and the type will be automatically detected. As an example the code below makes use of the numeric comma type detection and sorting functions, saved into two different files for clarity (live example):

+ +
<script type="text/javascript" src="jquery.dataTables.js"></script>
+<script type="text/javascript" src="dataTables.numericCommaSort.js"></script>
+<script type="text/javascript" src="dataTables.numericCommaTypeDetect.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		$('#example').dataTable();
+	} );
+</script>
+
+ + + +

Plug-in type detection functions

+ +