Filter a list with dom and events
Live-filter a list as the user types: the smallest real fw page, with dom and events.
Apache-2.0Runs on this page
Package: @awacloud/fw
Filter a list as the user types, with an accessible live count.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Small interactive pages that need DOM helpers and clean listener lifetimes without a component framework.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.html,main.jsandstyle.css, then openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link.
What it shows
dom.query/queryAll,show/hide,classAdd/classRemove,text.events.scope(name).on(...): named listeners you can clear as a group.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { dom } from '@awacloud/fw/dom/query/dom.js';
import { events } from '@awacloud/fw/dom/query/events.js';
fw.runtime.registerAllDeep([dom, events]);
const $ = fw.runtime.resolve('dom');
const ui = fw.runtime.resolve('events').scope('filter');
const list = /** @type {HTMLElement} */ ($.query(document.body, '#items'));
const count = /** @type {HTMLElement} */ ($.query(document.body, '#count'));
/** @param {string} term */
export function applyFilter(term) {
const needle = term.trim().toLowerCase();
let shown = 0;
for (const item of $.queryAll(list, 'li')) {
const match = (item.textContent ?? '').toLowerCase().includes(needle);
if (match) { $.show(item); shown++; } else { $.hide(item); }
if (match && needle) $.classAdd(item, 'match'); else $.classRemove(item, 'match');
}
$.text(count, `${shown} shown`);
return shown;
}
ui.on('input', $.query(document.body, '#filter'), 'input', (event) => {
applyFilter(/** @type {HTMLInputElement} */ (event.target).value);
});
applyFilter('');index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Filter a list with dom and events</title>
<link rel="stylesheet" href="../../_style/examples.css">
<link rel="stylesheet" href="./style.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Filter a list</h1>
<label for="filter">Filter</label>
<input id="filter" type="search" autocomplete="off">
<ul id="items">
<li>Base64</li>
<li>CBOR</li>
<li>MessagePack</li>
<li>UTF-8</li>
</ul>
<p id="count" aria-live="polite"></p>
</main>
</body>
</html>style.css
/* SPDX-License-Identifier: Apache-2.0 */
input { display: block; inline-size: 100%; margin-block: 0.5rem; }
li.match { font-weight: 600; }