冠军

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

使用 jQuery dataTables - 3 解析请求参数

最近比较忙,一直没有更新,先发一篇 dataTables 参数处理。
对于 dataTables 来说,当使用服务器端分页的时候,会向服务器传递多个参数,在服务器端根据这些参数来进行服务器端的分页处理。这些参数比较多,详细地说明见 使用 jQuery dataTables - 2 四种数据来源
对于服务器端来说,显然需要将这些参数进行解析,以方便使用,下面的代码将请求参数解析为一个 C# 的对象,这样,服务器端就可以方便地使用这些参数了。
代码由有比较详细的注释,也可以点击这里直接下载代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace jQuery.DataTables
{
    // 排序的方向
    public enum SortDirection
    {
        Asc,    // 升序
        Desc    // 降序
    }
 
    // 排序列的定义
    public class SortColumn
    {
        public int Index { get; set; }                  // 列序号
        public SortDirection Direction { get; set; }    // 列的排序方向
    }
 
    // 列定义
    public class Column
    {
        public string Name { get; set; }        // 列名
        public bool Sortable { get; set; }      // 是否可排序
        public bool Searchable { get; set; }    // 是否可搜索
        public string Search { get; set; }      // 搜索串
        public bool EscapeRegex { get; set; }   // 是否正则
    }
 
    public class DataTablesRequest
    {
        private HttpRequestBase request;        // 内部使用的 Request 对象
 
        public DataTablesRequest(System.Web.HttpRequestBase request)    // 用于 MVC 模式下的构造函数
        {
            this.request = request;
 
            this.echo = this.ParseStringParameter(sEchoParameter);
            this.displayStart = this.ParseIntParameter(iDisplayStartParameter);
            this.displayLength = this.ParseIntParameter(iDisplayLengthParameter);
            this.sortingCols = this.ParseIntParameter(iSortingColsParameter);
 
            this.search = this.ParseStringParameter(sSearchParameter);
            this.regex = this.ParseStringParameter(bRegexParameter) == "true";
 
            // 排序的列
            int count = this.iSortingCols;
            this.sortColumns = new SortColumn[count];
            for (int i = 0; i < count; i++)
            {
                SortColumn col = new SortColumn();
                col.Index = this.ParseIntParameter(string.Format("iSortCol_{0}", i));
                col.Direction = SortDirection.Asc;
                if (this.ParseStringParameter(string.Format("sSortDir_", i)) == "desc")
                    col.Direction = SortDirection.Desc;
                this.sortColumns[i] = col;
            }
 
            this.ColumnCount = this.ParseIntParameter(iColumnsParameter);
 
            count = this.ColumnCount;
            this.columns = new Column[count];
 
            string[] names = this.ParseStringParameter(sColumnsParameter).Split(',');
 
            for (int i = 0; i < count; i++)
            {
                Column col = new Column();
                col.Name = names[i];
                col.Sortable = this.ParseStringParameter(string.Format("bSortable_{0}", i)) == "true";
                col.Searchable = this.ParseStringParameter(string.Format("bSearchable_{0}", i)) == "true";
                col.Search = this.ParseStringParameter(string.Format("sSearch_{0}", i));
                col.EscapeRegex = this.ParseStringParameter(string.Format("bRegex_{0}", i)) == "true";
                columns[i] = col;
            }
        }
        public DataTablesRequest(HttpRequest httpRequest)       // 标准的 WinForm 方式下的构造函数
            : this(new HttpRequestWrapper(httpRequest))
        { }
 
        #region
        private const string sEchoParameter = "sEcho";
 
        // 起始索引和长度
        private const string iDisplayStartParameter = "iDisplayStart";
        private const string iDisplayLengthParameter = "iDisplayLength";
 
        // 列数
        private const string iColumnsParameter = "iColumns";
        private const string sColumnsParameter = "sColumns";
 
        // 参与排序列数
        private const string iSortingColsParameter = "iSortingCols";
        private const string iSortColPrefixParameter = "iSortCol_";         // 排序列的索引
        private const string sSortDirPrefixParameter = "sSortDir_";         // 排序的方向 asc, desc
 
        // 每一列的可排序性
        private const string bSortablePrefixParameter = "bSortable_";
 
        // 全局搜索
        private const string sSearchParameter = "sSearch";
        private const string bRegexParameter = "bRegex";
 
        // 每一列的搜索
        private const string bSearchablePrefixParameter = "bSearchable_";
        private const string sSearchPrefixParameter = "sSearch_";
        private const string bEscapeRegexPrefixParameter = "bRegex_";
        #endregion
 
        private readonly string echo;
        public string sEcho
        {
            get { return echo; }
        }
 
        private readonly int displayStart;
        public int iDisplayStart
        {
            get { return this.displayStart; }
        }
 
        private readonly int displayLength;
        public int iDisplayLength
        {
            get { return this.displayLength; }
        }
 
        // 参与排序的列
        private readonly int sortingCols;
        public int iSortingCols
        {
            get { return this.sortingCols; }
        }
 
        // 排序列
        private readonly SortColumn[] sortColumns;
        public SortColumn[] SortColumns
        {
            get { return sortColumns; }
        }
 
        private readonly int ColumnCount;
        public int iColumns
        {
            get { return this.ColumnCount; }
        }
 
        private readonly Column[] columns;
        public Column[] Columns
        {
            get { return this.columns; }
        }
 
        private readonly string search;
        public string Search
        {
            get { return this.search; }
        }
 
        private readonly bool regex;
        public bool Regex
        {
            get { return this.regex; }
        }
 
        #region 常用的几个解析方法
        private int ParseIntParameter(string name)          // 解析为整数
        {
            int result = 0;
            string parameter = this.request[name];
            if (!string.IsNullOrEmpty(parameter))
            {
                int.TryParse(parameter, out result);
            }
            return result;
        }
 
        private string ParseStringParameter(string name)    // 解析为字符串
        {
            return this.request[name];
        }
 
        private bool ParseBooleanParameter(string name)     // 解析为布尔类型
        {
            bool result = false;
            string parameter = this.request[name];
            if (!string.IsNullOrEmpty(parameter))
            {
                bool.TryParse(parameter, out result);
            }
            return result;
        }
        #endregion
    }
}

这样在使用中就可以这样来获取请求参数了。

jQuery.DataTables.DataTablesRequest param
= new jQuery.DataTables.DataTablesRequest(this.Request);

posted on   冠军  阅读(11899)  评论(11编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示