0.2.8 • Published 6 years ago

angular2-query-builder-wlogic v0.2.8

Weekly downloads
8
License
MIT
Repository
github
Last release
6 years ago

Angular-QueryBuilder

A modernized Angular 4+ query builder based on jQuery QueryBuilder. Support for heavy customization with Angular components and provides a flexible way to handle custom data types.

Getting Started

Install

npm install angular2-query-builder

Demo

Play with the demo here.

See the Plunker

Documentation

Documentation link

Examples

Basic Usage

app.component.html
<query-builder [(ngModel)]='query' [config]='config'></query-builder>
app.component.ts
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'}
      ]
    }
  }
}

Custom Input Components

app.component.html
<query-builder [(ngModel)]='query' [config]='config'>
  <ng-container *queryInput="let rule; type: 'date'">
    <custom-datepicker [(ngModel)]="rule.value"></custom-datepicker>
  </ng-container>
</query-builder>
app.component.ts
query = {
  condition: 'and',
  rules: [
    {field: 'birthday', operator: '=', value: new Date()}
  ]
};

config: QueryBuilderConfig = {
  fields: {
    birthday: {name: 'Birthday', type: 'date', operators: ['=', '<=', '>']
      defaultValue: (() => return new Date())
    },
  }
}

Custom Styling (with Bootstrap 4)

Bootstrap demo.

app.component.html
<query-builder [(ngModel)]='query' [config]='config' [classNames]='classNames'></query-builder>
app.component.ts
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'
}

Customizing with Angular Material

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

<query-builder [(ngModel)]='query' [config]='config'>
  <ng-container *queryButtonGroup="let ruleset; let addRule=addRule; let addRuleSet=addRuleSet; let removeRuleSet=removeRuleSet">
    <button mat-button (click)="addRule()">+ Rule</button>
    <button mat-button (click)="addRuleSet()">+ Ruleset</button>
    <button mat-button (click)="removeRuleSet()">- Ruleset</button>
  </ng-container>
  <ng-container *queryRemoveButton="let rule; let removeRule=removeRule">
    <button mat-icon-button color="accent" (click)="removeRule(rule)">
      <mat-icon>remove</mat-icon>
    </button>
  </ng-container>
  <ng-container *querySwitchGroup="let ruleset">
    <mat-radio-group *ngIf="ruleset" [(ngModel)]="ruleset.condition">
      <mat-radio-buttonvalue="and">And</mat-radio-button>
      <mat-radio-button value="or">Or</mat-radio-button>
    </mat-radio-group>
  </ng-container>
  <ng-container *queryField="let rule; let fields=fields; let changeField=changeField">
    <mat-form-field>
      <mat-select [(ngModel)]="rule.field" (ngModelChange)="changeField($event, rule)">
        <mat-option *ngFor="let field of fields" [value]="field.value">{{field.name}}</mat-option>
      </mat-select>
    </mat-form-field>
  </ng-container>
  <ng-container *queryOperator="let rule; let operators=operators">
    <mat-form-field>
      <mat-select [(ngModel)]="rule.operator">
        <mat-option *ngFor="let value of operators" [value]="value">{{value}}</mat-option>
      </mat-select>
    </mat-form-field>
  </ng-container>
  <!-- Override input component for 'boolean' type -->
  <ng-container *queryInput="let rule; type: 'boolean'">
    <mat-checkbox [(ngModel)]="rule.value"></mat-checkbox>
  </ng-container>
  <!-- Override input component for 'category' type -->
  <ng-container *queryInput="let rule; let field=field; let options=options; type: 'category'">
    <mat-form-field>
      <mat-select [(ngModel)]="rule.value" [placeholder]="field.name">
        <mat-option *ngFor="let opt of options" [value]="opt.value">
          {{ opt.name }}
        </mat-option>
      </mat-select>
    </mat-form-field>
  </ng-container>
  ...
</query-builder>

Property Bindings Quick Reference

See documentation for more details on interfaces and properties.

query-builder

NameTypeRequiredDefaultDescription
allowRulesetbooleanOptionaltrueDisplays the + Ruleset button if true.
classNamesobjectOptionalCSS class names for different child elements in query-builder component.
configQueryBuilderConfigRequiredConfiguration object for the main component.
dataRulesetOptionalDEPRECATED (Use ngModel or value instead.)
ngModelRulesetOptionalObject that stores the state of the component. Supports 2-way binding.
operatorMap{ [key: string]: string[] }OptionalUsed to map field types to list of operators.
valueRulesetOptionalObject that stores the state of the component.

Structural Directives

Use these directives to replace different parts of query builder with custom components.

queryInput

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
fieldFieldCurrent field object which contains the field's value and name
optionsOption[]List of options for the field, returned by getOptions

queryOperator

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
operatorsstring[]List of operators for the field, returned by getOperators

queryField

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
fieldsField[]List of fields for the component, specified by config
changeField(fieldName: string, rule: Rule) => voidFunction to handle changes to the field component

querySwitchGroup

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules

queryButtonGroup

Context NameTypeDescription
$implicitRuleSetCurrent rule set object which contain a list of child rules
addRule() => voidFunction to handle adding a new rule
addRuleSet() => voidFunction to handle adding a new rule set
removeRuleSet() => voidFunction to handle removing the current rule set

queryRemoveButton

Context NameTypeDescription
$implicitRuleCurrent rule object which contains the field, value, and operator
removeRule(rule: Rule) => voidFunction to handle removing a rule

Dependencies

  • Angular 4+

That's it.

Workflow

See the angular-library-seed project for details on how to build and run tests.