Build your web apps using Smart Custom Elements
Smart.Utilities.QuaryParser - A Dynamic LINQ Query Parser
Smart.Utilities.QuaryParser
The Smart QueryParser represents a utlity class for parsing DynamicLinq expressions to Smart.QueryBuilder value. It is used by the Smart.QueryBuilder in order to parse Linq expressions.
However the QueryParser is available as a stand alone class that extends the Smart.Utilities base class.
The QueryParser class allows to convert Smart.QueryBuilder value to DynamicLinq string expression. The Smart.QueryBuilder value represents an Array of nested Arrays where each nested Array represents a single query condition or group.
Properties
The Smart.Utilities.QuaryParser class exposes the following properties:
Property | Type | Description | Optional |
---|---|---|---|
customOperations | Array | An array of objects. Each object complies with Smart.QueryBuilder customOperations property definition. | no |
fields | Array | An array of objects. Each object complies with Smart.QueryBuilder fields property definition. | no |
dynamicField | Function | A function that complies with Smart.QueryBuilder getDynamicField property definition. The function must return a fields object. | yes |
Detailed information regarding the properties is available in the Smart.QueryBuilder API documentation on the website.
Methods
The QueryParser class also exposes the following methods:
Method | Arguments | Description | Return Type |
---|---|---|---|
toLinq | Array | Converts the array passed as an argument to DynamicLinq expression. The array should be a valid Smart.QueryBuilder value. | String |
toValue | String | Converts a string to Smart.QueryBuilder value. The string should be a valid Dynamic LINQ expression. | Array |
The QueryParser utility is part of the smart.filterbuilder.js file. In order to use it, the file must be imported first.
Here's an exmaple demo page that uses the QueryParser:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> </head> <body> <!-- scripts --> <script type="module" src="../../../source/modules/smart.querybuilder.js"></script> <script type="module" src="index.js"></script> </body> </html>
The index.js file contains the initialization code for the QueryParser:
const queryParser = new Smart.Utilities.QueryParser(); //Set the fields queryParser.fields = [{ label: 'Id', dataField: 'id', dataType: 'string', filterOperations: ['list_not_contains'] }]; //Set the customOperations queryParser.customOperations = [{ label: 'List Not Contains', name: 'list_not_contains', expressionTemplate: '{0}.Any(!it.Contains("{1}"))' }]; //Converts the linq to QueryBuilderValue const queryBuilderValue = queryParser.toValue('id.Any(!it.Contains("one"))'); //Converts the QueryBuilder value back to Linq const linq = queryParser.toLinq(queryBuilderValue);
The QueryParser class allows to set the properties directly on initialization. The constructor accepts three arguemnts: constructor(fields, customOperations, dynamicField). An instance of the class can be created like so:
const fields = [{ label: 'Id', dataField: 'id', dataType: 'string', filterOperations: ['list_not_contains'] }], customOperations = [{ label: 'List Not Contains', name: 'list_not_contains', expressionTemplate: '{0}.Any(!it.Contains("{1}"))' }]; const queryParser = new Smart.Utilities.QueryParser(fields, customOperations); //Converts the Linq to QueryBuilder value queryParser.toValue('id.Any(!it.Contains("one"))');
The dynamicField property allows to dynamically load additional fields that have not been set initially via the fields property. For example:
const dynamicField = () => ({ label: 'Id', dataField: 'id', dataType: 'string', filterOperations: ['list_not_contains'] }), queryParser = new Smart.Utilities.QueryParser([], customOperations, dynamicField); //Converts the Linq to QueryBuilder value queryParser.toValue('id.Any(!it.Contains("one"))');