PadCodedocs

Connecting views to data

Show variables in views with the ⚡︎ button, let controls change them, and repeat a design for every item in a list.

A view can show a variable, and a control can change one. When the variable changes, everything that shows it updates. There are four ways to connect them:

  • Link a setting to a variable or a formula with the ⚡︎ button.
  • Put names in braces in text: Coffees today: {count}. See Text with values.
  • Give a control its variable, like the Text a Text Field keeps, in the Inspector's Data tab.
  • Repeat a design for every item in a list with a Repeater.

Most settings in the Inspector have a ⚡︎ button beside them, called Set from variable. It works in the Style tab and in a step's settings in the Actions tab.

  1. Select the view.
  2. Find the setting, like a Text's Content in the Style tab.
  3. Tap ⚡︎ and pick a variable under App data.

The setting's box becomes an orange chip with the variable's name, and the ⚡︎ fills in. The view now shows the variable's value and changes with it. On the canvas you see the starting value; in Preview, the live one.

The ⚡︎ menu on a step's Text: the app's variables under App data, and Formula… at the bottom.

The ⚡︎ menu shows what can go in that place:

In the menuWhat it linksThe chip shows
App dataOne of your variables.Its name
Screen inputsA value this screen was opened with. Only when the screen has inputs.ƒ params.name
Repeater itemA field of the current item, for a view inside a Repeater.item.title
From this actionIn a step's settings: it, each item, inside Repeat for Each, Update in List and Remove from List; error.message, error.status and error.body inside Try's Catch.ƒ it
SecretsA secret. Only in a Call API step's Headers.The secret's name
Formula…Opens the Formula sheet, to work the value out.ƒ count * 2
Remove LinkShown once the setting is linked: takes the link away.

To change a link, tap ⚡︎ again and pick something else. Formula… opens with the link that's there, so you can edit it.

#Controls that change a variable

A control keeps what people type or pick in a variable. When you add one from the Library, PadCode makes a variable for it, named after what it keeps: a Switch comes with isOn, a Text Field with text.

ComponentSettingWhat the variable keeps
Text Field, Password Field, Text AreaTextWhat people type
SwitchIs OnWhether it's on (True / False)
SliderValueWhere the knob sits
StepperValueThe number + and − change
PickerSelectionThe option picked, as its text
Date PickerSelectionThe date picked
Multi-Date PickerSelectionThe dates picked
Color PickerSelectionThe color picked

On the Free plan, Text Field and Switch are there; the Password Field, Text Area, Slider, Stepper and the pickers are part of Pro. See Plans and billing.

To use a different variable:

  1. Select the control.
  2. Open the Inspector's Data tab. Under This view, the setting shows its variable.
  3. Tap it and pick another variable, or New Value to make one named after the setting.

The same menu is in the Style tab, with the control's other settings.

In Preview, typing in the field or flipping the switch changes the variable, and every view showing it updates. The canvas doesn't run your app, so controls there don't change anything.

A Text Field's Data tab: under This view, its Text keeps the variable client.

#The Data tab

The Inspector's Data tab shows how the selected view connects to data:

  • This view: the variables the view reads or changes, like a control's Text or Is On. On a Repeater, Repeat for; on a container, Repeat. A view with none of these doesn't show this section.
  • Screen inputs: values this screen receives when another screen opens it. While the screen has none, this section is closed: tap its title to open it. Tap Add input to add one (input1, then input2, and so on), type its default value in the box, or tap ⊖ to remove it. Read one as params.input1 in a formula, or pick it under Screen inputs in the ⚡︎ menu. The screen that opens this one fills it with Go to Screen's With.
  • Variables, secrets, APIs & actions: switches the Library to its Data & Logic tab.

#Show a list with a Repeater

A Repeater shows its views once for every item in a list: one row per task, one card per product.

#Add a Repeater

There are two ways:

  • From the Library. Add a Repeater (under Lists & Data). It comes with a new list of three records, named items (or items2 if that's taken), and a Text that shows each item's title.
  • From views you already have. Select a container, like a Column, and open the Data tab. Under This view, tap Repeat for each item… next to Repeat, then pick For each in a list, or New List to make one. The container's views move into a new Repeater, which is selected.

#Pick the list

Select the Repeater and open the Data tab. Repeat for lists every variable whose starting value is a list. The menu shows names with a capital letter and spaces, so lineItems appears as Line Items.

The Repeater's Data setting in the Style tab is the same list, with a ⚡︎ button. Use it to repeat over a list that's worked out or that an API fills:

  • ⚡︎ → Formula… → filter(tasks, !it.done) shows only the tasks that aren't done.
  • ⚡︎ → Formula… → catalog.products shows a list inside an API's answer.

#Show each item's fields

  1. Select a view inside the Repeater, like its Text.
  2. Tap ⚡︎ next to its text.
  3. Pick a field under Repeater item.

The chip shows item.title. Formulas and text in braces read the current item as item: {item.title}, currency(item.price). Repeater item lists the fields of the list's first item, so it's empty while the list is empty; type a formula with item. instead.

A Repeater selected in Layers, with its Data linked to the list items in the Style tab.

On the canvas you edit the first copy, and the others follow it. An empty list still shows its design once, so you can work on it.

#See also