I have a site that displays bar graphs of data. I am trying to implement pagination for the graphs so that the user can click 'next' or 'previous' to scroll through different subsets of the total data.
Here is the HTML section in question:
<div class="graph_fields_wrap1 row backdrop col-lg-12">
<div class="col-lg-6">
<h3 class="titles">Top Ten Author Citations</h3>
<h4 class="titles">All time (from 1970)</h4>
<button class="pager" id="previous1" type="button"><span class="glyphicon glyphicon-chevron-left"></span> previous</button>
<button class="pager" id="next1" type="button">next <span class="glyphicon glyphicon-chevron-right"></span></button>
<div class="chart1 well bs-component"></div>
</div>
<div class="col-lg-6">
<h3 class="titles">Top Ten Author Citations</h3>
<h4 class="titles userTitle"></h4>
<button class="pager" id="previous2" type="button"><span class="glyphicon glyphicon-chevron-left"></span> previous</button>
<button class="pager" id="next2" type="button">next <span class="glyphicon glyphicon-chevron-right"></span></button>
<div class="chart2 well bs-component"></div>
</div>
</div> <!-- row -->
Here is the JavaScript:
$(document).ready(function() {
// change graph according to author selection
var wrapperG = $(".graph_fields_wrap1"); // wrapper for div containing citations graphs
var next1 = $("#next1"); // pagination for all time cited graph
var previous1 = $("#previous1");
var next2 = $("#next2"); // pagination for user defined cited graph
var previous2 = $("#previous2");
// variables to log subset location in arrays (to use in slice)
var from1 = 1;
var to1 = 11;
// PAGINATION //
// all time cited, next author set
$(wrapperG).on("click", next1, function (e) {
// ignore default action for this event
e.preventDefault();
// shift pointers up 10 for next subset of array
from1 += 10;
console.log(from1);
to1 += 10;
console.log(to1);
// remove currently displayed graph, 1st child of div (1st graph is 0th)
$($(wrapperG).children()[0]).remove();
// load new graph before other graph (1st child of div)
$(wrapperG).prepend("<div class='col-lg-6'><h3 class='titles'>Top Ten Author Citations</h3><h4 class='titles'>All time (from 1970)</h4><button class='pager' id='previous1' type='button'><span class='glyphicon glyphicon-chevron-left'></span> previous</button><button class='pager' id='next1' type='button'>next <span class='glyphicon glyphicon-chevron-right'></span></button><div class='chart1 well bs-component'></div></div>").loadGraph((topCited.slice(from1,to1)), "chart1", palette1);
});
// all time cited, previous author set
$(wrapperG).on("click", previous1, function (e) {
// ignore default action for this event
e.preventDefault();
// shift pointers down 10 for previous subset of array
from1 -= 10;
console.log(from1);
to1 -= 10;
console.log(to1);
// remove currently displayed graph, 1st child of div (1st graph is 0th)
$($(wrapperG).children()[0]).remove();
// load new graph before other graph (1st child of div)
$(wrapperG).prepend("<div class='col-lg-6'><h3 class='titles'>Top Ten Author Citations</h3><h4 class='titles'>All time (from 1970)</h4><button class='pager' id='previous1' type='button'><span class='glyphicon glyphicon-chevron-left'></span> previous</button><button class='pager' id='next1' type='button'>next <span class='glyphicon glyphicon-chevron-right'></span></button><div class='chart1 well bs-component'></div></div>").loadGraph((topCited.slice(from1,to1)), "chart1", palette1);
});
});
I initially used $(next1).on("click", function
but the button only fired once then stopped working. Looking up similar queries on Stack Overflow I saw that, because the HTML is removed, the bound handlers are as well. So then I binded the handler to a part that is not removed (wrapperG
).
I put the console.log
lines to help me see what's going on when I click the buttons. When I load the page and click 'next', the console logs the data 11, 21, 1, 11 and keeps repeating that data on repeat clicks of either the 'next' or 'previous' button click. Obviously on clicking 'next' I want the data to log 11, 21 then 21, 31 then 31, 41 and so on. Similarly for the 'previous' button but decreasing by 10 each time. The bar graph then shows 10 bars of data according to where in the array the slice
is taken.
If I comment out the 'previous' button jQuery section then the 'next' button works fine and the bar graph displays the data properly. This makes me think that the problem is in both buttons being contained within the same div
. As I'm removing child()[0]
of .graph_fields_wrap1
within the function then this is the only div
I can refer to.
** ADDITIONAL **
Listing the console.log
data above I realised I'd copied it out incorrectly. It is actually returning 11, 21, 1, 11.
This localizes the problem to being when clicking the 'next' button, it is firing both event handlers, so the first one increases the values by 10, but then the second one decreases them by 10, negating the effect.
Therefore, I need to find out why clicking the 'next' button is firing off both events and how to stop this from happening.