A list you can add to
Build invoice line items: type a line, add it to a list, remove it again, and add up the total.
What you'll build: the line items of an invoice, like the Invoice Maker app. People type a description, pick a quantity and type a rate, then tap Add line. Each line shows its amount and a Remove button, and the total adds itself up.
What you'll learn:
- Keep a list of records in one variable.
- Show one row per item with a Repeater.
- Add an item made of what people typed.
- Remove the row whose button was tapped.
- Add up a list with a formula.
#The variables
| Name | Type | Starting value |
|---|---|---|
items | List of Records | One example line |
draftDescription | Text | Empty |
draftQty | Whole Number | 1 |
draftRate | Text | Empty |
The draft… variables hold what's being typed, before it becomes a line.
#Steps
#Make the list
- In the Library (the left panel), open Data & Logic.
- Tap Add variable, then List of… → Records. It's called
items. (If that name was taken, it gets a number; tap the name to change it.) - Under the list, tap Add item. The new record has no fields yet: tap Add field four times and pick Text, Text, Decimal Number and Decimal Number. They're named
field1,field2…; tap each name and change them toid,description,qtyandrate. - Fill in an example line:
1,Logo design,1,400. - Add the other three variables from the table with Add variable. Each is named after its type (
text,count…), so tap each name and type the one from the table. SetdraftQty's starting value to 1.
#Show one row per line
- Tap the empty canvas around the device, so nothing is selected.
- In the Library's Components tab, tap Column (under Layout). It goes at the bottom of the screen and stays selected.
- Tap Row. It goes inside the Column.
- Tap Text, Spacer, Text and Button, in that order. Each one lands after the one before it, inside the Row.
- Select the Column again (tap it in Layers). In the Inspector's Data tab, tap Repeat for each item… next to Repeat, and choose For each in items. The Row is now inside a Repeater, which repeats it once per line.
- Select the first Text in the row. Next to Content, tap ⚡︎ → Repeater item → description.
Select the second Text and type this into Content:
{currency(item.qty * item.rate)}- Select the Button and change its Title to
Remove.
item is the line this copy of the row is showing. currency(…) writes a number as money in the iPad's currency; add a code to pick one, like currency(item.rate, 'EUR').
#Remove a line
- With the Remove button selected, open the Actions tab. When Tapped is already picked.
- Tap Add action and choose Remove from List (under Data). Tap the step to open it.
Set State to
itemsand Where to:it.id == item.id
Where starts as true, which would remove every line, so change it before you try the button.
#Type a new line
- Tap the empty canvas, then add a Text Field. In its Text menu (in the Style tab, or the Data tab), pick
draftDescription, and set its Title toDescription. The title shows in the empty field. - Add a Stepper. Set its Title to
Qty {draftQty}, its Value todraftQtyand its Range to 1 and 99. - Add another Text Field for the rate. Pick
draftRatein its Text menu, set Title toRate, and under Validation set Must be to Number. People get a number keyboard, and a message under the field if they type something else.
#Add the line
A step can't build a record out of separate fields: Add to List's Item only takes fixed values, or one value linked with ⚡︎. So the new line comes in through a reusable action, whose values arrive together as params.
- In Data & Logic, under Reusable actions, tap New action. In the sheet that opens, tap Add action → Add to List.
- Set State to
items. Next to Item, tap ⚡︎ → Formula…, typeparamsand tap Done. - Tap Done to close the sheet. Tap the action's name,
newAction, and change it toaddLine. - With the Rate field selected, add a Button: it goes right after the field. Set its Title to
Add line. In its Actions tab, add these steps:
| Step | Settings |
|---|---|
| If | Condition isEmpty(trim(draftDescription)). Under Then: Show Message (Text Describe the line first, Style Warning), then Stop. |
| If | Condition (number(draftRate) ?? 0) <= 0. Under Then: Show Message (Text Enter a rate above 0, Style Warning), then Stop. |
| Run Action | Action addLine. Under With, tap Add field four times and name the fields id, description, qty and rate. Link each with ⚡︎: id → Formula… uuid(); description → Formula… trim(draftDescription); qty → App data → draftQty; rate → Formula… number(draftRate). |
| Reset Value | State draftDescription |
| Reset Value | State draftQty |
| Reset Value | State draftRate |
#Add up the total
Add a Labeled Value (under Layout). Set its Title to
Totaland type this into Value:{currency(sum(map(items, it.qty * it.rate)))}- Optional: add a Text that says
No lines yet. Add one below.and set its Show when (under Show & enable) toisEmpty(items).
Tap Preview, add a few lines and remove one. The total follows along.
#How it works
- Repeater: draws its views once per item in
items. Inside,itemis the line being drawn, soitem.descriptionanditem.qty * item.ratediffer on every row. - Adding: the If steps check what was typed; Stop ends the run before anything is added. Run Action sends four values to
addLine, whereparamsis all of them as one record:{ id, description, qty, rate }. Add to List puts that record at the end ofitems. - Ids:
uuid()gives each line a new id, so Whereit.id == item.idremoves only the row that was tapped, even when two lines look the same. - Numbers:
draftRateholds text, sonumber(draftRate)reads the number out of it. When it can't, it gives nothing, and?? 0turns that into 0. - Total:
map(items, it.qty * it.rate)turns the list into each line's amount, andsum(…)adds them up. It runs again wheneveritemschanges.