Property Grid
Overview
A small and simple property grid, written in pure Vanilla JavaScript (web components), inspired by jqPropertyGrid.
With this PropertyGrid control you can pass object and allow users to edit the properties with the data type-specific editors. The component offers the ability to group and sort its items, use custom editors per data type, work with and without configuration...etc.
Installation
Install with npm:
npm i clean-web-ui-property-grid --save
Usage
Import
If you are using typescript:
import {PropertyGrid} from 'clean-web-ui-property-grid';
With JavaScript, just import the script in the html body:
<script src="clean-web-ui-property-grid.js"></script>
Getting Started
Basic
You can use by inserting web component in html:
<property-grid id="pg1"></property-grid>
With JS:
const pg1 = document.getElementById('pg1');
pg1.selectedObject = pgData;
Advanced
More complex example would be to pass config and options to the grid.
In HTML:
<div id="propertyGridWithConfig"></div>
In Js/Typescript:
import {createPropertyGrid} from 'clean-web-ui-property-grid';
var pgOptions = {
hasGroups: true,
propertySort: true
};
var pgConfig = {
filter: { group: 'Behavior', name: 'Filter', type: 'boolean' },
filterSize: {
group: 'Behavior',
name: 'Filter size',
type: 'number',
options: { min: 0, max: 500, step: 10 },
}
};
var pgData = {
filter: true,
filterSize: 200
};
createPropertyGrid('propertyGridWithConfig', pgConfig, pgOptions, pgData);
document.getElementById('pgpropertyGridWithConfig').addEventListener('valueChange', (v) => console.log(v));
API
Properties
config - PropertyGridConfig | null
This is the metadata object that describes the target object properties.
Here you can pass and configure how the property grid editors will work like: control type, min/max values...etc.
{
"browsable": boolean
"group": string
"name": string
"type": 'text' | 'number' | 'boolean' | 'color' | 'options' | 'label'
"description": string
"showHelp": boolean
"items": string[]
"options": Object
}
options - PropertyGridOptions | null
The options that are passed to the property grid control to configure how the grid will work. Here you pass options that configure the grid itself, not the individual controls. You can pass options like: show/hide groups, sort method...etc.
{
"hasGroups": boolean
"propertySort": boolean | Function
"onValueChange": FormControl
"controls": Function
"enabled": boolean
"toolbarVisible": boolean
}
selectedObject - Object
The is the object that you want to edit in the grid. If no config is passed to the grid, the grid will try to create config based on object properties and value type.
disabled - boolean
Gets or sets a value indicating whether the control can respond to user interaction.
Methods
render() - void
Build the config for the grid, render controls on UI and attach event listeners. Called internally by the grid when selectedObject is changed.
getValues() - Object
Returns the current values for all properties in the grid.
destroy() - void
Destroys all elements and removes all event listners from elements.
Called internally by the grid when the component is about to be destroyed.
Events
valueChanged - CustomEvent
Fired when the value is changed for some of controls in the property grid.
Event payload:
{name: 'controlname', value: 'new value'}
Hooks
onValueChange(event) - Object | boolean | void
Hook that is called before value is changed. Usefull if you want to intercept the original valueChange event.
Styling
Available CSS variables
--property-grid-header-background: #E0ECFF;
--property-grid-header-border: 1px dotted #95B8E7;
--property-grid-table-row-group-background: #E0ECFF;
--property-grid-table-row-group-font-weight: bold;
--property-grid-table-row-hover: #f0f5fd;
--property-grid-cell-border: 1px dotted #ccc;