Getting Started with QueryBuilder Web Component
Smart UI Web Components work with current evergreen browsers and Node 18+ for local tooling; pin package versions to match your project policy.
Smart UI is distributed as the smart-webcomponents NPM package. You can also use the full download from the Download page.
Quick start
- Install the package:
npm install smart-webcomponents
- Load the QueryBuilder module (ES module script):
<script type="module" src="node_modules/smart-webcomponents/source/modules/smart.querybuilder.js"></script>
- Add the default stylesheet (prefer angular.json / bundler entry in app codebases; for plain HTML use a link):
<link rel="stylesheet" type="text/css" href="node_modules/smart-webcomponents/source/styles/smart.default.css" />
- Add markup in one of two ways - semantic custom element (the component tag is in your HTML) or a host
div(you mount programmatically with appendTo):
Semantic element (id matches the selector in Smart()):
<smart-query-builder id="querybuilder"></smart-query-builder>
Host container (id matches appendTo on Smart.QueryBuilder):
<div id="querybuilderContainer"></div>
- Initialize after the module loads: define a const querybuilderOptions object, then either bind with Smart('#querybuilder', ...) on the semantic tag or use new Smart.QueryBuilder({ ...querybuilderOptions, appendTo: '#querybuilderContainer' }) on the host
div:
<script type="module"> import 'node_modules/smart-webcomponents/source/modules/smart.querybuilder.js'; const querybuilderOptions = { allowDrag: true, fields: [ { label: 'Id', dataField: 'id', dataType: 'number' }, { label: 'Product', dataField: 'productName', dataType: 'string' }, { label: 'Unit Price', dataField: 'price', dataType: 'number' }, { label: 'Purchased', dataField: 'purchased', dataType: 'datetime' }, { label: 'Available', dataField: 'available', dataType: 'boolean' } ] }; // Option A - semantic <smart-query-builder> with id="querybuilder" Smart('#querybuilder', class { get properties() { return querybuilderOptions; } }); // Option B - host div id="querybuilderContainer" // const querybuilderInstance = new Smart.QueryBuilder({ // ...querybuilderOptions, // appendTo: '#querybuilderContainer' // }); // Option C - constructor(selector, options), then append the returned element yourself // const myQueryBuilder = new Smart.QueryBuilder('#querybuilder', querybuilderOptions); // document.body.appendChild(myQueryBuilder); </script>
Uncomment Option B when you use the host
div; use Option A when you use the semantic element. The Runtime cookbook also documents new Smart.QueryBuilder('#querybuilder', querybuilderOptions) with appendChild, and document.createElement('smart-query-builder') with .props or Object.assign (all are valid patterns; do not combine overlapping patterns for the same instance unless you intend multiple components). - Serve the folder over HTTP (or use your bundler dev server) and open the page.
Runtime cookbook
Alternative creation patterns and imperative APIs. These are all valid ways to create Smart UI components: semantic markup + Smart(); new Smart.QueryBuilder({ ...options, appendTo: '#...' }); new Smart.QueryBuilder('#querybuilder', querybuilderOptions) plus appendChild on the returned element; and document.createElement('smart-query-builder') then assigning options via .props or Object.assign on the element.
Constructor with a selector string and options, then append the returned element (for example const myQueryBuilder = new Smart.QueryBuilder('#querybuilder', querybuilderOptions)):
const querybuilderOptions = {
allowDrag: true,
fields: [
{ label: 'Id', dataField: 'id', dataType: 'number' },
{ label: 'Product', dataField: 'productName', dataType: 'string' },
{ label: 'Unit Price', dataField: 'price', dataType: 'number' },
{ label: 'Purchased', dataField: 'purchased', dataType: 'datetime' },
{ label: 'Available', dataField: 'available', dataType: 'boolean' }
]
};
const myQueryBuilder = new Smart.QueryBuilder('#querybuilder', querybuilderOptions);
document.body.appendChild(myQueryBuilder);
Create with document.createElement('smart-query-builder'), assign properties (same as any custom element), then append:
const querybuilderOptions = {
allowDrag: true,
fields: [
{ label: 'Id', dataField: 'id', dataType: 'number' },
{ label: 'Product', dataField: 'productName', dataType: 'string' },
{ label: 'Unit Price', dataField: 'price', dataType: 'number' },
{ label: 'Purchased', dataField: 'purchased', dataType: 'datetime' },
{ label: 'Available', dataField: 'available', dataType: 'boolean' }
]
};
const querybuilder = document.createElement('smart-query-builder');
Object.assign(querybuilder, querybuilderOptions);
document.body.appendChild(querybuilder);
Host on a div with appendTo (import the module, then instantiate when the document is ready; the container id must match appendTo):
import "../../source/modules/smart.querybuilder.js";
document.readyState === 'complete' ? init() : window.addEventListener('load', init);
function init() {
const querybuilderOptions = {
allowDrag: true,
fields: [
{ label: 'Id', dataField: 'id', dataType: 'number' },
{ label: 'Product', dataField: 'productName', dataType: 'string' },
{ label: 'Unit Price', dataField: 'price', dataType: 'number' },
{ label: 'Purchased', dataField: 'purchased', dataType: 'datetime' },
{ label: 'Available', dataField: 'available', dataType: 'boolean' }
]
};
const querybuilder = new Smart.QueryBuilder({
...querybuilderOptions,
appendTo: '#querybuilderContainer'
});
}
Append to the DOM:
const container = document.getElementById('querybuilder-container');
container.appendChild(querybuilder);
Remove from the DOM:
querybuilder.remove();
Set a property:
querybuilder.disabled = true; querybuilder.theme = 'dark';
Get a property value:
const isDisabled = querybuilder.disabled; const currentTheme = querybuilder.theme;
Invoke a method:
querybuilder.refresh(); querybuilder.focus();
Add event listener:
querybuilder.addEventListener('change', (event) => {
console.log('change triggered:', event.detail.item);
});
Remove event listener:
const handleQueryBuilderEvent = (event) => {
console.log('change triggered:', event.detail.item);
};
querybuilder.addEventListener('change', handleQueryBuilderEvent);
querybuilder.removeEventListener('change', handleQueryBuilderEvent);
Common Use Cases
-
Get filter expression
Retrieve the current query as a filter
const filter = queryBuilder.value;
-
Set initial conditions
Pre-populate the query builder
queryBuilder.value = [ ['productName', 'contains', 'Tea'], 'and', ['price', '>', 5] ];
Troubleshooting
- How do I get the filter expression?
- Access queryBuilder.value to get the current conditions as an array that can be used for filtering data.
- How do I add custom operators?
- Define custom operators in the customOperations property for specific field types.
Accessibility
The QueryBuilder component follows WAI-ARIA best practices:
- Keyboard navigation - Tab, Arrow keys, Enter, and Escape are supported
- ARIA roles - Appropriate roles and labels are applied automatically
- Focus management - Visible focus indicators for keyboard users
- Screen readers - State changes are announced to assistive technology
- High contrast - Supports Windows High Contrast Mode and forced colors
For custom labeling, set aria-label or aria-labelledby attributes on the component.
Supported stacks: Smart UI targets Angular 17+, React 18+, Vue 3+, Node 18 LTS, and evergreen browsers; pin exact package versions to your org policy.