Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs for pagination: bind this to before callback #977

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,36 @@ The above will iterate over a data set containing: `["item1 with a suffix", "ite

You can do anything in this `before` callback. Maybe a custom `.sort()`, `.filter()`, `.map()` to remap the entries, `.slice()` to paginate only a subset of the data, etc!

### Accessing `this` in `before` {% addedin "0.12.2" %}

If you need to, you can access all data for the containing page or template, via `this` in your `before` function.

{% raw %}
```markdown
---js
{
pagination: {
data: "testdata",
size: 2,
before: function(data) {
const containerPageTitle = this.data.title;
return data.filter(entry => entry.startsWith(containerPageTitle + ": "));
}
},
testdata: [
"Page foo: item foo1",
"Page foo: item foo2",
"Page moo: item moo1",
"Page moo: item moo2"
]
}
---
<!-- the rest of the template -->
```
{% endraw %}

In the above example, if the containing page has the title `"Page moo"`, then the data will be filtered to only contain: `["Page moo: item moo1", "Page moo: item moo2"]`.

### Order of Operations

If you use more than one of these data set modification features, here’s the order in which they operate:
Expand Down