Tutorial 6 - (Answers)
Last Time we challenged you to pull together everything you'd learned to create some Pagination Buttons powered by Graph. Answers here!
Challenge Objective
For this challenge, we'd asked you to set up your own simple Pagination controls. This combined several of the skills you've learned so far. As usual, don't worry if you need to check these answers before completing the challenge on your own.
On a new Page you create, the User should be able to see the first three Items in the Gallery WebApp.
Then, when they press the Page 2 button, the Page should refresh and they should see the second Page of Results.
Step 1) Writing the Graph Query
Firstly, here's the query without variables.
Now let's add the variables:
Notes:
The most difficult part here might have been working out the type. Here the query expects Page to be an integer. The documentation panel confirms this:
From the documentation panel, you can also see "= 1" after the type. This is a default, which means if no variable is passed through, the variable will be given a default value of 1. Not all variables will have defaults.
The type here does not have an exclamation mark ! so the query won't deliberately fail if no value is passed in.
We set
per_page
to 3
Step 2) Sync the Query to the Site using Siteglide-CLI
Refer back to Tutorial 5 to refresh these steps.
Step 3) Adding the HTML Controls
We gave you these in the tips. You'll have needed to add them on a Page of your choice.
Pressing one of the buttons will redirect us to the same Page, but will be adding a page
query parameter to the URL. This is the easiest way to pass variables to Liquid- because Liquid runs before the Page loads, and the URL is one of the only available sources of dynamic information at this point.
It's also possible to pass information to Liquid on another Page via an XHR request. This can lead to smoother UX, but is more challenging and will be covered in a future tutorial.
Step 4) Reading the URL Parameters
Let's say we pressed the second button, our relative URL will now be:/my-page-slug?page=2
As we showed you in the tips, Liquid can read this parameter at the following dot-notation path. This will output the value on the Page: {{context.params.page}}
Or you can store this in a variable, which we'll need to do here: `
`
By the way, the keys in context.params
are dynamically generated for any query parameter in the URL, so you can pass through any variable you like with this method.
Step 5) Feeding the Variables into the Query
a) First, let's set a default value, just in case a User arrives at the Page without setting the Parameter: <div data-gb-custom-block data-tag="assign" data-0='1' data-1='1' data-2='1' data-3='1' data-4='1' data-5='1' data-6='1' data-7='1' data-8='1' data-9='1' data-10='1' data-11='1' data-12='1' data-13='1' data-14='1' data-15='1' data-16='1' data-17='1' data-18='1' data-19='1' data-20='1' data-21='1' data-22='1' data-23='1'></div>
b) The query is expecting an integer, so let's apply an Integer filter that will change the type to an Int
without changing the value: <div data-gb-custom-block data-tag="assign" data-0='1' data-1='1' data-2='1' data-3='1' data-4='1' data-5='1' data-6='1' data-7='1' data-8='1' data-9='1' data-10='1' data-11='1' data-12='1' data-13='1' data-14='1' data-15='1' data-16='1' data-17='1' data-18='1' data-19='1' data-20='1' data-21='1' data-22='1' data-23='1' data-24='0' data-25='0' data-26='0' data-27='0' data-28='0'></div>
c) Let's add the graphql
tag with the variable parameter.
Step 6) Output the Results
We can output the results using the variable name we defined in the graphql
tag.
If pressing the HTML anchors changes the results by fetching different "pages" of JSON results, you've been successful. Congratulations.
If you're having difficulty with any of these steps still, please don't hesitate to ask for help on the Forum.
Full Code
Liquid
GraphQL
Optional- Taking this further
The next steps give you ideas for how to take this further. They are not part of the initial challenge!
Step 7) Output a Layout (Optional)
These JSON results don't look great. Remember, you can use Liquid to pass the results into a Layout- if you can find that Layout's relative path in Code Editor.
However, you won't have access to the user-friendly names for fields in that Layout, because GraphQL will output the raw database IDs for fields.
You'll want to target fields in this Layout with syntax like:{{this.properties.webapp_field_1_2}}
You can view the database IDs for fields in the Admin using the toggle in the top-right of the screenshot, or you can look at the keys in the JSON results.
Step 8) Use the Results of the Query to only show buttons you need. (Optional)
By returning total_pages
from the query, we'll know exactly how many Pagination buttons to display:
You can then use this to manipulate the HTML pagination controls:
Next Time
Next time, we'll learn how to sort the results of your Query.
Let's go!
Last updated