1. Docs
  2. Cookbook

A cart with quantities

Order lines in a table, a minus and plus on each, a bin when there's one left, and a total the table keeps itself.

3 steps · ends in a cart that totals itself

A cart is a table of lines, each with a price and a quantity. The table can add itself up, so the total and the item count are declared once and stay right through every tap.

Step 1 of 3

The lines and the total

A line table with a name, an icon and color for its picture, a price in cents and a qty. Two sums in the model keep the totals: <sum name="items" of="qty"/> counts the items, and <sum name="total" of="qty * price"/> multiplies each line out and adds them up. They read as $db.line.items and $db.line.total, and format(…, 'money') writes cents as dollars.

app.ham · step 1
<app>    <config primary_color="deep-orange"/>    <database name="cart"              version="1">        <model name="line">            <field name="name"/>            <field name="icon"/>            <field name="color"/>            <field name="price"                   type="int"/>            <field name="qty"                   type="int"                   default="1"/>            <sum name="items"                 of="qty"/>            <sum name="total"                 of="qty * price"/>            <seed>[{"name": "Margherita", "icon": "local_pizza", "color": "deep-orange", "price": 1200, "qty": 2}, {"name": "Garlic knots", "icon": "bakery_dining", "color": "amber", "price": 550, "qty": 1}, {"name": "Lemonade", "icon": "local_drink", "color": "yellow", "price": 350, "qty": 3}]</seed>        </model>    </database>    <screen>        <app-bar title="Your order"/>        <main>            <column style="w-full p-16 gap-12">                <@foreach in="line"                          order="name"                          as="l">                    <row style="w-full gap-12 items-center">                        <avatar icon="$l.icon"                                color="$l.color"                                size="44"/>                        <column flex="1">                            <text>@{$l.name}</text>                            <text>                                @{format($l.price * $l.qty / 100, 'money')}                            </text>                        </column>                        <text>× @{$l.qty}</text>                    </row>                </@foreach>                <row style="w-full justify-between">                    <text>@{$db.line.items} items</text>                    <text>@{format($db.line.total / 100, 'money')}</text>                </row>            </column>        </main>    </screen></app>
Step 1 on a phone: The lines and the total Step 1 on a phone, dark: The lines and the total
Step 1, drawn by the engine from the snippet beside it.

Step 2 of 3

Change the quantities

Each line gets a minus, its quantity and a plus. Plus runs #db.line.increment:$l.id,qty and minus #db.line.decrement. With one left, the minus becomes a bin (@show swaps them) that deletes the line with #db.line.delete. Every change re-adds the sums, so the line price, the count and the total follow on their own. <@empty> says so when the last line goes.

app.ham · step 211 new lines
<app>    <config primary_color="deep-orange"/>    <database name="cart"              version="1">        <model name="line">            <field name="name"/>            <field name="icon"/>            <field name="color"/>            <field name="price"                   type="int"/>            <field name="qty"                   type="int"                   default="1"/>            <sum name="items"                 of="qty"/>            <sum name="total"                 of="qty * price"/>            <seed>[{"name": "Margherita", "icon": "local_pizza", "color": "deep-orange", "price": 1200, "qty": 2}, {"name": "Garlic knots", "icon": "bakery_dining", "color": "amber", "price": 550, "qty": 1}, {"name": "Lemonade", "icon": "local_drink", "color": "yellow", "price": 350, "qty": 3}]</seed>        </model>    </database>    <screen>        <app-bar title="Your order"/>        <main>            <column style="w-full p-16 gap-12">                <@foreach in="line"                          order="name"                          as="l">                    <row style="w-full gap-12 items-center">                        <avatar icon="$l.icon"                                color="$l.color"                                size="44"/>                        <column flex="1">                            <text>@{$l.name}</text>                            <text>                                @{format($l.price * $l.qty / 100, 'money')}                            </text>                        </column>                        <button @show="$l.qty > 1"                                @click="#db.line.decrement:$l.id,qty"                                icon="remove"/>                        <button @show="$l.qty == 1"                                @click="#db.line.delete:$l.id"                                icon="delete"/>                        <text>@{$l.qty}</text>                        <button @click="#db.line.increment:$l.id,qty"                                icon="add"/>                    </row>                <@empty>                    <text>Your cart is empty.</text>                </@foreach>                <row style="w-full justify-between">                    <text>@{$db.line.items} items</text>                    <text>@{format($db.line.total / 100, 'money')}</text>                </row>            </column>        </main>    </screen></app>
Step 2 on a phone: Change the quantities Step 2 on a phone, dark: Change the quantities
Step 2, drawn by the engine from the snippet beside it.

Step 3 of 3

Finish it

Each line becomes a soft grey card, the buttons get round outlines and a small haptic on every tap, the total gets large and bold, and Check out shows while there's anything to check out.

app.ham · step 328 new lines
<app>    <config primary_color="deep-orange"/>    <database name="cart"              version="1">        <model name="line">            <field name="name"/>            <field name="icon"/>            <field name="color"/>            <field name="price"                   type="int"/>            <field name="qty"                   type="int"                   default="1"/>            <sum name="items"                 of="qty"/>            <sum name="total"                 of="qty * price"/>            <seed>[{"name": "Margherita", "icon": "local_pizza", "color": "deep-orange", "price": 1200, "qty": 2}, {"name": "Garlic knots", "icon": "bakery_dining", "color": "amber", "price": 550, "qty": 1}, {"name": "Lemonade", "icon": "local_drink", "color": "yellow", "price": 350, "qty": 3}]</seed>        </model>    </database>    <screen>        <app-bar title="Your order"/>        <main>            <column style="w-full p-16 gap-12">                <@foreach in="line"                          order="name"                          as="l">                    <row style="w-full p-12 gap-12 items-center rounded-2xl bg-grey-100 dark:bg-grey-900">                        <avatar icon="$l.icon"                                color="$l.color"                                size="44"/>                        <column flex="1">                            <text style="font-bold">@{$l.name}</text>                            <text style="muted font-sm">                                @{format($l.price * $l.qty / 100, 'money')}                            </text>                        </column>                        <button @show="$l.qty > 1"                                @click="#db.line.decrement:$l.id,qty; haptic:selection"                                variant="outlined"                                icon="remove"                                style="rounded-full"/>                        <button @show="$l.qty == 1"                                @click="#db.line.delete:$l.id; haptic:light"                                variant="outlined"                                icon="delete"                                style="rounded-full"/>                        <text style="w-28 font-bold text-center">@{$l.qty}</text>                        <button @click="#db.line.increment:$l.id,qty; haptic:selection"                                variant="outlined"                                icon="add"                                style="rounded-full"/>                    </row>                <@empty>                    <text style="muted text-center py-32">Your cart is empty.</text>                </@foreach>                <row style="w-full pt-8 justify-between">                    <text style="muted">@{$db.line.items} items</text>                    <text style="font-xl font-bold">@{format($db.line.total / 100, 'money')}</text>                </row>                <button @show="$db.line.items > 0"                        variant="filled"                        icon="shopping_bag"                        style="w-full rounded-full">Check out</button>            </column>        </main>    </screen></app>
Step 3 on a phone: Finish it Step 3 on a phone, dark: Finish it
Step 3, drawn by the engine from the snippet beside it.

This page is also Markdown, and all of HAM is in llms.txt.