Datatables のサーバー側処理は初めてで、wcf サービスを使用したサーバー側処理に適した例やサンプル コードが見つかりません。
WCF サービスに接続してデータを取得する jQuery Datatables (datatables.net) サーバー側処理を使用しようとしています。
iDisplayStart と iDisplayLength (wcf メソッドのパラメーターとして取得) を使用してページネーションを実装し、SQL クエリを (wcf メソッドで) 作成して、表示するレコード数を制限しました。
さて、問題はwcfメソッドで検索とソートをどのように実装するかです。このためには、並べ替えのためにどの列をクリックするか、SQL WHERE 句を作成するために表示される列は何かが必要です。
ここでの私のアプローチは、フロント エンドのデータ テーブルに基づいて SQL クエリを作成することです。wcfでデータテーブルのサーバー側処理を行う方法はありますか?
質問の一部が明確でない場合は、コメントしてください。
以下はフロントエンドコードです
スクリプト
$(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>
<form id="form1" runat="server">
<div>
<table id="example" width="100%">
<thead>
</thead>
<tbody>
</tbody>
</table>
</div>
</form>
</body>
WCF メソッド (バックエンド)
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;
}