pager: store object returned from ajaxProcessing for pager output or other fun & exciting things. Fixes #326

This commit is contained in:
Mottie 2013-10-09 21:41:58 -05:00
parent 9cb780126e
commit 838b4552d5
5 changed files with 158 additions and 22 deletions

View File

@ -119,18 +119,17 @@
p.page = (t) ? 0 : p.page;
p.endRow = Math.min( p.filteredRows, p.totalRows, p.size * ( p.page + 1 ) );
out = p.$container.find(p.cssPageDisplay);
// form the output string
s = p.output.replace(/\{(page|filteredRows|filteredPages|totalPages|startRow|endRow|totalRows)\}/gi, function(m){
return {
'{page}' : p.page + 1,
'{filteredRows}' : p.filteredRows,
'{filteredPages}' : p.filteredPages,
'{totalPages}' : p.totalPages,
'{startRow}' : p.startRow,
'{endRow}' : p.endRow,
'{totalRows}' : p.totalRows
}[m];
});
// form the output string (can now get a new output string from the server)
s = ( p.ajaxData && p.ajaxData.hasOwnProperty('output') ? p.ajaxData.output || p.output : p.output )
// {page} = one-based index; {page+#} = zero based index +/- value
.replace(/\{page([\-+]\d+)?\}/gi, function(m,n){
return p.page + (n ? parseInt(n, 10) : 1);
})
// {totalPages}, {extra}, {extra:0} (array) or {extra : key} (object)
.replace(/\{\w+(\s*:\s*\w+)?\}/gi, function(m){
var t = m.replace(/[{}\s]/g,''), a = t.split(':'); console.log(n);
return a.length > 1 && p.ajaxData && p.ajaxData[a[0]] ? p.ajaxData[a[0]][a[1]] : p[t] || p.ajaxData[t] || '';
});
if (out.length) {
out[ (out[0].tagName === 'INPUT') ? 'val' : 'html' ](s);
if ( p.$goto.length ) {
@ -216,14 +215,12 @@
// process data
if ( typeof(p.ajaxProcessing) === "function" ) {
// ajaxProcessing result: [ total, rows, headers ]
var i, j, hsh, $f, $sh, th, d, l, $err, rr_count,
var i, j, hsh, $f, $sh, t, th, d, l, $err, rr_count,
c = table.config,
$t = c.$table,
tds = '',
result = p.ajaxProcessing(data, table) || [ 0, [] ],
hl = $t.find('thead th').length,
// allow [ total, rows, headers ] or [ rows, total, headers ]
t = isNaN(result[0]) && !isNaN(result[1]);
hl = $t.find('thead th').length;
$t.find('thead tr.' + p.cssErrorRow).remove(); // Clean up any previous error.
@ -243,16 +240,26 @@
.appendTo( $t.find('thead:first') );
c.$tbodies.eq(0).empty();
} else {
//ensure a zero returned row count doesn't fail the logical ||
rr_count = result[t ? 1 : 0];
p.totalRows = isNaN(rr_count) ? p.totalRows || 0 : rr_count;
d = result[t ? 0 : 1] || []; // row data
// process ajax object
if (toString.call(result) !== "[object Array]") {
p.ajaxData = result;
p.totalRows = result.total;
th = result.headers;
d = result.rows;
} else {
// allow [ total, rows, headers ] or [ rows, total, headers ]
t = isNaN(result[0]) && !isNaN(result[1]);
//ensure a zero returned row count doesn't fail the logical ||
rr_count = result[t ? 1 : 0];
p.totalRows = isNaN(rr_count) ? p.totalRows || 0 : rr_count;
d = result[t ? 0 : 1] || []; // row data
th = result[2]; // headers
}
l = d.length;
th = result[2]; // headers
if (d instanceof jQuery) {
// append jQuery object
c.$tbodies.eq(0).empty().append(d);
} else if (d.length) {
} else if (l) {
// build table from array
if ( l > 0 ) {
for ( i = 0; i < l; i++ ) {

37
docs/assets/Info0.json Normal file
View File

@ -0,0 +1,37 @@
{
"total": 80,
"headers" : [
"ID", "Name", "Country Code", "District", "Population"
],
"rows" : [
[ 1, "Kabul", "AFG", "Kabol", 1780000 ],
[ 2, "Qandahar", "AFG", "Qandahar", 237500 ],
[ 3, "Herat", "AFG", "Herat", 186800 ],
[ 4, "Mazar-e-Sharif", "AFG", "Balkh", 127800 ],
[ 5, "Amsterdam", "NLD", "Noord-Holland", 731200 ],
[ 6, "Rotterdam", "NLD", "Zuid-Holland", 593321 ],
[ 7, "Haag", "NLD", "Zuid-Holland", 440900 ],
[ 8, "Utrecht", "NLD", "Utrecht", 234323 ],
[ 9, "Eindhoven", "NLD", "Noord-Brabant", 201843 ],
[ 10, "Tilburg", "NLD", "Noord-Brabant", 193238 ],
[ 11, "Groningen", "NLD", "Groningen", 172701 ],
[ 12, "Breda", "NLD", "Noord-Brabant", 160398 ],
[ 13, "Apeldoorn", "NLD", "Gelderland", 153491 ],
[ 14, "Nijmegen", "NLD", "Gelderland", 152463 ],
[ 15, "Enschede", "NLD", "Overijssel", 149544 ],
[ 16, "Haarlem", "NLD", "Noord-Holland", 148772 ],
[ 17, "Almere", "NLD", "Flevoland", 142465 ],
[ 18, "Arnhem", "NLD", "Gelderland", 138020 ],
[ 19, "Zaanstad", "NLD", "Noord-Holland", 135621 ],
[ 20, "´s-Hertogenbosch", "NLD", "Noord-Brabant", 129170 ],
[ 21, "Amersfoort", "NLD", "Utrecht", 126270 ],
[ 22, "Maastricht", "NLD", "Limburg", 122087 ],
[ 23, "Dordrecht", "NLD", "Zuid-Holland", 119811 ],
[ 24, "Leiden", "NLD", "Zuid-Holland", 117196 ],
[ 25, "Haarlemmermeer", "NLD", "Noord-Holland", 110722 ]
],
"extra" : ["extra-info0-0", "extra-info0-1", "extra-info0-2", "extra-info0-3"]
}

37
docs/assets/Info1.json Normal file
View File

@ -0,0 +1,37 @@
{
"total": 80,
"headers" : [
"ID", "Name", "Country Code", "District", "Population"
],
"rows" : [
[ 26, "Zoetermeer", "NLD", "Zuid-Holland", 110214 ],
[ 27, "Emmen", "NLD", "Drenthe", 105853 ],
[ 28, "Zwolle", "NLD", "Overijssel", 105819 ],
[ 29, "Ede", "NLD", "Gelderland", 101574 ],
[ 30, "Delft", "NLD", "Zuid-Holland", 95268 ],
[ 31, "Heerlen", "NLD", "Limburg", 95052 ],
[ 32, "Alkmaar", "NLD", "Noord-Holland", 92713 ],
[ 33, "Willemstad", "ANT", "Curaçao", 2345 ],
[ 34, "Tirana", "ALB", "Tirana", 270000 ],
[ 35, "Alger", "DZA", "Alger", 2168000 ],
[ 36, "Oran", "DZA", "Oran", 609823 ],
[ 37, "Constantine", "DZA", "Constantine", 443727 ],
[ 38, "Annaba", "DZA", "Annaba", 222518 ],
[ 39, "Batna", "DZA", "Batna", 183377 ],
[ 40, "Sétif", "DZA", "Sétif", 179055 ],
[ 41, "Sidi Bel Abbès", "DZA", "Sidi Bel Abbès", 153106 ],
[ 42, "Skikda", "DZA", "Skikda", 128747 ],
[ 43, "Biskra", "DZA", "Biskra", 128281 ],
[ 44, "Blida (el-Boulaida)", "DZA", "Blida", 127284 ],
[ 45, "Béjaïa", "DZA", "Béjaïa", 117162 ],
[ 46, "Mostaganem", "DZA", "Mostaganem", 115212 ],
[ 47, "Tébessa", "DZA", "Tébessa", 112007 ],
[ 48, "Tlemcen (Tilimsen)", "DZA", "Tlemcen", 110242 ],
[ 49, "Béchar", "DZA", "Béchar", 107311 ],
[ 50, "Tiaret", "DZA", "Tiaret", 100118 ]
],
"extra" : ["extra-info1-0", "extra-info1-1", "extra-info1-2", "extra-info1-3"]
}

37
docs/assets/Info2.json Normal file
View File

@ -0,0 +1,37 @@
{
"total": 80,
"headers" : [
"ID", "Name", "Country Code", "District", "Population"
],
"rows" : [
[ 51, "Ech-Chleff (el-Asnam)", "DZA", "Chlef", 96794 ],
[ 52, "Ghardaïa", "DZA", "Ghardaïa", 89415 ],
[ 53, "Tafuna", "ASM", "Tutuila", 5200 ],
[ 54, "Fagatogo", "ASM", "Tutuila", 2323 ],
[ 55, "Andorra la Vella", "AND", "Andorra la Vella", 21189 ],
[ 56, "Luanda", "AGO", "Luanda", 2022000 ],
[ 57, "Huambo", "AGO", "Huambo", 163100 ],
[ 58, "Lobito", "AGO", "Benguela", 130000 ],
[ 59, "Benguela", "AGO", "Benguela", 128300 ],
[ 60, "Namibe", "AGO", "Namibe", 118200 ],
[ 61, "South Hill", "AIA", "", 961 ],
[ 62, "The Valley", "AIA", "", 595 ],
[ 63, "Saint John´s", "ATG", "St John", 24000 ],
[ 64, "Dubai", "ARE", "Dubai", 669181 ],
[ 65, "Abu Dhabi", "ARE", "Abu Dhabi", 398695 ],
[ 66, "Sharja", "ARE", "Sharja", 320095 ],
[ 67, "al-Ayn", "ARE", "Abu Dhabi", 225970 ],
[ 68, "Ajman", "ARE", "Ajman", 114395 ],
[ 69, "Buenos Aires", "ARG", "Distrito Federal", 2982146 ],
[ 70, "La Matanza", "ARG", "Buenos Aires", 1266461 ],
[ 71, "Córdoba", "ARG", "Córdoba", 1157507 ],
[ 72, "Rosario", "ARG", "Santa Fé", 907718 ],
[ 73, "Lomas de Zamora", "ARG", "Buenos Aires", 622013 ],
[ 74, "Quilmes", "ARG", "Buenos Aires", 559249 ],
[ 75, "Almirante Brown", "ARG", "Buenos Aires", 538918 ]
],
"extra" : ["extra-info2-0", "extra-info2-1", "extra-info2-2", "extra-info2-3"]
}

18
docs/assets/Info3.json Normal file
View File

@ -0,0 +1,18 @@
{
"total": 80,
"headers" : [
"ID", "Name", "Country Code", "District", "Population"
],
"rows" : [
[ 76, "La Plata", "ARG", "Buenos Aires", 521936 ],
[ 77, "Mar del Plata", "ARG", "Buenos Aires", 512880 ],
[ 78, "San Miguel de Tucumán", "ARG", "Tucumán", 470809 ],
[ 79, "Lanús", "ARG", "Buenos Aires", 469735 ],
[ 80, "Merlo", "ARG", "Buenos Aires", 463846 ]
],
"extra" : ["extra-info3-0", "extra-info3-1", "extra-info3-2", "extra-info3-3"]
}