Looper
The Looper component acts as a container and requires an array and another component, such as a List or Card. It uses the array to dynamically populate the other component's content inside the Looper.
Properties
Name | Type | Default | Description |
---|---|---|---|
loopData | array | Defines an array of data in the format id, prop1, prop2, prop3. The id must be is small letters. | |
itemName | string | Defines a unique identifier that allows access the array element's properties. |
[!NOTE] Example: How create dynamic content using the Looper
For this example, we assume you already have a Page with Form Block, Form Text Field, a submit Button called "Add", and a Looper. You will also need a database table for a To Do List with "id" and "item" fields.
Additionally, you will need one query and two mutations:
- addToDo - Takes input from the text field and stores it in the database table.
- getTodos - Fetches the To Do List data from the database table.
- deleteToDo - Deletes a To Do item in the database table.
- Drag a Card component from the Components pane and drop it onto your Looper.
- Click the Page Structure icon to confirm the Card component is inside the Looper Content component.
- Click the Requests icon to open the Requests pane and "Run" your getTodos query.
- Once you get your Response, click the State icon to open the State pane.
- Under the Requests section, click the green arrow next to the getTodos name to view its properties.
- Click the green arrow next to the "data" property and drill down to the "items" property. At this point, the array will not contain any data.
- Click the clipboard icon below the "Items" property to copy the array to the clipboard.
- Click the Page Structure icon and select your Looper component, which opens the Properties pane.
- Paste the array you copied into the Loop Data field. Your array should look something like this:
{{ getTodos.data?.todosList.items || [] }}
- In the Item Name field, enter the word item.
- Select your Card component on the Page and open the Properties pane.
- Under Custom Properties, click "Add Item" to add a custom property.
- Click the gear icon next to the propname to open a pop-up window.
- Enter "cardid" in the Key field and
{{ item?.id || "" }}
in the Value field, then close the pop-up.- Click the gear icon next to the Header property to open a pop-up window.
- Enter
{{ item?.todo || "" }}
in the Title field, which will display the item as the header for the Card component.- Select the Card Content component on the Page and open the Properties pane.
- Drag an Icon Button component onto the Card Content component.
- Expand the Icon section of the Properties pane, and click on the gear icon next to the Icon property to open a pop-up window.
- Click the "Change" button, which opens a pop-up window.
- Enter "Delete" in the Search field, choose an appropriate icon, click "Apply", and close the Icon properties pop-up window.
- Select the Icon Button and click the Events tab in the right-hand pane.
- Click "+" and select the "On Click" event.
- Click "Choose Action", then select "Run Custom Code", and click the "Add Custom Code" button.
- You want to call your deleteToDo mutation in the code panel and pass it the cardid.
- Your code should look similar to:
deleteToDo.run({ variables:{ "todoId": item.id} });
.If you enter a To Do item in the form text field, such as "Clean Car" and click the "Add" button, a new card is created with the heading "Clean Car". As you add new items, they are added to the Looper array.
If you click the delete button on a card, the card is deleted. As you delete items, they are deleted from the Looper array.