I'm new to Datatables server-side processing and I do not find a suitable example or sample code for server-side processing with wcf service.
I'm trying to use jQuery Datatables (datatables.net) server-side processing that connects to WCF Service to get data.
I've implemented pagination using iDisplayStart and iDisplayLength (that I get as parameters to the wcf method) to construct sql query (in wcf method) to limit no of records to be displayed.
Now, the question is how to implement search and sorting in wcf method. For this, I need which column is clicked for sorting and what are the columns being displayed to construct the sql WHERE clause.
Here, my approach is to construct the sql query based on the datatables at the front end. Is it the way to get the datatables server-side processing done with wcf?
If any part of the question is not clear, please comment.
Following is the front-end code
The script
$(document).ready(function () {
$('#example').dataTable({
"aoColumns": [
{ "sTitle": "#", "sName": "ID", "mData": "ID"},
{ "sTitle": "PIN Number", "sName": "PIN", "mData": "PIN" },
{ "sTitle": "Amount (Rs.)", "sName": "Amount", "mData": "Amount" }
],
"sPaginationType": "full_numbers",
"bJQueryUI": true,
"bSort": true,
"bProcessing": true,
"bServerSide": true,
"bAutoWidth": true,
"sAjaxSource": "http://localhost:61216/datatabletestservice.svc/gettable",
"fnServerData": function (sSource, aoData, fnCallback) {
$.ajax({
"datatType": 'json',
"contentType": 'application/json',
"url": sSource,
"data": aoData,
"success": function (msg) {
var json = $.parseJSON(msg);
fnCallback(json);
}
})
},
});
});
</script>
Body
<body>
<form id="form1" runat="server">
<div>
<table id="example" width="100%">
<thead>
</thead>
<tbody>
</tbody>
</table>
</div>
</form>
</body>
WCF Method (Back-End)
public string GetTable(int iDisplayStart, int iDisplayLength, string sSearch, bool bEscapeRegex, int iColumns, int iSortingCols, int iSortCol_0, string sSortDir_0, int sEcho)
{
string query;
DataTable dt;
DateTime t1 = DateTime.Now;
string connectionstring = "server=my_server;database=my_db;uid=myuser;password=mypassword;";
query = "SELECT SQL_CALC_FOUND_ROWS * FROM voucher LIMIT " + iDisplayStart + ", " + iDisplayLength;
dt = MySqlHelper.ExecuteDatatable(connectionstring, query);
int totalRows = Convert.ToInt32(MySqlHelper.ExecuteScalar(connectionstring, "SELECT FOUND_ROWS()"));
string jsonString = JsonUtils.GetPlainJsonDataByDataTable(dt);
var result = JsonUtils.GetObjectFromJson<dynamic>(jsonString);
string test = "{" +
"\"sEcho\": \"" + sEcho + "\", " +
"\"iTotalRecords\": \"" + totalRows + "\", " +
"\"iTotalDisplayRecords\": \"" + totalRows + "\", " +
"\"aaData\": " + result +
"}";
return test;
}