You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Assumption in this example is that your connection string is stored in application.config file under key MyConnection.
29
48
30
49
# Create ASP.NET Controller that will serve Http requests
31
50
@@ -95,4 +114,53 @@ First, you need to parse Request parameters using UriParser in order to extract
95
114
}
96
115
```
97
116
98
-
That's everything that you need to do. With three lines of code you can get OData service on any table.
117
+
That's everything that you need to do. With three lines of code you can get OData service on any table.
118
+
119
+
120
+
## Implement REST service that process JQuery DataTables Ajax request
121
+
122
+
[JQuery DataTables](https://datatables.net/) is JQuery component that enhances HTML tables and adds rich client-side functionalities such as filtering, pagination, ordering by columns, etc. JQuery DataTables component might work in two modes:
123
+
- Client-side mode where rows are loaded into the table in browser, and then all sorting, filering and pagination operations are done via JavaScript.
124
+
- Server-side mode where AJAX request with informaiton about the curent page, sort/filter condition, is sent to the server, and REST API should return results that should be shown in the table.
125
+
126
+
In order to configure JQuery DataTables in server-side processing mode, you need to put an empty HTML table in your HTML page, and specify that DataTables plugin should be applied on this page with the following options:
127
+
```
128
+
$('#mytable').DataTable({
129
+
"serverSide": true,
130
+
"ajax": "/api/People",
131
+
"columns": [
132
+
{ "data": "name", "width": "10%" },
133
+
{ "data": "surname", "width": "10%" },
134
+
{ "data": "address", "width": "50%" },
135
+
{ "data": "town", "width": "10%" }
136
+
]
137
+
});
138
+
```
139
+
Option "serverSide" will tell DataTables plugin to send AJAX request to the service that will return results that should be shown. Url of the service is defined in "ajax" option. The last option is list of the columns that shoudl be shown.
140
+
To implement REST service that handles AJAX requests that JQuery DataTables sends in server-side mode, you would need to add the TableSpec object that describes the structure of the table that will be queried (name and columns). An example is shown in the following code:
141
+
```
142
+
IQueryPipe sqlQuery = null;
143
+
144
+
TableSpec tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
145
+
146
+
public PeopleController(IQueryPipe sqlQueryService)
147
+
{
148
+
this.sqlQuery = sqlQueryService;
149
+
}
150
+
```
151
+
152
+
Now you need to create async method that will serve JQuery DataTables AJAX requests with following classes:
153
+
- UriParser that will parse Http request parameters that JQuery DataTables component sends
154
+
- QueryBuilder that will create T-SQL query that will be executed.
155
+
First, you need to parse Request parameters using UriParser in order to extract the definition of query (QuerySpec object). Then you need to use QueryBuilder to create SQL query using the QuerySpec. Then you need to provide sql query to QueryPipe that will stream results to JQuery DataTables using Response.Body:
156
+
157
+
```
158
+
// GET api/People
159
+
[HttpGet]
160
+
public async Task Get()
161
+
{
162
+
var querySpec = JQueryDataTables.UriParser.Parse(tableSpec, this.Request);
163
+
var sql = QueryBuilder.Build(querySpec, tableSpec).AsJson();
0 commit comments