Skip to content

Filter and sort a list

A list over a host collection shows whatever the host’s fetch returns. To filter, search or sort it, let the host do the work: declare the query as the fetch’s inputs, and run the fetch again when the person changes it. The ticket queue example does all of this.

Each choice is a typed input on the collection’s fetch. A fixed set of choices is a string with an enum; free text is a plain string the host interprets:

{
"capabilities": {
"fetch": {
"inputs": {
"status": {
"kind": "string",
"required": true,
"enum": [
"all",
"open",
"pending",
"closed"
]
},
"sort": {
"kind": "string",
"required": true,
"enum": [
"newest",
"oldest"
]
},
"search": {
"kind": "string",
"required": false,
"maxLength": 60
}
}
}
}
}

The document keeps the query in its own state twice: requested holds what the person has asked for, and applied what the list on screen was fetched with. Both start the same, and startup fetches with the requested query:

{
"query": {
"requested": {
"status": "all",
"sort": "newest",
"search": ""
},
"applied": {
"status": "all",
"sort": "newest",
"search": ""
}
},
"lifecycle": {
"startup": [
{
"action": "fetch",
"params": {
"object": "tickets",
"inputs": {
"status": {
"$state": "/query/requested/status"
},
"sort": {
"$state": "/query/requested/sort"
},
"search": {
"$state": "/query/requested/search"
}
}
}
}
]
}
}

Mica checks every input against the declaration before the host is asked, so a value outside the enum never reaches it. What the inputs mean — which rows match a search, how “newest” is ordered — is entirely the host’s.

A fixed set of choices is what a Choice is for. Give it the same values as the input’s enum, bind it to the requested query, and let its commit — which fires on each change a person makes, after the choice is written — fetch and apply:

The queue’s status filter is that Choice, shown as one row of buttons (display: "segmented") with the same values as the input’s enum:

{
"status-choice": {
"type": "Choice",
"props": {
"label": "Status",
"display": "segmented",
"options": [
{
"value": "all",
"label": "All"
},
{
"value": "open",
"label": "Open"
},
{
"value": "pending",
"label": "Pending"
},
{
"value": "closed",
"label": "Closed"
}
],
"required": false,
"helpText": null,
"value": {
"$bindState": "/query/requested/status"
}
},
"children": [],
"on": {
"commit": [
{
"action": "fetch",
"params": {
"object": "tickets",
"inputs": {
"status": {
"$state": "/query/requested/status"
},
"sort": {
"$state": "/query/requested/sort"
},
"search": {
"$state": "/query/requested/search"
}
}
}
},
{
"action": "setState",
"params": {
"statePath": "/query/applied",
"value": {
"$state": "/query/requested"
}
}
}
]
}
}
}

The Choice shows the requested choice, so while a fetch is refused as busy it already shows the new one; the list and anything reading applied keep showing what is on screen. Use display: "select" or "combobox" for a longer list, and a Toggle for a two-way switch whose input is a boolean.

A sort control that offers the other order — “Show oldest first” while the list is newest — is two Buttons, each shown while the applied query has the other order. Each press saves its order into the requested query, fetches with every input read from the requested query, then copies the whole requested query to the applied one.

Everything that shows the current query reads applied, so the controls show the last choice whose request was accepted:

  • Rapid presses on different controls combine: Pending, then Show oldest first, gives pending tickets, oldest first, because every fetch reads all requested choices.
  • A press or commit refused as busy stops its event before the copy, so its choice stays requested but not applied: the controls keep showing the last accepted choice, and the next request includes it.
  • A superseded press never runs its copy, so the later press’s copy wins.
  • A fetch that fails still resolves, so the copy runs and the error text shows. The runtime keeps the previous rows on a failure, so the list hides while its latest fetch has failed. Once a retry starts loading, the previous rows can show again until the new answer arrives:
{
"ticket-list": {
"type": "Stack",
"props": {
"direction": "vertical",
"gap": "sm",
"align": "stretch",
"justify": "start",
"wrap": false,
"padding": "none",
"surface": null
},
"children": [
"ticket-card"
],
"visible": {
"$state": "/meta/objects/tickets/operations/fetch/phase",
"neq": "error"
},
"repeat": {
"statePath": "/objects/tickets/data",
"key": "id"
}
}
}
{
"sort-newest": {
"type": "Button",
"props": {
"label": "Show oldest first",
"variant": "outline",
"size": "md",
"disabled": false,
"fullWidth": false
},
"children": [],
"on": {
"press": [
{
"action": "setState",
"params": {
"statePath": "/query/requested/sort",
"value": "oldest"
}
},
{
"action": "fetch",
"params": {
"object": "tickets",
"inputs": {
"status": {
"$state": "/query/requested/status"
},
"sort": {
"$state": "/query/requested/sort"
},
"search": {
"$state": "/query/requested/search"
}
}
}
},
{
"action": "setState",
"params": {
"statePath": "/query/applied",
"value": {
"$state": "/query/requested"
}
}
}
]
},
"visible": {
"$state": "/query/applied/sort",
"eq": "newest"
}
},
"sort-oldest": {
"type": "Button",
"props": {
"label": "Show newest first",
"variant": "outline",
"size": "md",
"disabled": false,
"fullWidth": false
},
"children": [],
"on": {
"press": [
{
"action": "setState",
"params": {
"statePath": "/query/requested/sort",
"value": "newest"
}
},
{
"action": "fetch",
"params": {
"object": "tickets",
"inputs": {
"status": {
"$state": "/query/requested/status"
},
"sort": {
"$state": "/query/requested/sort"
},
"search": {
"$state": "/query/requested/search"
}
}
}
},
{
"action": "setState",
"params": {
"statePath": "/query/applied",
"value": {
"$state": "/query/requested"
}
}
}
]
},
"visible": {
"$state": "/query/applied/sort",
"eq": "oldest"
}
}
}

