-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
Bob edited this page Mar 11, 2015
·
18 revisions
At the very minimum, the following elements and configuration are needed to display and make use of the ISBjs widget:
- a div or other dom element to contain the button to begin or reset a search
- a <ul> to contain the dynamic search elements
- code to instantiate and configure ISBjs
- code to add the filters you want to expose to the end user
//For each field you want to expose to the end user, add an array of filter properties, providing\
//the display value, the actual field name to be used in the database query, the data type
//and nullability. Nullability is only needed for dates that map to nullable DateTime? in
//.net classes
isb.addFilterProperty([
{ "display": "OrderID", "value": "OrderID", "dataType": "number" },
{ "display": "CustomerID", "value": "CustomerID", "dataType": "text" },
{ "display": "OrderDate", "value": "OrderDate", "dataType": "date", "nullable": true }]);
}
Next, you must provide some way to trigger a parsing of the widget. The return value of the parse function is an array of string that you can use in a dynamic linq to entities query (other parsings experimental at this time). You will utilize the first element of this array only (until support for parameters is added).
- Provide a way to initiate the query, such as a button
- Call Parse on ISBjs
- Send the result of parsing to the server for processing
- Handle the returned data; for instance, display in a list or table
Execute Query
...
````
```
<script>
...
//Send the query to your handler. For example, to send the query to a
//web api controller named "Orders" via AngularJs:
function OrdersController($scope, $http) {
$scope.orders = [];
$scope.parseandquery = function () {
$scope.orders = [];
try {
var linqresults = isb.parse("l2e").join(); //parse, and specifically ask for the Linq to Entities format "l2e"
//the example web api expects x-www-form-urlencoded key "Query" with value of
// the parse result. Your implementation may vary.
$http.post('http://isb.contrivedexample.com/api/Orders',
{'Query' : document.querySelector("#dynLinq").value}).success(
function (data, status, headers, config) {
$scope.orders = data;
$scope.loading = false;
}).error(function (data, status, headers, config) {
$scope.loading = false;
alert(status);
});
} catch (e) {
alert(e.message);
}
}
}
}
</script>
```
And finally the WebApi Controller implementation. You can see the use of the System.Linq.Dynamic namespace and how the entity framework query simply takes a string in the Where clause:
```
using com.crowcoder.isbjs;
using IsbApi.Models;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Http;
OrderID | CustomerID | EmployeeID |
---|---|---|
{{o.OrderID}} | {{o.CustomerID}} | {{o.EmployeeID}} |
namespace IsbApi.Controllers { public class OrdersController : ApiController { private readonly NorthwindContext nwCtx = new NorthwindContext();
//IsbQuery is a simple POCO that has one string property named "Query"
//to match up with the POST data in the ajax call above.
public IEnumerable<Orders> Post([FromBody] IsbQuery l2eQuery)
{
var qry = nwCtx.Orders.AsQueryable();
if (l2eQuery != null)
{
qry = qry.Where(l2eQuery.Query);
}
return qry.ToList();
}
} }
### To work with a live demo, debug and view the source please visit:
[ISBjs Playground](http://www.yardsaleseason.com/isb/Playground.html)