score:1
Glad you came back with what you've tried until now based off the comment I posted. I couldn't work on it yesterday as I got too busy with work. Anyway, got it working now.
I made a few changes to it and also added certain additional features (First, Prev, Next, Last) and seems to be working okay now.
HTML added:
<div id="toggle-buttons">
<button class="button" data-id="first">
First Page
</button>
<button class="button disabled" data-id="previous">
Prev Page
</button>
<button class="button" data-id="next">
Next Page
</button>
<button class="button" data-id="last">
Last Page
</button>
<button class="button" id="all-button">
Show All
</button>
</div>
Javascript to paginate the table based on the button click:
const totalBodies = $('tbody').length;
var counter = 0; // set counter initially to 0
d3.selectAll('#toggle-buttons button').on('click', function () {
if(d3.select(this).classed('disabled'))
return ;
switch(d3.select(this).attr('data-id')) {
case 'first':
counter = 0;
d3.select(this.parentNode).select('[data-id="previous"]').classed('disabled', true);
d3.select(this.parentNode).select('[data-id="next"]').classed('disabled', false);
break;
case 'last':
counter = totalBodies-1;
d3.select(this.parentNode).select('[data-id="next"]').classed('disabled', true);
d3.select(this.parentNode).select('[data-id="previous"]').classed('disabled', false);
break;
case 'next':
counter++;
if(counter === totalBodies-1) {
d3.select(this).classed('disabled', true);
}
d3.select(this.parentNode).select('[data-id="previous"]').classed('disabled', false);
break;
case 'previous':
counter--;
if(!counter) {
d3.select(this).classed('disabled', true);
}
d3.select(this.parentNode).select('[data-id="next"]').classed('disabled', false);
break;
}
redraw(counter);
});
Explanation (pretty much understandable I suppose):
- Switch case to determine the button click based on which the counter will change. Important Toggling the
disabled
class on the buttons. redraw(counter)
on every button click (unless it has a disabled class)
Here's a fork of your fiddle:
https://jsfiddle.net/shashank2104/noxg31eq/
I'm selecting the first page by default. You can change that according to the requirement and add the disabled
class in the initial phase accordingly. Also, I see a table
nested within a table
which doesn't seem right - maybe tablesorter
adds a table
tag as well. You can take a look at that.
Hope this helps.
score:0
I got some of it myself based off what Shashank posted:
let tbodiesPerPage = 10;
const totalBodies = $('tbody').length;
d3.select("#buttons").datum({
portion: 0
});
// the chain select here pushes the datum onto the up and down buttons also
d3.select("#buttons").select("#previous-button").on("click", function (d) {
console.log('next was ', d.portion);
if (d.portion - tbodiesPerPage >= 0) {
d.portion -= tbodiesPerPage;
redraw(d.portion);
}
});
d3.select("#buttons").select("#next-button").on("click", function (d) {
// let the bodies hit the floor
console.log('previous was', d.portion);
if (d.portion < (totalBodies - tbodiesPerPage)) {
d.portion += tbodiesPerPage;
redraw(d.portion);
}
});
function redraw(start) {
d3
.select("table")
.selectAll("tr")
.style("display", function (d, i) {
return i >= start && i < start + tbodiesPerPage ? null : "none";
});
}
redraw(0);
Source: stackoverflow.com
Related Query
- How to paginate multiple HTML tbodies with jQuery
- How to avoid redundancy with HTML code -- do not want same code for multiple html files
- How to set multiple attributes with one value function?
- How to select multiple selectors with selectAll?
- How to use D3 selectAll with multiple class names
- How To Move (Drag and Drop) multiple Shapes with D3
- How can D3 select multiple shapes with shared attributes from a group?
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- how to add array of text to multiple divs with d3
- How to overcome x-axis styling problems when multiple X-axis Values with single point to load in C3 graph?
- How to plot data points on multi-line chart with multiple y axes
- How to zoom in chart correctly with multiple y-axis
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
- How to make multiple charts with d3.chart using nested data
- How to export multiple html tables and multiple d3 generated graphs into a single pdf
- How to load multiple files with Queue.js and D3.js?
- How do I update multiple columns of data with D3?
- how do I get stroke (border) around multiple rectangles with d3?
- How to draw multiple bars in a row in a bar chart with spaces in between with d3v5?
- How can I apply a clipPath in SVG with multiple paths and NOT clip out the area between the paths?
- How to include two htmls into another html using jQuery
- How to create multiple pie charts with D3 from a single array with multiple objects?
- How to set up custom color scale with multiple sub-parts in D3?
- How to interact JQuery slider with d3 vertical lines for every data point
- How do you add rotation logic for mobile devices upon dragging on a 3d globe with D3.js and html canvas
- How to draw a scatter plot with multiple y values for each x value?
- dc.js with crossfilter and d3, how to load multiple csv files at once, without knowing how many files the user will upload
- How to create one group with multiple dimension on dc.js
- How to redraw my d3.js chart with data from html form?
- With d3.js, how do you modify corresponding nodes when you have multiple selections for the same data?
More Query from same tag
- Map custom value field to x value when using a stacked layout
- d3.js force-directed graph maintain constant link distances
- Plotly.js Adding markers adds padding to x-axis
- Confused by d3.js's event dispatch
- How to set the width of td as float number
- Assigning heights dynamically, based on arithmetic series
- replacing d3.js diagram instead of creating a new one every time
- my tooltip in d3 won't show up in the right place
- Scientific Notation in Handsontable
- D3 - Returning the whole array with the max count
- want to generate multiple charts in d3 in same page for different data set
- Why does ngDirective `template` allow for correct `clientHeight` but `templateUrl` gives 0
- Data not formatted to date properly in d3
- How to create a line chart with vertical line and different backgrounds?
- Calculate Width of a DIV based on sibling count (not-fixed) using CSS or LESS
- How do I use MongoDB data with D3 and Node?
- D3 - Binding CSV Data to Array
- How can we create an onclick event for a d3 data map?
- D3 treeview - start with collapsed nodes
- queue.js not asynchronously?
- d3 time format returning null
- how to change zoom.translateExtent after declaring zoom
- Loading Image till external json data loads for collapsible tree -d3.js
- Using D3 in React without svg for table rows arn't rendering
- D3 charts change the time format and its display on the axes
- I get an error on my .enter() d3
- How to add text on top the heatmap to properly show value for each rectangle when using D3.js?
- Use d3.queue() to load .tsvs when working within Django
- dc.js Adding a dynamic legend to a pie chart
- Game (match) result tree graph drawing