An Input bound to the requested query’s search field fetches and applies on commit: when the person presses Enter, or leaves the field after changing it. It never fetches per keystroke:

{
"search": {
"type": "Input",
"props": {
"label": "Search",
"placeholder": "Subject words, like refund",
"inputType": "text",
"required": false,
"helpText": "Press Enter, or leave the field, to search. The host decides what matches.",
"maxLength": 60,
"value": {
"$bindState": "/query/requested/search"
},
"error": {
"$state": "/meta/objects/tickets/operations/fetch/error/fieldErrors/search"
}
},
"children": [],
"on": {
"commit": [
{
"action": "fetch",
"params": {
"object": "tickets",
"inputs": {
"status": {
"$state": "/query/requested/status"
},
"sort": {
"$state": "/query/requested/sort"
},
"search": {
"$state": "/query/requested/search"
}
}
}
},
{
"action": "setState",
"params": {
"statePath": "/query/applied",
"value": {
"$state": "/query/requested"
}
}
}
]
}
}
}

A fetch dispatched by a Button press or a form control’s commit replaces one that an earlier press or commit started and that is still loading. The older fetch rejects as superseded and its answer is dropped, so a person who presses Open, then Closed before the first answer arrives always ends up with the closed tickets. A superseded fetch also stops the rest of its event’s actions.

A fetch that startup or the host itself started is never replaced: a press while it is loading is rejected as busy, and pressing again once it has finished works. The refused choice is still requested, so any later press or commit sends it.

Loading, error and “no matches” text read the fetch’s phase and the row count, exactly as in Show a list. Because the host filtered the list, the count is the number of matches:

{
"no-matches": {
"type": "Text",
"props": {
"content": "No tickets match. Try another status or search.",
"size": "md",
"tone": "muted",
"align": "left",
"italic": false
},
"children": [],
"visible": [
{
"$state": "/meta/objects/tickets/operations/fetch/phase",
"eq": "success"
},
{
"$state": "/meta/objects/tickets/count",
"eq": 0
}
]
}
}

A query input Mica refuses before asking the host, such as a search longer than its maxLength, reports why at the input’s field error, which the search Input’s error shows under the box. Give the search Input the same maxLength so nobody can type past it; the author sets it, since Mica never copies a limit from the declaration:

{
"search": {
"type": "Input",
"props": {
"label": "Search",
"placeholder": "Subject words, like refund",
"inputType": "text",
"required": false,
"helpText": "Press Enter, or leave the field, to search. The host decides what matches.",
"maxLength": 60,
"value": {
"$bindState": "/query/requested/search"
},
"error": {
"$state": "/meta/objects/tickets/operations/fetch/error/fieldErrors/search"
}
},
"children": [],
"on": {
"commit": [
{
"action": "fetch",
"params": {
"object": "tickets",
"inputs": {
"status": {
"$state": "/query/requested/status"
},
"sort": {
"$state": "/query/requested/sort"
},
"search": {
"$state": "/query/requested/search"
}
}
}
},
{
"action": "setState",
"params": {
"statePath": "/query/applied",
"value": {
"$state": "/query/requested"
}
}
}
]
}
}
}

A Show when condition inside the template can hide rows whose field does not equal a value, and that value may come from document state:

"ticket-card": {
"type": "Stack",
"visible": { "$item": "status", "eq": { "$state": "/showOnly" } }
}

This hides rows; it does not remove them. /meta/objects/{alias}/count still counts every row, so the document cannot tell when nothing is left to show and has no “no matches” state. A condition holds one comparison, so “show all” is a different element, not a value of /showOnly. Sorting and text search in the document are not available: order and search belong in the host query.