Skip to content

Commit 6595841

Browse files
Merge pull request #25 from coding-blocks/question_pagination
add: pagination in question route
2 parents a51d79c + 89846a1 commit 6595841

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Component from '@ember/component';
2+
3+
export default class PaginationPillsComponent extends Component {
4+
didReceiveAttrs() {
5+
const length = this.pages
6+
this.set('pagesArray', Array.from({length}, (v, i) => ++i))
7+
}
8+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<nav aria-label="Page navigation">
2+
<ul class="pagination justify-content-center">
3+
<li class="page-item">
4+
<button
5+
class="page-link"
6+
{{action setPage (dec pagination.currentPage)}}
7+
disabled={{eq pagination.currentPage 1}}>
8+
Previous
9+
</button>
10+
</li>
11+
12+
{{#each pagesArray as |page|}}
13+
<li class="page-item">
14+
<button
15+
class="page-link active {{if (eq page pagination.currentPage) "bg-red white"}}"
16+
{{action setPage page}} >
17+
{{page}}
18+
</button>
19+
</li>
20+
{{/each}}
21+
22+
<li class="page-item">
23+
<button
24+
class="page-link"
25+
{{action setPage (inc pagination.currentPage)}}
26+
disabled={{not pagination.nextOffset}}>
27+
Next
28+
</button>
29+
</li>
30+
</ul>
31+
</nav>

app/pods/questions/index/controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import Controller from '@ember/controller';
22
import { inject as service } from '@ember/service';
33
import { task, timeout } from 'ember-concurrency';
4+
import { computed } from '@ember/object';
45

56
export default Controller.extend({
67
store: service(),
78
queryParams: ['page', 'limit'],
89
page: 1,
910
limit: 10,
1011
searchString: '',
12+
pageCount: computed('limit', 'questions', function() {
13+
return Math.ceil(this.questions.meta.pagination.count / this.limit)
14+
}),
1115
searchTask: task(function * () {
1216
yield timeout(250)
1317

app/pods/questions/index/template.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,9 @@
6565
</div>
6666
</div>
6767
{{/each}}
68+
{{pagination-pills
69+
pagination=questions.meta.pagination
70+
pages=pageCount
71+
setPage=(action (mut page))}}
6872
</div>
6973
{{outlet}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { render } from '@ember/test-helpers';
4+
import hbs from 'htmlbars-inline-precompile';
5+
6+
module('Integration | Component | pagination-pills', function(hooks) {
7+
setupRenderingTest(hooks);
8+
9+
test('it renders', async function(assert) {
10+
// Set any properties with this.set('myProperty', 'value');
11+
// Handle any actions with this.set('myAction', function(val) { ... });
12+
13+
await render(hbs`{{pagination-pills}}`);
14+
15+
assert.equal(this.element.textContent.trim(), '');
16+
17+
// Template block usage:
18+
await render(hbs`
19+
{{#pagination-pills}}
20+
template block text
21+
{{/pagination-pills}}
22+
`);
23+
24+
assert.equal(this.element.textContent.trim(), 'template block text');
25+
});
26+
});

0 commit comments

Comments
 (0)