8.18.1.32. data_table

ImGui’s tables API — BeginTable / EndTable with body-internal TableSetupColumn / TableHeadersRow / TableNextRow / TableSetColumnIndex / TableNextColumn cursor primitives — lives behind one boost container plus six snake_case pass-throughs in imgui/imgui_table_builtin. The container is named data_table (not table) because table is a daslang reserved keyword for the table<K;V> type constructor.

The container takes the ImGui id, the column count, the flags, an outer size and an inner width — same five arguments the cpp BeginTable takes — and brackets the matching EndTable. Inside the body, the row/column cursor calls are plain pass-throughs that resolve in the imgui_table_builtin module namespace (so the project-wide lint, which forbids raw imgui::* calls in user code, stays satisfied without an allow-list extension).

Source: modules/dasImgui/examples/tutorial/data_table.das.

8.18.1.32.1. Walkthrough

The recording drives the sortable shape by clicking the real column headers: clicking Name flips the active sort ascending to descending; clicking Type replaces the key and re-groups the rows by type; and Shift+clicking Value adds it as a secondary key (the 2 badge), so within each type the rows break ties by value. Each click is verified against the re-rendered cell text, so a header click that failed to re-sort would abort the recording.

  1options gen2
  2options _comment_hygiene = true
  3
  4require imgui
  5require imgui_app
  6require opengl/opengl_boost
  7require live/glfw_live
  8require live/live_api
  9require live/live_commands
 10require live/live_vars
 11require live_host
 12require imgui/imgui_live
 13require imgui/imgui_boost_runtime
 14require imgui/imgui_boost_v2
 15require imgui/imgui_widgets_builtin
 16require imgui/imgui_containers_builtin
 17require imgui/imgui_table_builtin
 18require imgui/imgui_visual_aids
 19
 20// =============================================================================
 21// TUTORIAL: data_table — boost container for ImGui's tables API.
 22//
 23// `BeginTable` / `EndTable` and the body-internal cursor calls
 24// (TableSetupColumn, TableHeadersRow, TableNextRow, TableSetColumnIndex,
 25// TableNextColumn) live behind one container + six snake_case pass-throughs
 26// in `imgui/imgui_table_builtin`. The container is named `data_table`
 27// (not `table`, which is a daslang reserved keyword for `table<K;V>`).
 28//
 29//   data_table(MY_TABLE, (text = "##split", columns = 3, flags = ...,
 30//                          outer_size = float2(0,0), inner_width = 0.0f)) {
 31//       table_setup_column("Name")
 32//       table_setup_column("Type")
 33//       table_setup_column("Value")
 34//       table_headers_row()
 35//       for (row in rows) {
 36//           table_next_row()
 37//           table_set_column_index(0); text(NAME[row], (text = ...))
 38//           table_set_column_index(1); text(TYPE[row], (text = ...))
 39//           table_set_column_index(2); text(VAL[row], (text = ...))
 40//       }
 41//   }
 42//
 43// `TableState` echoes per-call config (columns / flags / outer_size /
 44// inner_width). `sort_specs()` (block helper) yields the per-frame sort
 45// spec list — see the Sortable tables section below. Multi-select
 46// hand-off and custom row-bg callbacks would extend the state
 47// additively if added later.
 48//
 49// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/data_table.das
 50// LIVE:       daslang-live modules/dasImgui/examples/tutorial/data_table.das
 51// =============================================================================
 52
 53struct private Row {
 54    name : string
 55    kind : string
 56    value : string
 57}
 58
 59var private ROWS : array<Row>
 60
 61// Stable column identifiers passed to table_setup_column(.., user_id=...).
 62// The sort comparator dispatches on user_id (not column_index) so the
 63// sort stays correct when the user reorders columns via drag.
 64let private COL_NAME = 0x0001u
 65let private COL_KIND = 0x0002u
 66let private COL_VAL  = 0x0003u
 67
 68[init]
 69def seed_rows() {
 70    ROWS <- [
 71        Row(name = "alpha", kind = "int", value = "42"),
 72        Row(name = "beta", kind = "float", value = "3.14"),
 73        Row(name = "gamma", kind = "string", value = "hello"),
 74        Row(name = "delta", kind = "bool", value = "true"),
 75        Row(name = "epsilon", kind = "int", value = "-7"),
 76        Row(name = "zeta", kind = "float", value = "0.001")
 77    ]
 78}
 79
 80def private compare_rows(spec : TableSortSpec; a : Row; b : Row) : int {
 81    // Returns -1 / 0 / 1 for a-vs-b along the column the spec names.
 82    var ord = 0
 83    if (spec.column_user_id == COL_NAME) {
 84        if (a.name < b.name) {
 85            ord = -1
 86        } elif (a.name > b.name) {
 87            ord = 1
 88        }
 89    } elif (spec.column_user_id == COL_KIND) {
 90        if (a.kind < b.kind) {
 91            ord = -1
 92        } elif (a.kind > b.kind) {
 93            ord = 1
 94        }
 95    } elif (spec.column_user_id == COL_VAL) {
 96        if (a.value < b.value) {
 97            ord = -1
 98        } elif (a.value > b.value) {
 99            ord = 1
100        }
101    }
102    if (spec.sort_direction == ImGuiSortDirection.Descending) {
103        ord = -ord
104    }
105    return ord
106}
107
108def private sort_rows(specs : array<TableSortSpec>) {
109    // Multi-key comparator: first spec that disambiguates a vs b wins; name is the total-order tiebreak.
110    ROWS |> sort() $(a, b) {
111        for (s in specs) {
112            let ord = compare_rows(s, a, b)
113            if (ord != 0) return ord < 0
114        }
115        return a.name < b.name
116    }
117}
118
119var DT_NAME : table<int; NarrativeState>
120var DT_KIND : table<int; NarrativeState>
121var DT_VAL : table<int; NarrativeState>
122
123[export]
124def init() {
125    live_create_window("dasImgui data_table tutorial", 860, 540)
126    live_imgui_init(live_window)
127    // Deterministic FirstUseEver layout for the recording: disable imgui.ini so a
128    // prior session's window pos/size can't drift the framing. Tutorial-scoped on
129    // purpose - not a blanket change to live_imgui_init.
130    DisableIniPersistence()
131    let io & = unsafe(GetIO())
132    GetStyle().FontScaleMain = 1.4
133}
134
135[export]
136def update() {
137    if (!live_begin_frame()) return
138    begin_frame()
139
140    ImGui_ImplGlfw_NewFrame()
141    apply_synth_io_override()
142    NewFrame()
143
144    SetNextWindowPos(ImVec2(30.0f, 30.0f), ImGuiCond.FirstUseEver)
145    SetNextWindowSize(ImVec2(640.0f, 380.0f), ImGuiCond.FirstUseEver)
146    window(DT_WIN, (text = "data_table", closable = false,
147                    flags = ImGuiWindowFlags.None)) {
148        text(DT_HEADER, (text = "Three columns, six rows, frozen header. Click headers to sort; Shift+click for multi-column."))
149
150        let tflags = (ImGuiTableFlags.BordersOuter |
151                      ImGuiTableFlags.RowBg |
152                      ImGuiTableFlags.Resizable |
153                      ImGuiTableFlags.Reorderable |
154                      ImGuiTableFlags.Hideable |
155                      ImGuiTableFlags.Sortable |
156                      ImGuiTableFlags.SortMulti |
157                      ImGuiTableFlags.ScrollY)
158        data_table(DT_TABLE, (text = "##rows", columns = 3,
159                              flags = tflags,
160                              outer_size = float2(0.0f, 0.0f),
161                              inner_width = 0.0f)) {
162            table_setup_scroll_freeze(0, 1)
163            table_setup_column("Name",  ImGuiTableColumnFlags.DefaultSort, 0.0f, COL_NAME)
164            table_setup_column("Type",  ImGuiTableColumnFlags.None,        0.0f, COL_KIND)
165            table_setup_column("Value", ImGuiTableColumnFlags.None,        0.0f, COL_VAL)
166            table_headers_row()
167
168            sort_specs() $(specs) {
169                sort_rows(specs)
170            }
171
172            for (i in range(length(ROWS))) {
173                table_next_row()
174                table_set_column_index(0)
175                text(DT_NAME[i], (text = ROWS[i].name))
176                table_set_column_index(1)
177                text(DT_KIND[i], (text = ROWS[i].kind))
178                table_set_column_index(2)
179                text(DT_VAL[i], (text = ROWS[i].value))
180            }
181        }
182    }
183
184    end_of_frame()
185    Render()
186    var w, h : int
187    live_get_framebuffer_size(w, h)
188    glViewport(0, 0, w, h)
189    glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
190    glClear(GL_COLOR_BUFFER_BIT)
191    live_imgui_render()
192
193    live_end_frame()
194}
195
196[export]
197def shutdown() {
198    live_imgui_shutdown()
199    live_destroy_window()
200}
201
202[export]
203def main() {
204    init()
205    while (!exit_requested()) {
206        update()
207    }
208    shutdown()
209}

