| 2 |
lars |
1 |
/*! Page length control via links for DataTables
|
|
|
2 |
* 2014 SpryMedia Ltd - datatables.net/license
|
|
|
3 |
*/
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* @summary LengthLinks
|
|
|
7 |
* @description Page length control via links for DataTables
|
|
|
8 |
* @version 1.1.0
|
|
|
9 |
* @file dataTables.searchHighlight.js
|
|
|
10 |
* @author SpryMedia Ltd (www.sprymedia.co.uk)
|
|
|
11 |
* @contact www.sprymedia.co.uk/contact
|
|
|
12 |
* @copyright Copyright 2014 SpryMedia Ltd.
|
|
|
13 |
*
|
|
|
14 |
* License MIT - http://datatables.net/license/mit
|
|
|
15 |
*
|
|
|
16 |
* This feature plug-in for DataTables adds page length control links to the
|
|
|
17 |
* DataTable. The `dom` option can be used to insert the control using the `L`
|
|
|
18 |
* character option and it uses the `lengthMenu` options of DataTables to
|
|
|
19 |
* determine what to display.
|
|
|
20 |
*
|
|
|
21 |
* @example
|
|
|
22 |
* $('#myTable').DataTable( {
|
|
|
23 |
* dom: 'Lfrtip'
|
|
|
24 |
* } );
|
|
|
25 |
*
|
|
|
26 |
* @example
|
|
|
27 |
* $('#myTable').DataTable( {
|
|
|
28 |
* lengthMenu: [ [10, 25, 50, -1], [10, 25, 50, "All"] ]
|
|
|
29 |
* dom: 'Lfrtip'
|
|
|
30 |
* } );
|
|
|
31 |
*/
|
|
|
32 |
|
|
|
33 |
(function(window, document, $, undefined) {
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
$.fn.dataTable.LengthLinks = function ( inst ) {
|
|
|
37 |
var api = new $.fn.dataTable.Api( inst );
|
|
|
38 |
var settings = api.settings()[0];
|
|
|
39 |
var container = $('<div></div>').addClass( settings.oClasses.sLength );
|
|
|
40 |
var lastLength = -1;
|
|
|
41 |
|
|
|
42 |
// API so the feature wrapper can return the node to insert
|
|
|
43 |
this.container = function () {
|
|
|
44 |
return container[0];
|
|
|
45 |
};
|
|
|
46 |
|
|
|
47 |
// Listen for events to change the page length
|
|
|
48 |
container.on( 'click.dtll', 'a', function (e) {
|
|
|
49 |
e.preventDefault();
|
|
|
50 |
api.page.len( $(this).data('length')*1 ).draw( false );
|
|
|
51 |
} );
|
|
|
52 |
|
|
|
53 |
// Update on each draw
|
|
|
54 |
api.on( 'draw', function () {
|
|
|
55 |
// No point in updating - nothing has changed
|
|
|
56 |
if ( api.page.len() === lastLength ) {
|
|
|
57 |
return;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
var menu = settings.aLengthMenu;
|
|
|
61 |
var lang = menu.length===2 && $.isArray(menu[0]) ? menu[1] : menu;
|
|
|
62 |
var lens = menu.length===2 && $.isArray(menu[0]) ? menu[0] : menu;
|
|
|
63 |
|
|
|
64 |
var out = $.map( lens, function (el, i) {
|
|
|
65 |
return el == api.page.len() ?
|
|
|
66 |
'<a class="active" data-length="'+lens[i]+'">'+lang[i]+'</a>' :
|
|
|
67 |
'<a data-length="'+lens[i]+'">'+lang[i]+'</a>';
|
|
|
68 |
} );
|
|
|
69 |
|
|
|
70 |
container.html( settings.oLanguage.sLengthMenu.replace( '_MENU_', out.join(' | ') ) );
|
|
|
71 |
lastLength = api.page.len();
|
|
|
72 |
} );
|
|
|
73 |
|
|
|
74 |
api.on( 'destroy', function () {
|
|
|
75 |
container.off( 'click.dtll', 'a' );
|
|
|
76 |
} );
|
|
|
77 |
};
|
|
|
78 |
|
|
|
79 |
// Subscribe the feature plug-in to DataTables, ready for use
|
|
|
80 |
$.fn.dataTable.ext.feature.push( {
|
|
|
81 |
"fnInit": function( settings ) {
|
|
|
82 |
var l = new $.fn.dataTable.LengthLinks( settings );
|
|
|
83 |
return l.container();
|
|
|
84 |
},
|
|
|
85 |
"cFeature": "L",
|
|
|
86 |
"sFeature": "LengthLinks"
|
|
|
87 |
} );
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
})(window, document, jQuery);
|