Skip to content

Commit 2961eb8

Browse files
Merge pull request BroadleafCommerce#317 from BroadleafCommerce/qa-1078-1451-fix-pagination-math-and-have-search-move-to-0-index
Prevent a requirement to scroll up after searching on a list grid
2 parents 03ef1e6 + 84663aa commit 2961eb8

File tree

2 files changed

+65
-46
lines changed

2 files changed

+65
-46
lines changed

admin/broadleaf-open-admin-platform/src/main/resources/open_admin_style/js/admin/components/listGrid-filter.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,11 @@ $(document).ready(function() {
522522
for (key in submitData) {
523523
BLCAdmin.history.replaceUrlParameter(key, submitData[key]);
524524
}
525+
526+
// Moved this outside of the ajax call so that it would remove this
527+
// value from the url BEFORE making the ajax call.
528+
BLCAdmin.history.replaceUrlParameter('startIndex');
529+
525530
var urlParams = "";
526531
var baseUrl = window.location.href;
527532
var indexOfQ = baseUrl.indexOf('?');
@@ -534,10 +539,13 @@ $(document).ready(function() {
534539
type: "GET",
535540
data: submitData
536541
}, function(data) {
537-
BLCAdmin.history.replaceUrlParameter('startIndex');
538542
for (key in submitData) {
539543
BLCAdmin.history.replaceUrlParameter(key, submitData[key]);
540544
}
545+
546+
// Added the scroll to Index so that it does not save the previous
547+
// current index from before starting the search.
548+
BLCAdmin.listGrid.paginate.scrollToIndex($('body').find('tbody'), 0);
541549
BLCAdmin.listGrid.replaceRelatedListGrid($(data), null, { isRefresh : false});
542550
$firstInput.trigger('input');
543551
});

admin/broadleaf-open-admin-platform/src/main/resources/open_admin_style/js/admin/components/listGrid-paginate.js

+56-45
Original file line numberDiff line numberDiff line change
@@ -367,52 +367,57 @@
367367
}
368368
}
369369
}
370-
371370
if (startIndex != null && maxIndex != null) {
372-
var delta;
373-
if (startIndex <= topIndex && maxIndex <= botIndex) {
374-
// Top range missing - show in the middle of the top and max
375-
delta = (topIndex + maxIndex) / 2;
376-
} else if (startIndex > topIndex && maxIndex < botIndex) {
377-
// Mid range missing - show in the middle of the start and max
378-
delta = (startIndex + maxIndex) / 2;
379-
} else if (startIndex > topIndex && maxIndex >= botIndex) {
380-
// Bottom range missing - show in the middle of the start and bot
381-
delta = (startIndex + botIndex) / 2;
382-
} else {
383-
// Full range missing - show in the middle of the top and bot
384-
delta = (topIndex + botIndex) / 2;
385-
}
386-
delta = delta - topIndex;
387-
spinnerOffset = $tbody.closest('.mCustomScrollBox').position().top + 3 + (this.getRowHeight($tbody) * delta);
388-
BLCAdmin.listGrid.showLoadingSpinner($tbody, spinnerOffset);
389-
390-
var params = BLCAdmin.history.getUrlParameters();
391-
for (var param in params) {
392-
baseUrl = BLCAdmin.history.getUrlWithParameter(param, params[param], null, baseUrl);
393-
}
394-
395-
var url = BLCAdmin.history.getUrlWithParameter('startIndex', startIndex, null, baseUrl);
396-
url = BLCAdmin.history.getUrlWithParameter('maxIndex', maxIndex, null, url);
397-
398-
//console.log('Loading more records -- ' + url);
399-
400-
BLC.ajax({ url: url, type: 'GET' }, function(data) {
401-
var $newTbody = data.find('tbody');
402-
BLCAdmin.listGrid.paginate.injectRecords($tbody, $newTbody);
403-
BLCAdmin.listGrid.paginate.releaseLock();
404-
405-
// now that I've loaded records, see if I need to do it again
406-
var topIndex = BLCAdmin.listGrid.paginate.getTopVisibleIndex($tbody);
407-
var topIndexLoaded = BLCAdmin.listGrid.paginate.isIndexLoaded($tbody, topIndex);
408-
var botIndex = BLCAdmin.listGrid.paginate.getBottomVisibleIndex($tbody);
409-
var botIndexLoaded = BLCAdmin.listGrid.paginate.isIndexLoaded($tbody, botIndex);
410-
if (!botIndexLoaded || !topIndexLoaded) {
411-
BLCAdmin.listGrid.paginate.loadRecords($tbody, baseUrl);
371+
// If we find that the start index is greater than the max index, we do not
372+
// need to gather any further records, so don't make an ajax call. We also do not
373+
// want to show the spinner as nothing needs to be done in the first place.
374+
if (startIndex <= maxIndex) {
375+
var delta;
376+
if (startIndex <= topIndex && maxIndex <= botIndex) {
377+
// Top range missing - show in the middle of the top and max
378+
delta = (topIndex + maxIndex) / 2;
379+
} else if (startIndex > topIndex && maxIndex < botIndex) {
380+
// Mid range missing - show in the middle of the start and max
381+
delta = (startIndex + maxIndex) / 2;
382+
} else if (startIndex > topIndex && maxIndex >= botIndex) {
383+
// Bottom range missing - show in the middle of the start and bot
384+
delta = (startIndex + botIndex) / 2;
412385
} else {
413-
BLCAdmin.listGrid.hideLoadingSpinner($tbody);
386+
// Full range missing - show in the middle of the top and bot
387+
delta = (topIndex + botIndex) / 2;
414388
}
415-
});
389+
delta = delta - topIndex;
390+
spinnerOffset = $tbody.closest('.mCustomScrollBox').position().top + 3 + (this.getRowHeight($tbody) * delta);
391+
392+
393+
BLCAdmin.listGrid.showLoadingSpinner($tbody, spinnerOffset);
394+
395+
var params = BLCAdmin.history.getUrlParameters();
396+
for (var param in params) {
397+
baseUrl = BLCAdmin.history.getUrlWithParameter(param, params[param], null, baseUrl);
398+
}
399+
400+
var url = BLCAdmin.history.getUrlWithParameter('startIndex', startIndex, null, baseUrl);
401+
url = BLCAdmin.history.getUrlWithParameter('maxIndex', maxIndex, null, url);
402+
403+
//console.log('Loading more records -- ' + url);
404+
BLC.ajax({ url: url, type: 'GET' }, function(data) {
405+
var $newTbody = data.find('tbody');
406+
BLCAdmin.listGrid.paginate.injectRecords($tbody, $newTbody);
407+
BLCAdmin.listGrid.paginate.releaseLock();
408+
409+
// now that I've loaded records, see if I need to do it again
410+
var topIndex = BLCAdmin.listGrid.paginate.getTopVisibleIndex($tbody);
411+
var topIndexLoaded = BLCAdmin.listGrid.paginate.isIndexLoaded($tbody, topIndex);
412+
var botIndex = BLCAdmin.listGrid.paginate.getBottomVisibleIndex($tbody);
413+
var botIndexLoaded = BLCAdmin.listGrid.paginate.isIndexLoaded($tbody, botIndex);
414+
if (!botIndexLoaded || !topIndexLoaded) {
415+
BLCAdmin.listGrid.paginate.loadRecords($tbody, baseUrl);
416+
} else {
417+
BLCAdmin.listGrid.hideLoadingSpinner($tbody);
418+
}
419+
});
420+
}
416421
} else {
417422
BLCAdmin.listGrid.paginate.releaseLock();
418423
}
@@ -423,7 +428,10 @@
423428
// ************************* *
424429

425430
getRowHeight : function($tbody) {
426-
return $tbody.find('tr:not(.blank-padding):first').height();
431+
// Updated the code here to return the exact value (possibly float values) of
432+
// the height of the row. Previously it would round this value which led to
433+
// inaccurate math.
434+
return $tbody.find('tr:not(.blank-padding):first')[0].getBoundingClientRect().height;
427435
},
428436

429437
getTopVisibleIndex : function($tbody) {
@@ -440,7 +448,10 @@
440448
getBottomVisibleIndex : function($tbody) {
441449
var scrollOffset = $tbody.closest('.mCSB_container').position().top;
442450
var trHeight = this.getRowHeight($tbody);
443-
var bottomVisibleIndex = Math.floor((scrollOffset * -1 + $tbody.closest('.listgrid-body-wrapper').height() - 4) / trHeight);
451+
// Updated the code here to use the exact value (possibly float value) of
452+
// the listgrid body wrapper. Previously it would round this value which
453+
// led to inaccurate math.
454+
var bottomVisibleIndex = Math.floor((scrollOffset * -1 + $tbody.closest('.listgrid-body-wrapper')[0].getBoundingClientRect().height - trHeight) / trHeight);
444455
return bottomVisibleIndex;
445456
},
446457

0 commit comments

Comments
 (0)