8.18.1.32.1.1. Requires

One extra module on top of the baseline boost layer:

  • imgui/imgui_table_builtin — the data_table container plus the six table_* snake_case primitives the body calls into.

8.18.1.32.1.2. Container shape

data_table follows the same named-tuple convention as the other containers (window / child / tab_bar):

data_table(MY_TABLE, (text = "##rows", columns = 3,
                       flags = ImGuiTableFlags.Borders,
                       outer_size = float2(0.0f, 0.0f),
                       inner_width = 0.0f)) {
    // body
}

text is the ImGui id (use the ##suffix convention to keep it out of the visible label). columns is the column count; outer_size = float2(0,0) lets ImGui auto-size; inner_width = 0.0f means “no explicit inner width” (use the outer width).

8.18.1.32.1.3. Body primitives

The six body cursor calls are plain def public wrappers — same arguments as the underlying ImGui calls, snake_case names:

  • table_setup_column(label, flags?, init_width?, user_id?) — declare a column before the header row.

  • table_setup_scroll_freeze(cols, rows) — pin the first N columns / M rows during scrolling.

  • table_headers_row() — submit the header row using the table_setup_column labels.

  • table_next_row(flags?, min_row_height?) — start the next row.

  • table_set_column_index(col) -> bool — jump to a specific column; returns true when the column is visible.

  • table_next_column() -> bool — advance one column (or wrap to next row); also returns the visibility bool.

TableState (the container’s state struct) echoes per-call config — columns, flags, outer_size, inner_width — so snapshot consumers can read the table’s shape without parsing the daslang call site. Multi-select hand-off remains deferred — pinned ImGui 1.92.6 exposes BeginMultiSelect / ImGuiMultiSelectIO, but the boost-wrapper hand-off has not been designed yet. A custom row-bg callback API would extend the state additively if added later.

8.18.1.32.1.4. Sortable tables

The tutorial table uses the full sortable shape — Sortable | SortMulti | Reorderable | Hideable flags on the table, a stable user_id on each table_setup_column, and a sort_specs() block-arg helper inside the body that ImGui fires when the sort state goes dirty.

  • ImGuiTableFlags.Sortable enables single-column sort (click any header). Adding ImGuiTableFlags.SortMulti enables multi-column sort (Shift+click a second header to append a secondary sort key).

  • table_setup_column("Name", flags, init_width, user_id=COL_NAME) tags the column with a stable identifier (a uint). The sort comparator dispatches on column_user_id rather than column_index, so the sort stays correct after the user reorders columns via drag.

  • sort_specs() $(specs) { ... } is the wrapper that captures the ImGui TableGetSortSpecs() data, converts each ImGuiTableColumnSortSpecs entry into a daslang-friendly TableSortSpec (with column_index, column_user_id, sort_order, sort_direction), invokes the body block with the array, and auto-clears the SpecsDirty flag on return. The block only fires when ImGui reports dirty (header click), so the comparator cost is paid once per sort change rather than every frame.

The block-body comparator pattern walks the specs in priority order and returns on the first spec that disambiguates a pair — sort_order = 0 is the primary key, sort_order = 1 is the first tiebreak, and so on. A final tiebreak on a unique field (here: name) keeps the order total.

For a complete standalone example (inventory table with id / name / qty columns and a multi-key comparator), see modules/dasImgui/examples/features/sort_specs.das.

8.18.1.32.1.5. Why the name

table is a daslang reserved keyword — the type constructor for table<K;V> (the hash-map type). Defining a function or container named table is a parse error. data_table follows the standard UI-library term (Material’s DataTable, Bootstrap’s table, etc.) and disambiguates from the type namespace at every call site.

8.18.1.32.1.6. Standalone vs live

Same convention as previous tutorials. daslang.exe runs the table once and exits at exit_requested(). daslang-live keeps the window open and reloads on source edits.

See also

Full source: modules/dasImgui/examples/tutorial/data_table.das

Sortable inventory example: modules/dasImgui/examples/features/sort_specs.das — the canonical sort_specs() reference with a multi-key comparator.

Integration tests: modules/dasImgui/tests/test_app_small_property_editor.das (uses the same data_table container surface) and modules/dasImgui/tests/test_sort_specs.das (smoke for the sortable rail).

Boost macros — the macro layer.