GithubHelp home page GithubHelp logo

emerbrito / ng-expression-builder Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 4.0 3.35 MB

An expression builder built with Angular Material.

License: MIT License

JavaScript 4.38% CSS 2.60% HTML 13.86% TypeScript 79.16%

ng-expression-builder's Introduction

Angular Material Expression Builder

Expression Builder

A simple to use expression builder created with Angular Material components.
Outputs a data structure (JSON) representing the expression which you can use to build queries and filters.

npm install @emerbrito/expression-builder

Getting Started

The first step is to configure the fields available to the end user when building expressions.
A field is defined by the Field interface:

export interface Field {
    label: string,
    name: string,    
    type: FieldType,
    values?: OptionValue[]
}

Field type is used for input validation and to render the appropriated control.
Property values is optional. When specified a dropdown with the options will be rendered indifferent of the field type, which still applies to validation.

Bellow is an example of some field definitions:

const fields = [
    {
        name: "firstName",
        label: "First Name",
        type: FieldType.Text
    },
    {
        "name": "married",
        "label": "Married",
        "type": FieldType.Boolean,
        "values": [
            {
                "value": "true ",
                "label ": "Yes"
            },
            {
                "value": "false ",
                "label ": "No"
            }
        ]
    }
];

Use the component's input property to pass the field configurations:

<expression-builder 
    [fields]="fields" 
    [data]="exp" 
    (valuechange)="changeHandler($event)">
</expression-builder>

Component properties

@Input() fields

Type: Field[]
Required. Array containing the fields available trough the expression builder.

@Input() data

Type: QueryExpression Optional.
Initial expression.

@Output() valuechange

Argument: ExpressionChangeEvent
Fires every time the expression changes. Contains the current expression and a flag indicating whether or not it is in a valid state.

Sample Expression

Bellow is a sample of the expression produce by the component:

{
  "operator": "and",
  "rules": [
    {
      "fieldName": "voter",
      "condition": "eq",
      "value": "true"
    },
    {
      "operator": "or",
      "rules": [
        {
          "fieldName": "married",
          "condition": "eq",
          "value": "true"
        },
        {
          "fieldName": "age",
          "condition": "ge",
          "value": "21"
        }
      ]
    }
  ]
}

Before You Start

This package dependens on Angular Material.
Before you start make sure you add it to your project.

ng add @angular/material

More details on Angular Material installation can be found here.

ng-expression-builder's People

Contributors

emerbrito avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.