← Data apps

One table instead

Rebuild the playlist with a table: one row per song, a column for each field (title, artist, length). Each song's details live together in one row, so adding or removing a song can't split them up. A Table object shows the table, and clicking a row removes that song.

Spreadsheets, school timetables and music libraries all keep data the same way: one row for each thing, a column for each detail.

Last lesson, a song's title and artist were linked only by sitting at the same place in two lists. Today they live together in one table:

Removing a song now removes its whole row, so nothing can get out of step.

A playlist app: boxes for title, artist and minutes, an Add song button, and a table with columns title, artist and length.

How this lesson works: See it, Build, Check, Make it yours
See itTry the finished thing first, so you know what you are building.

Try the finished app

This is the app you'll build, open on the Run tab. Add a song, then click a song in the table to remove it. On the left, under Tables, the rows the app has now show under the table.

Try it yourselfYour edits are saved in this browser.

BuildWatch the short clip, then do the steps. Your app carries on from the step before.

Build 1: make the table

  1. On the left, under Tables, type Songs and press +. A new table has one field, name.
  2. Click name at the top of its column, choose Rename, and call it title.
  3. Press + at the end of the field names to add a field. Call it artist. Add one more called length.
  4. Click length, and choose Number: lengths are numbers, not text.
  5. Press + Row and type a song: title, artist, minutes. Press Tab to move along. Add a second song.
Make a table · Sound on for the voiceover
Try it yourselfYour edits are saved in this browser.

A table Songs with the fields title, artist and length.

    Run your program. This ticks when your code does all of the above.

    BuildWatch the short clip, then do the steps. Your app carries on from the step before.

    Build 2: show it on the screen

    On the Design tab, click the empty table on the screen (SongTable). In Properties, set Shows table to Songs. Your two songs appear, one row each. Columns to show lets you hide a field, but keep all three for now.

    Show the table · Sound on for the voiceover
    Try it yourselfYour edits are saved in this browser.

    SongTable shows the Songs table.

      Run your program. This ticks when your code does all of the above.

      BuildWatch the short clip, then do the steps. Your app carries on from the step before.

      Build 3: add a row

      1. Add an event: AddButton is clicked.
      2. Add the action Tables, Add a row to table, table Songs. There's one box for each field.
      3. Fill them in: title TitleInput.text, artist ArtistInput.text, length int(LengthInput.text).
      4. Run it and add a song. Watch it arrive at the bottom of the table.
      Add a row · Sound on for the voiceover
      Try it yourselfYour edits are saved in this browser.

      When AddButton is clicked, a row is added to Songs with the title, artist and length that were typed.

        Run your program. This ticks when your code does all of the above.

        BuildWatch the short clip, then do the steps. Your app carries on from the step before.

        Build 4: click a row to remove it

        1. Add an event: SongTable, An item in SongTable is clicked. ClickedIndex is the number of the clicked row.
        2. Add the action Tables, Remove row … from table: row ClickedIndex, table Songs.
        3. Run it and remove the middle song. One action, and the whole song is gone.
        Remove the clicked row · Sound on for the voiceover
        Try it yourselfYour edits are saved in this browser.

        When a row of SongTable is clicked, that row is removed from Songs.

          Run your program. This ticks when your code does all of the above.

          CheckA quick check that the idea has stuck.

          Check: rows and fields

          Songs has three rows: Blue Sky, Arc, Cove. Row 2 is removed. What is item(Songs, 2).title now?

          In the table, can removing a song leave the wrong artist beside a title, as in the two lists?

          Make it yoursChange your app on purpose, then try it on someone.

          Make it yours: a genre field

          This screen has a new Genre box.

          1. Add a field genre to Songs (the + after the field names). It's text.
          2. Open the AddButton event's Add a row action. It has a new box for genre: fill it with GenreInput.text.

          Compare that with last lesson's third list: one new field, and nothing else to keep in step.

          Ask someone to add three songs without any help from you. Watch where they pause. Labels, an example in a box, or a message after adding are all good fixes.

          Try it yourselfYour edits are saved in this browser.

          Songs has a genre field, and Add a row fills it from GenreInput.

            Run your program. This ticks when your code does all of the above.

            Now you can
            Next lesson3. Records and fieldsUse the songs in a table: show the clicked song as Now playing, add up every song's length with For each row, and find the longest song by keeping the best one so far. Each row is a record, and LoopItem.length or ClickedItem.title reads one field of it.
            For teachers

            Students rebuild the playlist as a table: Songs, with the fields title, artist and length (a number field), made in App Builder's sidebar spreadsheet. A Table object shows it; Add a row takes one value per field; clicking a row removes it with Remove row ClickedIndex. The Check traces row numbers after a removal and asks why a table can't split a song. About 60 minutes.

            Open the teacher guide: answers, solutions, slides and a worksheet.

            Students will be able to

            Before class

            Tables are an App Builder Level 4 feature; these lessons open App Builder at Level 4.

            A table is a list of records: each row is one record, each column a field. Field types matter: length is a number so it can be added up (next lesson) and sorted by size (lesson 5). int(LengthInput.text) turns the typed text into a number; a number field also accepts text that is a number.

            Removing a row renumbers the rows below it, as a list does.

            Running the lesson

            1. Before Build 1 (whole class): draw the two lists from last lesson, then redraw them as one table. What's the same, what's better?
            2. Build 1: checkpoint: everyone's Songs table has three fields and length shows 123 (number).
            3. Check: act it out: three students hold cards for three rows; remove the middle one, and the third steps up.

            The hard part (on purpose)

            Making the table: rename the first field, add fields, change length to Number. The sidebar is narrow; the field's own menu (click its name) does rename, type and delete.

            When students get stuck

            Stretch and support

            Stretch: Use Columns to show to hide length on the screen. Does the table still keep it?

            Support: Make the table together on the projector; pairs build the events.

            Where it leads

            Next: Records and fields: read one field of a row, add up a column, find the longest song.

            What each step is for