| 4 |
lars |
1 |
/**
|
|
|
2 |
* Sometimes for quick navigation, it can be useful to allow an end user to
|
|
|
3 |
* enter which page they wish to jump to manually. This paging control uses a
|
|
|
4 |
* text input box to accept new paging numbers (arrow keys are also allowed
|
|
|
5 |
* for), and four standard navigation buttons are also presented to the end
|
|
|
6 |
* user.
|
|
|
7 |
*
|
|
|
8 |
* @name Navigation with text input
|
|
|
9 |
* @summary Shows an input element into which the user can type a page number
|
|
|
10 |
* @author [Allan Jardine](http://sprymedia.co.uk)
|
|
|
11 |
*
|
|
|
12 |
* @example
|
|
|
13 |
* $(document).ready(function() {
|
|
|
14 |
* $('#example').dataTable( {
|
|
|
15 |
* "sPaginationType": "input"
|
|
|
16 |
* } );
|
|
|
17 |
* } );
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
$.fn.dataTableExt.oPagination.input = {
|
|
|
21 |
"fnInit": function ( oSettings, nPaging, fnCallbackDraw )
|
|
|
22 |
{
|
|
|
23 |
var nFirst = document.createElement( 'span' );
|
|
|
24 |
var nPrevious = document.createElement( 'span' );
|
|
|
25 |
var nNext = document.createElement( 'span' );
|
|
|
26 |
var nLast = document.createElement( 'span' );
|
|
|
27 |
var nInput = document.createElement( 'input' );
|
|
|
28 |
var nPage = document.createElement( 'span' );
|
|
|
29 |
var nOf = document.createElement( 'span' );
|
|
|
30 |
|
|
|
31 |
nFirst.innerHTML = oSettings.oLanguage.oPaginate.sFirst;
|
|
|
32 |
nPrevious.innerHTML = oSettings.oLanguage.oPaginate.sPrevious;
|
|
|
33 |
nNext.innerHTML = oSettings.oLanguage.oPaginate.sNext;
|
|
|
34 |
nLast.innerHTML = oSettings.oLanguage.oPaginate.sLast;
|
|
|
35 |
|
|
|
36 |
nFirst.className = "paginate_button first disabled";
|
|
|
37 |
nPrevious.className = "paginate_button previous disabled";
|
|
|
38 |
nNext.className="paginate_button next";
|
|
|
39 |
nLast.className = "paginate_button last";
|
|
|
40 |
nOf.className = "paginate_of";
|
|
|
41 |
nPage.className = "paginate_page";
|
|
|
42 |
nInput.className = "paginate_input";
|
|
|
43 |
|
|
|
44 |
if ( oSettings.sTableId !== '' )
|
|
|
45 |
{
|
|
|
46 |
nPaging.setAttribute( 'id', oSettings.sTableId+'_paginate' );
|
|
|
47 |
nPrevious.setAttribute( 'id', oSettings.sTableId+'_previous' );
|
|
|
48 |
nPrevious.setAttribute( 'id', oSettings.sTableId+'_previous' );
|
|
|
49 |
nNext.setAttribute( 'id', oSettings.sTableId+'_next' );
|
|
|
50 |
nLast.setAttribute( 'id', oSettings.sTableId+'_last' );
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
nInput.type = "text";
|
|
|
54 |
nPage.innerHTML = "Page ";
|
|
|
55 |
|
|
|
56 |
nPaging.appendChild( nFirst );
|
|
|
57 |
nPaging.appendChild( nPrevious );
|
|
|
58 |
nPaging.appendChild( nPage );
|
|
|
59 |
nPaging.appendChild( nInput );
|
|
|
60 |
nPaging.appendChild( nOf );
|
|
|
61 |
nPaging.appendChild( nNext );
|
|
|
62 |
nPaging.appendChild( nLast );
|
|
|
63 |
|
|
|
64 |
$(nFirst).click( function ()
|
|
|
65 |
{
|
|
|
66 |
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
|
|
|
67 |
if (iCurrentPage != 1)
|
|
|
68 |
{
|
|
|
69 |
oSettings.oApi._fnPageChange( oSettings, "first" );
|
|
|
70 |
fnCallbackDraw( oSettings );
|
|
|
71 |
$(nFirst).addClass('disabled');
|
|
|
72 |
$(nPrevious).addClass('disabled');
|
|
|
73 |
$(nNext).removeClass('disabled');
|
|
|
74 |
$(nLast).removeClass('disabled');
|
|
|
75 |
}
|
|
|
76 |
} );
|
|
|
77 |
|
|
|
78 |
$(nPrevious).click( function()
|
|
|
79 |
{
|
|
|
80 |
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
|
|
|
81 |
if (iCurrentPage != 1)
|
|
|
82 |
{
|
|
|
83 |
oSettings.oApi._fnPageChange(oSettings, "previous");
|
|
|
84 |
fnCallbackDraw(oSettings);
|
|
|
85 |
if (iCurrentPage == 2)
|
|
|
86 |
{
|
|
|
87 |
$(nFirst).addClass('disabled');
|
|
|
88 |
$(nPrevious).addClass('disabled');
|
|
|
89 |
}
|
|
|
90 |
$(nNext).removeClass('disabled');
|
|
|
91 |
$(nLast).removeClass('disabled');
|
|
|
92 |
}
|
|
|
93 |
} );
|
|
|
94 |
|
|
|
95 |
$(nNext).click( function()
|
|
|
96 |
{
|
|
|
97 |
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
|
|
|
98 |
if (iCurrentPage != Math.ceil((oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)))
|
|
|
99 |
{
|
|
|
100 |
oSettings.oApi._fnPageChange(oSettings, "next");
|
|
|
101 |
fnCallbackDraw(oSettings);
|
|
|
102 |
if (iCurrentPage == (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength) - 1))
|
|
|
103 |
{
|
|
|
104 |
$(nNext).addClass('disabled');
|
|
|
105 |
$(nLast).addClass('disabled');
|
|
|
106 |
}
|
|
|
107 |
$(nFirst).removeClass('disabled');
|
|
|
108 |
$(nPrevious).removeClass('disabled');
|
|
|
109 |
}
|
|
|
110 |
} );
|
|
|
111 |
|
|
|
112 |
$(nLast).click( function()
|
|
|
113 |
{
|
|
|
114 |
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
|
|
|
115 |
if (iCurrentPage != Math.ceil((oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)))
|
|
|
116 |
{
|
|
|
117 |
oSettings.oApi._fnPageChange(oSettings, "last");
|
|
|
118 |
fnCallbackDraw(oSettings);
|
|
|
119 |
$(nFirst).removeClass('disabled');
|
|
|
120 |
$(nPrevious).removeClass('disabled');
|
|
|
121 |
$(nNext).addClass('disabled');
|
|
|
122 |
$(nLast).addClass('disabled');
|
|
|
123 |
}
|
|
|
124 |
} );
|
|
|
125 |
|
|
|
126 |
$(nInput).keyup( function (e) {
|
|
|
127 |
// 38 = up arrow, 39 = right arrow
|
|
|
128 |
if ( e.which == 38 || e.which == 39 )
|
|
|
129 |
{
|
|
|
130 |
this.value++;
|
|
|
131 |
}
|
|
|
132 |
// 37 = left arrow, 40 = down arrow
|
|
|
133 |
else if ( (e.which == 37 || e.which == 40) && this.value > 1 )
|
|
|
134 |
{
|
|
|
135 |
this.value--;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
if ( this.value === "" || this.value.match(/[^0-9]/) )
|
|
|
139 |
{
|
|
|
140 |
/* Nothing entered or non-numeric character */
|
|
|
141 |
this.value = this.value.replace(/[^\d]/g, ''); // don't even allow anything but digits
|
|
|
142 |
return;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
var iNewStart = oSettings._iDisplayLength * (this.value - 1);
|
|
|
146 |
if (iNewStart < 0)
|
|
|
147 |
{
|
|
|
148 |
iNewStart = 0;
|
|
|
149 |
}
|
|
|
150 |
if (iNewStart > oSettings.fnRecordsDisplay())
|
|
|
151 |
{
|
|
|
152 |
iNewStart = (Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength) - 1) * oSettings._iDisplayLength;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if (iNewStart === 0)
|
|
|
156 |
{
|
|
|
157 |
$(nFirst).addClass('disabled');
|
|
|
158 |
$(nPrevious).addClass('disabled');
|
|
|
159 |
$(nNext).removeClass('disabled');
|
|
|
160 |
$(nLast).removeClass('disabled');
|
|
|
161 |
}
|
|
|
162 |
else if (iNewStart == ((Math.ceil((oSettings.fnRecordsDisplay() - 1) / oSettings._iDisplayLength) - 1) * oSettings._iDisplayLength))
|
|
|
163 |
{
|
|
|
164 |
$(nNext).addClass('disabled');
|
|
|
165 |
$(nLast).addClass('disabled');
|
|
|
166 |
$(nFirst).removeClass('disabled');
|
|
|
167 |
$(nPrevious).removeClass('disabled');
|
|
|
168 |
}
|
|
|
169 |
else
|
|
|
170 |
{
|
|
|
171 |
$(nFirst).removeClass('disabled');
|
|
|
172 |
$(nPrevious).removeClass('disabled');
|
|
|
173 |
$(nNext).removeClass('disabled');
|
|
|
174 |
$(nLast).removeClass('disabled');
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
oSettings._iDisplayStart = iNewStart;
|
|
|
178 |
fnCallbackDraw( oSettings );
|
|
|
179 |
} );
|
|
|
180 |
|
|
|
181 |
/* Take the brutal approach to cancelling text selection */
|
|
|
182 |
$('span', nPaging).bind( 'mousedown', function () { return false; } );
|
|
|
183 |
$('span', nPaging).bind( 'selectstart', function () { return false; } );
|
|
|
184 |
|
|
|
185 |
// If we can't page anyway, might as well not show it
|
|
|
186 |
var iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength);
|
|
|
187 |
if(iPages <= 1)
|
|
|
188 |
{
|
|
|
189 |
$(nPaging).hide();
|
|
|
190 |
}
|
|
|
191 |
},
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
"fnUpdate": function ( oSettings, fnCallbackDraw )
|
|
|
195 |
{
|
|
|
196 |
if ( !oSettings.aanFeatures.p )
|
|
|
197 |
{
|
|
|
198 |
return;
|
|
|
199 |
}
|
|
|
200 |
var iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength);
|
|
|
201 |
var iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
|
|
|
202 |
|
|
|
203 |
var an = oSettings.aanFeatures.p;
|
|
|
204 |
if (iPages <= 1) // hide paging when we can't page
|
|
|
205 |
{
|
|
|
206 |
$(an).hide();
|
|
|
207 |
}
|
|
|
208 |
else
|
|
|
209 |
{
|
|
|
210 |
/* Loop over each instance of the pager */
|
|
|
211 |
for (var i = 0, iLen = an.length ; i < iLen ; i++)
|
|
|
212 |
{
|
|
|
213 |
var spans = an[i].getElementsByTagName('span');
|
|
|
214 |
var inputs = an[i].getElementsByTagName('input');
|
|
|
215 |
spans[3].innerHTML = " of " + iPages;
|
|
|
216 |
inputs[0].value = iCurrentPage;
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
};
|