2

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;
    }
4

2 に答える 2

2

要求が DataTable から来ると、パラメーター iSortCol_0 には並べ替えられる列のインデックスが含まれ、パラメーター sSortDir_0 は並べ替え方向 (昇順、降順) を指定します。

ここで説明したように: http://rantdriven.com/post/Using-Datatablesnet-JQuery-Plug-in-with-WCF-Services.aspx

編集

iSortCol_0 整数を使用して、SQL クエリの order by 句に渡すことができます。これは順序による順序と呼ばれ、ほとんどの DB エンジンでサポートされています。iSortCol_0 がゼロベースの場合、渡す前に 1 を追加する必要があります。

于 2013-09-26T08:44:58.707 に答える