You will build: A post list with a filter panel that opens on click, filters by author and confirms each change with a toast
Read first: Show data from an API
In this tutorial you make a page react to the visitor without writing code. You add page variables, open and close a panel with a click, filter a list with an expression, and run steps when a variable changes or the page loads. You start from the post list built in Show data from an API.
Add page variables
Load more posts
Open the page with the
postsquery and press Data at the bottom of the left sidebar. Select thepostsquery and change Path to/posts?_limit=30. The first 30 posts come from three authors, withuserId1, 2 and 3.Add a variable for the panel
Next to Page variables, press + and choose Boolean. The variable is called
isOpenand starts asfalse. It will say whether the filter panel is open.Add a variable for the filter
Press + again and choose Number. Rename it to
userIdand leave the default value at0, which will mean "all authors".
Open and close a panel
Add the button and the panel
Back on the canvas, add a Button above the list and set its Text to
Filters. Under it, draw a frame namedFilter panelwith three buttons:All authors,Author 1andAuthor 2.Show the panel only when it is open
Select
Filter panel. In Visibility, press the bind button next to Show when and typevars.isOpen. The panel disappears from the canvas, becauseisOpenisfalse.Add a click workflow
Select the
Filtersbutton. In Events, keep Click and press Add workflow. The Logic workspace opens on a new client workflow whose trigger is a click on that button.Toggle the variable
In Steps, press Add step and choose Set variable. Set Variable to
isOpenand Value to!vars.isOpen. Each click now flips the value betweentrueandfalse.Try it
Press Run in preview. The Run log at the bottom shows the step, and the panel appears on the canvas. Run it again to hide the panel.
Running the workflow shows and hides Filter panel on the canvas, and the Run log lists one Set variable step per run.
Filter the list
Filter the items with an expression
Select the collection list. In Collection list, press the binding next to Items and replace it with:
jsvars.userId === 0 ? queries.posts.data : queries.posts.data.filter(p => p.userId === vars.userId)When
userIdis0the list shows every post; otherwise it keeps only the posts of that author.Set the author on click
Select the
Author 1button and add a click workflow in Events. Add a Set variable step with VariableuserIdand Value1. Do the same forAuthor 2with the value2.Reset the filter
Select
All authors, add a click workflow and add a Reset variable step foruserId. It puts the variable back to its default value,0.
React when a variable changes
Start a workflow on a variable change
In the Logic workspace, press + (Add workflow) and choose Variable changes. In Trigger, set Variable to
userId. This workflow runs every time the filter changes, whichever button changed it.Close the panel
Add a Set variable step with Variable
isOpenand Valuefalse.Confirm with a toast
Add a Show toast step. Next to Message, press Use an expression and type:
jsvars.userId === 0 ? 'Showing all posts' : `Showing posts by author ${vars.userId}`Set Variant to Success.
Test the whole page
Press ⌘↵ to preview. Click Filters, then Author 2: the panel closes, the list shows only that author's posts and a toast confirms it. Click Filters and All authors to see every post again. Press ⌘↵ to go back to editing.
In the preview, choosing an author closes the panel, filters the list and shows a toast.
Page load and navigation
Greet visitors on page load
Press + (Add workflow) and choose Page load. Add a Show toast step with the message
Use Filters to pick an authorand the variant Info. It runs once each time the page opens.Send visitors to another page
Add a
Contact usbutton under the list and give it a click workflow with a Go to page step. In Destination, pick one of your pages, type a path such as/contact, or paste a full URL.A button that only goes to a page does not need a workflow: set Target in its Link section instead. Use Go to page when other steps must run first, or when the destination depends on the result of a step.
Publish
Press Publish and try the staging page. Workflows run in the visitor's browser, so the panel, the filter and the toasts work on the live site.
Your page opens a filter panel, filters the list by author, reacts to every change and links to another page.
What you learned
- Page variables hold the state of the page; bindings such as Show when and Items read them.
- Client workflows start on an element event, a variable change or page load, and run steps in order.
- Set variable with an expression like
!vars.isOpentoggles a value, and afilterexpression narrows a list.
Read the guides behind this tutorial: