csharp 用于处理DataTables.net客户端表呈现插件的Ajax调用的C#代码。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 用于处理DataTables.net客户端表呈现插件的Ajax调用的C#代码。相关的知识,希望对你有一定的参考价值。

//This snkppet shows the webmethod to use
// Permission to use this code for any purpose and without fee is hereby granted.
// No warrantles.

        [WebMethod (Description = "Server Side DataTables support", EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Data(object parameters)
        {
            var req = DataTableParameters.Get(parameters);
            ...
            
            var resultSet = new DataTableResultSet();
            resultSet.draw = req.Draw;
            resultSet.recordsTotal = /* total number of records in table */
            resultSet.recordsFltered = /* number of records after search - box filtering is applied */
            
            foreach (var recordFromDb in queryDb) { /* this is pseudocode */
                var columns = new List<string>();
                columns.Add("first column value");
                columns.Add("second column value");
                columns.Add("third column value");
                /* you may add as many columns as you need. Each column is a string in the List<string> */
                resultSet.data.Add(columns);
            }
            SendResponse(HttpContext.Current.Response, result);
        }
            
        private static void SendResponse(HttpResponse response, DataTableResultSet result)
        {
            response.Clear();
            response.Headers.Add("X-Content-Type-Options", "nosniff");
            response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
            response.ContentType = "application/json; charset=utf-8";
            response.Write(result.ToJSON());
            response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
<%--  Code fragment showing an example of invoking Ajax  --%>
<%-- Notice that the parameters from DataTable must be encapsulated in 
     a Javascript object, and then stringified.
    Notice also that the name of the Javascript object ("paerameters" in this example
    has to match the parameter name in the WebMethod, or dotnet won't find the correct
    WebMethod and the Ajax call will kick back a 500 error.
    
    Permission to use this code for any purpose and without fee is hereby granted.
    No warrantles.

--%>

<script type="text/javascript" src="/assets/js/datatables.min.js"></script>
<script type="text/javascript" >
 
  $(document).ready(function () {
    $('#mytable').DataTable({
      processing: true,
      serverSide: true,
      ajax: {
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/DataTables.aspx/Data",
        data: function (d) {
          return JSON.stringify({ parameters: d });
        }
      }
    });
  });
  </script>
// Turns the Ajax call parameters into a DataTableParameter object
// Permission to use this code for any purpose and without fee is hereby granted.
// No warrantles.

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace DataTables
{

    public class DataTableParameters
    {
        public Dictionary<int, DataTableColumn> Columns;
        public int Draw;
        public int Length;
        public Dictionary<int, DataTableOrder> Order;
        public bool SearchRegex;
        public string SearchValue;
        public int Start;

        private DataTableParameters()
        {
        }

        /// <summary>
        /// Retrieve DataTable parameters from WebMethod parameter, sanitized against parameter spoofing
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static DataTableParameters Get(object input)
        {
            return Get(JObject.FromObject(input));
        }

        /// <summary>
        /// Retrieve DataTable parameters from JSON, sanitized against parameter spoofing
        /// </summary>
        /// <param name="input">JToken object</param>
        /// <returns>parameters</returns>
        public static DataTableParameters Get(JToken input)
        {
            return new DataTableParameters
            {
                Columns = DataTableColumn.Get(input),
                Order = DataTableOrder.Get(input),
                Draw = (int)input["draw"],
                Start = (int)input["start"],
                Length = (int)input["length"],
                SearchValue =
                    new string(
                        ((string)input["search"]["value"]).Where(
                            c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-').ToArray()),
                SearchRegex = (bool)input["search"]["regex"]
            };
        }
    }

    public class DataTableColumn
    {
        public int Data;
        public string Name;
        public bool Orderable;
        public bool Searchable;
        public bool SearchRegex;
        public string SearchValue;

        private DataTableColumn()
        {
        }

        /// <summary>
        /// Retrieve the DataTables Columns dictionary from a JSON parameter list
        /// </summary>
        /// <param name="input">JToken object</param>
        /// <returns>Dictionary of Column elements</returns>
        public static Dictionary<int, DataTableColumn> Get(JToken input)
        {
            return (
                (JArray)input["columns"])
                .Select(col => new DataTableColumn
                {
                    Data = (int)col["data"],
                    Name =
                        new string(
                            ((string)col["name"]).Where(
                                c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-').ToArray()),
                    Searchable = (bool)col["searchable"],
                    Orderable = (bool)col["orderable"],
                    SearchValue =
                        new string(
                            ((string)col["search"]["value"]).Where(
                                c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-').ToArray()),
                    SearchRegex = (bool)col["search"]["regex"]
                })
                .ToDictionary(c => c.Data);
        }
    }

    public class DataTableOrder
    {
        public int Column;
        public string Direction;

        private DataTableOrder()
        {
        }

        /// <summary>
        /// Retrieve the DataTables order dictionary from a JSON parameter list
        /// </summary>
        /// <param name="input">JToken object</param>
        /// <returns>Dictionary of Order elements</returns>
        public static Dictionary<int, DataTableOrder> Get(JToken input)
        {
            return (
                (JArray)input["order"])
                .Select(col => new DataTableOrder
                {
                    Column = (int)col["column"],
                    Direction =
                        ((string)col["dir"]).StartsWith("desc", StringComparison.OrdinalIgnoreCase) ? "DESC" : "ASC"
                })
                .ToDictionary(c => c.Column);
        }
    }
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
/// <summary>
///     Resultset to be JSON stringified and set back to client.
/// </summary>
/// 
namespace DataTables
{
    [Serializable]
    [SuppressMessage("ReSharper", "InconsistentNaming")]
    public class DataTableResultSet
    {
        /// <summary>Array of records. Each element of the array is itself an array of columns</summary>
        public List<List<string>> data = new List<List<string>>();

        /// <summary>value of draw parameter sent by client</summary>
        public int draw;

        /// <summary>filtered record count</summary>
        public int recordsFiltered;

        /// <summary>total record count in resultset</summary>
        public int recordsTotal;

        public string ToJSON()
        {
            return JsonConvert.SerializeObject(this);
        }
    }

    [Serializable]
    [SuppressMessage("ReSharper", "InconsistentNaming")]
    public class DataTableResultError : DataTableResultSet
    {
        public string error;
    }
}

以上是关于csharp 用于处理DataTables.net客户端表呈现插件的Ajax调用的C#代码。的主要内容,如果未能解决你的问题,请参考以下文章

Datatables + PHP:多表的服务器端处理

datatables.net 搜索框 ajax

通过 DataTables.net 分页的图像和标题

Datatables.net ajax 重新加载加载消息

使用 Datatables.net 卡住的 Bootstrap 4 工具提示

在 MVC 应用程序中使用 DataTables.NET ajax 调用访问 WebAPI 2 json