1 |
|
2 |
|
3 |
/** |
4 |
* Generate the node required for user display length changing |
5 |
* @param {object} oSettings dataTables settings object |
6 |
* @returns {node} Display length feature node |
7 |
* @memberof DataTable#oApi |
8 |
*/ |
9 |
function _fnFeatureHtmlLength ( oSettings ) |
10 |
{ |
11 |
if ( oSettings.oScroll.bInfinite ) |
12 |
{ |
13 |
return null; |
14 |
} |
15 |
|
16 |
/* This can be overruled by not using the _MENU_ var/macro in the language variable */ |
17 |
var sName = 'name="'+oSettings.sTableId+'_length"'; |
18 |
var sStdMenu = '<select size="1" '+sName+'>'; |
19 |
var i, iLen; |
20 |
var aLengthMenu = oSettings.aLengthMenu; |
21 |
|
22 |
if ( aLengthMenu.length == 2 && typeof aLengthMenu[0] === 'object' && |
23 |
typeof aLengthMenu[1] === 'object' ) |
24 |
{ |
25 |
for ( i=0, iLen=aLengthMenu[0].length ; i<iLen ; i++ ) |
26 |
{ |
27 |
sStdMenu += '<option value="'+aLengthMenu[0][i]+'">'+aLengthMenu[1][i]+'</option>'; |
28 |
} |
29 |
} |
30 |
else |
31 |
{ |
32 |
for ( i=0, iLen=aLengthMenu.length ; i<iLen ; i++ ) |
33 |
{ |
34 |
sStdMenu += '<option value="'+aLengthMenu[i]+'">'+aLengthMenu[i]+'</option>'; |
35 |
} |
36 |
} |
37 |
sStdMenu += '</select>'; |
38 |
|
39 |
var nLength = document.createElement( 'div' ); |
40 |
if ( !oSettings.aanFeatures.l ) |
41 |
{ |
42 |
nLength.id = oSettings.sTableId+'_length'; |
43 |
} |
44 |
nLength.className = oSettings.oClasses.sLength; |
45 |
nLength.innerHTML = '<label>'+oSettings.oLanguage.sLengthMenu.replace( '_MENU_', sStdMenu )+'</label>'; |
46 |
|
47 |
/* |
48 |
* Set the length to the current display length - thanks to Andrea Pavlovic for this fix, |
49 |
* and Stefan Skopnik for fixing the fix! |
50 |
*/ |
51 |
$('select option[value="'+oSettings._iDisplayLength+'"]', nLength).attr("selected", true); |
52 |
|
53 |
$('select', nLength).bind( 'change.DT', function(e) { |
54 |
var iVal = $(this).val(); |
55 |
|
56 |
/* Update all other length options for the new display */ |
57 |
var n = oSettings.aanFeatures.l; |
58 |
for ( i=0, iLen=n.length ; i<iLen ; i++ ) |
59 |
{ |
60 |
if ( n[i] != this.parentNode ) |
61 |
{ |
62 |
$('select', n[i]).val( iVal ); |
63 |
} |
64 |
} |
65 |
|
66 |
/* Redraw the table */ |
67 |
oSettings._iDisplayLength = parseInt(iVal, 10); |
68 |
_fnCalculateEnd( oSettings ); |
69 |
|
70 |
/* If we have space to show extra rows (backing up from the end point - then do so */ |
71 |
if ( oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay() ) |
72 |
{ |
73 |
oSettings._iDisplayStart = oSettings.fnDisplayEnd() - oSettings._iDisplayLength; |
74 |
if ( oSettings._iDisplayStart < 0 ) |
75 |
{ |
76 |
oSettings._iDisplayStart = 0; |
77 |
} |
78 |
} |
79 |
|
80 |
if ( oSettings._iDisplayLength == -1 ) |
81 |
{ |
82 |
oSettings._iDisplayStart = 0; |
83 |
} |
84 |
|
85 |
_fnDraw( oSettings ); |
86 |
} ); |
87 |
|
88 |
|
89 |
$('select', nLength).attr('aria-controls', oSettings.sTableId); |
90 |
|
91 |
return nLength; |
92 |
} |
93 |
|
94 |
|
95 |
/** |
96 |
* Recalculate the end point based on the start point |
97 |
* @param {object} oSettings dataTables settings object |
98 |
* @memberof DataTable#oApi |
99 |
*/ |
100 |
function _fnCalculateEnd( oSettings ) |
101 |
{ |
102 |
if ( oSettings.oFeatures.bPaginate === false ) |
103 |
{ |
104 |
oSettings._iDisplayEnd = oSettings.aiDisplay.length; |
105 |
} |
106 |
else |
107 |
{ |
108 |
/* Set the end point of the display - based on how many elements there are |
109 |
* still to display |
110 |
*/ |
111 |
if ( oSettings._iDisplayStart + oSettings._iDisplayLength > oSettings.aiDisplay.length || |
112 |
oSettings._iDisplayLength == -1 ) |
113 |
{ |
114 |
oSettings._iDisplayEnd = oSettings.aiDisplay.length; |
115 |
} |
116 |
else |
117 |
{ |
118 |
oSettings._iDisplayEnd = oSettings._iDisplayStart + oSettings._iDisplayLength; |
119 |
} |
120 |
} |
121 |
} |