# Connect a grid and form

A list and an edit form should agree about the record they show. Aster gives them a shared context, while an edit model keeps uncommitted input separate from the stored record.

## Choose the detail layout

Use **inline row editing** for a generated form when the visible cells cover
the fields and can provide their editors. Enable Grid's row editing so a
double-click, Enter or F2 starts editing. A read-only row that already shows
all its details needs no View action. Honour an explicitly configured detail
view.

When the detail adds fields, context or controls, start with **row expansion**
if it fits comfortably beneath the row. Choose a split when the detail needs
more room or the user will move through many records in one session.

Show the main fields as soon as the row opens. In Aster Form, omit the section
title to lay out the fields directly. Use another disclosure only for secondary
content that people sometimes need, such as a change history.

| Layout | When to use it | Aster components |
| --- | --- | --- |
| Row expansion | A compact form, preview or a few related fields that add to what the row shows. | Supply an editor with `Grid.expansion`. Grid provides its own actions for the expanded row. |
| Vertical master–detail | The list and details both need the full width. Details sit below the list. | Use Split with `orientation = "vertical"` so people can resize the two panes. |
| Horizontal master–detail | People need to keep the list in view while reading or editing substantial detail, such as a ticket conversation. | Use a horizontal Split with the list at the start and details at the end. Let it stack in narrow containers. |

Name the divider for what it resizes and give Split a `persist` key so it
remembers the chosen size. Use [Tabs](/playground/tabs.html) within the detail
when the record has meaningful sections. Keep one edit model across those
sections. The [Grid expansion reference](/docs/reference/api/attr-Grid.expansion.md)
shows how to supply the detail editor.

### Actions for an expanded row

Supply `dirty`, `save` and `discard` on the editor returned by
`Grid.expansion.content`. Grid shows **Close details** while the row is clean.
When it changes, Grid shows **Save**, **Revert changes** and **Cancel**.
Save closes the row after a successful write. Revert changes restores the
saved values and keeps it open. Cancel drops the draft and closes it.
Aster Form supplies **Revert** next to each changed field.

Keep the form body for fields, validation and related content. Let Grid ask about
unsaved changes when someone presses Escape or opens another row. Use
`rowControls` to add actions such as **Edit** or **View** on a closed row.
Return `undefined` for any row that should use Grid's defaults, as the Tasks
and Model pages in the showcase do. [Grid row controls](/docs/reference/api/attr-Grid.rowControls.md)

## Run the workflow

[Open the editable grid-and-form example](/playground/). Choose a project, edit its name, and save. The grid reflects the saved change because it reads the same record store. You can edit the example’s source and run it again.

The playground’s default example, **A grid, a form and a picker on the same
data**, demonstrates the vertical split. Drag the divider to give the grid or
form more room.

## Publish the selection

Create a `PageScope` and provide it on a common ancestor. Give the grid a publication name, `projects`. The grid publishes its selection there; another view can observe `projects.key` without querying the grid’s DOM.

When a grid needs visible selection controls, set `selectionColumn: true`
(`.selectionColumn=${true}` in Lit). Grid gives the checkboxes an aligned
leading column, independent of displayed fields and their widths. A compact
editor opened with an Edit or View action can rely on row expansion alone.

```javascript
const scope = new PageScope();
provideScope(out, scope);
```

```html
<aster-grid .ctx=${ctx} .set=${"Projects"}
  .selectionColumn=${true} .publish=${"projects"} .label=${"Projects"}>
</aster-grid>
```

The HTML above is a Lit template fragment. Import `html`, `render`, `effect`, `PageScope`, `provideScope` and `EditModel` from the Aster module, and use the [getting started](/docs/guide/getting-started.md) setup for `ctx` and `out`.

## Create an edit model for the selected record

An effect observes the published key. Dispose the previous edit model when selection changes, then create a model for the selected record.

```javascript
let edit;
const stop = effect(() => {
  const key = scope.value("projects.key").value;
  edit?.dispose();
  edit = key === undefined
    ? undefined
    : new EditModel(ctx, "Projects", key);
  view();
});
```

`view()` is your rendering function. In the runnable example it binds `edit` to an `aster-form`, with a Save action calling `edit.save()`. The full working source is in the playground.

> Selection changes in this introductory example discard the previous draft. For an application with unsaved work, add a leave/selection guard before changing the active record. Choose that policy explicitly.

## Keep drafts separate from saved data

The form writes to the edit model. A successful save updates the shared store, which updates the grid. Validation and save errors belong to the edit workflow; applications should present them and keep the draft available for correction.

Dispose edit models and the page scope, and stop your effects, when their owning screen is removed. See [EditModel](/docs/reference/api/class-EditModel.md) and [PageScope](/docs/reference/api/class-PageScope.md) for lifecycle details.

## Keep live refreshes stable

Treat initial loading, a background refresh and a new query as different states.
During a same-query refresh, render the existing rows and mark the view busy;
show an error and retry action alongside them if the refresh fails. Reserve
loading placeholders for positions that have never loaded. Replacing a whole
view with a loading message can remove focus, collapse its geometry and restart
chart animations.

Aster's collection refresh keeps confirmed positions until the visible pages
and count can be replaced together. An uncertain sort move keeps the row at its
last confirmed position while the server settles its location. Known deletions
and records that definitely leave a filter disappear immediately. A new query
clears its old rows so they cannot appear to answer the new filter.

Grid, TileGrid and Listbox expose busy state without removing loaded content.
Key lists retain their identity when membership and order are unchanged; record
values still update through the shared store. Grouped results retain unchanged
group objects. Custom views should read the rows independently of loading status,
keep stable record/group keys and avoid rebuilding their component instance.
For charts, see [incremental chart updates](/docs/guide/connected-charts.md#include-accessible-alternatives-and-motion-preferences).

## Where to go next

Continue with [Compose a workspace](/docs/guide/declarative-workspace.md) to declare the
controls, commands and lazy editor around a shared model. If the ownership terms
are unfamiliar, revisit [the shared data model](/docs/guide/data-model.md).

[Explore the live example](/playground/) for the complete code, including a related-person picker. Use the [form reference](/docs/reference/api/class-Form.md) for metadata-driven sections and the [Grid reference](/docs/reference/api/class-Grid.md) for view configuration.
