|
App.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
@ViewChild('grid', {static: true})
public grid: GridComponent;
public format: object = {type: 'date', format: 'yyyy-MM-dd' };
onLoad(): void {
this.grid.element.addEventListener('keyup', (args: KeyboardEvent) => {
if ((args.target as HTMLElement).getAttribute('id') === 'toolSearch' && args.key === "Enter") {
let gridObj: Grid = this.grid;
// Regex for checking date of format – “dd/MM/yyyy”
var regex = /^\d{4}-((0\d)|(1[012]))-(([012]\d)|3[01])$/; // for making of regex into yyyy-MM-dd
// Test regex with the entered value
let input: HTMLInputElement = (args.target as HTMLInputElement);
if (regex.test(input.value)) {
var count = 0;
var predicates;
// Value is split to form JavaScript’s Date object
var dateSplit = input.value.split("-");
var dateVal = new Date(parseInt(dateSplit[0]), (parseInt(dateSplit[1]) - 1), parseInt(dateSplit[2]));
while (count < gridObj.columns.length) {
// Predicate is generated for all columns with type as ‘date’
let col: ColumnModel = gridObj.columns[count] as ColumnModel;
if (col.type === "date") {
// Predicates are generated with the column field name and date object value
predicates = (predicates === undefined) ? new Predicate(col.field, "contains", dateVal) : predicates.or(col.field, "contains", dateVal);
}
count++;
}
// Data manager is executed with grid’s datasource and query generated based on the predicates
new DataManager({ json: (gridObj.dataSource as object[]) }).executeQuery(new Query().where(predicates)).then((e: ReturnOption) => {
// The returned result is assigned to the grid datasource
gridObj.dataSource = e.result as object[];
});
} else if (input.value === "") {
if (gridObj.searchSettings.key === "") {
var data = orderDetails;
// If the input value and search key is empty the entire data source is assigned to the grid
gridObj.dataSource = DataUtil.parse.parseJson(data);
} else {
// If the input value is empty but the grid contains a search key, then another search is performed with empty value to remove the search
gridObj.search("");
}
} else {
// Grid search method is called with the input value
gridObj.search(input.value);
}
} |
|
App.component.html
<div class="control-section">
<ejs-grid #grid [dataSource]='data' (load)='onLoad($event)' height='350'>
<ng-template #toolbarTemplate let-data>
<ejs-toolbar>
<e-items>
<e-item id='collapse' prefixIcon='e-collapse'>
<ng-template #template>
<div class="e-input-group">
<input class="e-input" name='input' id="toolSearch" type="text" placeholder="Search" /> //input element is rendered
<span class="e-input-group-icon e-search-icon e-icons"></span>
</div>
</ng-template>
</e-item>
</e-items>
</ejs-toolbar>
</ng-template>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign='Right'></e-column>
</e-columns>
</ejs-grid>
</div> |