A modernized Angular 2+ query builder based on jquery QueryBuilder
npm install angular2-query-builder-wlogicnpm install angular2-query-builder##### app.component.html
``html`app.component.ts
##### `javascript
query = {
condition: 'and',
rules: [
{field: 'age', operator: '<=', value: 'Bob'},
{field: 'gender', operator: '>=', value: 'm'}
]
};
config: QueryBuilderConfig = {
fields: {
age: {name: 'Age', type: 'number'},
gender: {
name: 'Gender',
type: 'category',
options: [
{name: 'Male', value: 'm'},
{name: 'Female', value: 'f'}
]
}
}
}
`
##### app.component.html`html`
##### app.component.ts`javascript
query = {
condition: 'and',
rules: [
{field: 'birthday', operator: '=', value: new Date()}
]
};
config: QueryBuilderConfig = {
fields: {
birthday: {name: 'Birthday', type: 'date', operators: ['=', '<=', '>']
defaultValue: (() => return new Date())
},
}
}
`
##### app.component.html`html`app.component.ts
##### `javascript`
classNames: QueryBuilderClassNames = {
removeIcon: 'fa fa-minus',
addIcon: 'fa fa-plus',
button: 'btn',
buttonGroup: 'btn-group',
rightAlign: 'order-12 ml-auto',
switchRow: 'd-flex px-2',
switchGroup: 'd-flex align-items-center',
switchRadio: 'custom-control-input',
switchLabel: 'custom-control-label',
switchControl: 'custom-control custom-radio custom-control-inline',
row: 'row p-2 m-1',
rule: 'border',
ruleSet: 'border',
invalidRuleSet: 'alert alert-danger',
operatorControl: 'form-control',
operatorControlSize: 'col-auto px-0',
fieldControl: 'form-control',
fieldControlSize: 'col-auto',
inputControl: 'form-control',
inputControlSize: 'col-auto'
}
Example of how you can completely customize the query component with another library like Angular Material. For the full example, please look at the source code provided in the demo.
#### app.component.html
`html`
{{ opt.name }}
...
See documentation for more details on interfaces and properties.
#### query-builderallowRuleset
|Name|Type|Required|Default|Description|
|:--- |:--- |:--- |:--- |:--- |
||boolean|Optional|true| Displays the + Ruleset button if true. |classNames
||object|Optional|| CSS class names for different child elements in query-builder component. |config
||QueryBuilderConfig|Required|| Configuration object for the main component. |data
|| Ruleset |Optional|| DEPRECATED (Use ngModel or value instead.) |ngModel
|| Ruleset |Optional|| Object that stores the state of the component. Supports 2-way binding. |operatorMap
||{ [key: string]: string[] }|Optional|| Used to map field types to list of operators. |value
|| Ruleset |Optional|| Object that stores the state of the component. |
Use these directives to replace different parts of query builder with custom components.
#### queryInput
|Context Name|Type|Description|
|:--- |:--- |:--- |
|$implicit|Rule|Current rule object which contains the field, value, and operator|field
||Field|Current field object which contains the field's value and name|options
||Option[]|List of options for the field, returned by getOptions|
#### queryOperator$implicit
|Context Name|Type|Description|
|:--- |:--- |:--- |
||Rule|Current rule object which contains the field, value, and operator|operators
||string[]|List of operators for the field, returned by getOperators|
#### queryField$implicit
|Context Name|Type|Description|
|:--- |:--- |:--- |
||Rule|Current rule object which contains the field, value, and operator|fields
||Field[]|List of fields for the component, specified by config|changeField
||(fieldName: string, rule: Rule) => void|Function to handle changes to the field component|
#### querySwitchGroup$implicit
|Context Name|Type|Description|
|:--- |:--- |:--- |
||RuleSet|Current rule set object which contain a list of child rules|
#### queryButtonGroup$implicit
|Context Name|Type|Description|
|:--- |:--- |:--- |
||RuleSet|Current rule set object which contain a list of child rules|addRule
||() => void|Function to handle adding a new rule|addRuleSet
||() => void|Function to handle adding a new rule set|removeRuleSet
||() => void|Function to handle removing the current rule set|
#### queryRemoveButton$implicit
|Context Name|Type|Description|
|:--- |:--- |:--- |
||Rule|Current rule object which contains the field, value, and operator|removeRule
||(rule: Rule) => void`|Function to handle removing a rule|
That's it.