Parsing request parameters using jQuery dataTables – 3

I have been very busy recently and have not updated. I will post an article on dataTables parameter processing first.

For dataTables, when using server-side paging, multiple parameters will be passed to the server, and server-side paging processing will be performed on the server side based on these parameters. There are many parameters. For detailed description, see
Using jQuery dataTables – 2 Four Data Sources.

For the server side, these parameters obviously need to be parsed for easy use. The following code parses the request parameters into a C# object, so that the server side can use these parameters conveniently.

The code has more detailed comments, and you can also
Click here to download the code directly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace jQuery.DataTables
{
    // Sorting direction
    public enum SortDirection
    {
        Asc, // ascending order
        Desc // descending order
    }

    // Definition of sorting column
    public class SortColumn
    {
        public int Index { get; set; } // column number
        public SortDirection Direction { get; set; } // Sort direction of column
    }

    // column definition
    public class Column
    {
        public string Name { get; set; } // column name
        public bool Sortable { get; set; } // Whether it can be sorted
        public bool Searchable { get; set; } // Whether it is searchable
        public string Search { get; set; } // Search string
        public bool EscapeRegex { get; set; } // Is it regular?
    }

    public class DataTablesRequest
    {
        private HttpRequestBase request; // Internally used Request object

        public DataTablesRequest(System.Web.HttpRequestBase request) // Constructor for MVC mode
        {
            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";

            //Columns to be sorted
            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) //Constructor in standard WinForm mode
            : this(new HttpRequestWrapper(httpRequest))
        { }

        #region
        private const string sEchoParameter = "sEcho";

        // Starting index and length
        private const string iDisplayStartParameter = "iDisplayStart";
        private const string iDisplayLengthParameter = "iDisplayLength";

        //Number of columns
        private const string iColumnsParameter = "iColumns";
        private const string sColumnsParameter = "sColumns";

        // Participate in sorting sequence number
        private const string iSortingColsParameter = "iSortingCols";
        private const string iSortColPrefixParameter = "iSortCol_"; // Sort column index
        private const string sSortDirPrefixParameter = "sSortDir_"; // Sorting direction asc, desc

        // Sortability of each column
        private const string bSortablePrefixParameter = "bSortable_";

        //Global search
        private const string sSearchParameter = "sSearch";
        private const string bRegexParameter = "bRegex";

        //Search for each column
        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; }
        }

        //Columns involved in sorting
        private readonly int sortingCols;
        public int iSortingCols
        {
            get { return this.sortCols; }
        }

        // sort column
        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 Several commonly used parsing methods
        private int ParseIntParameter(string name) // Parse to integer
        {
            int result = 0;
            string parameter = this.request[name];
            if (!string.IsNullOrEmpty(parameter))
            {
                int.TryParse(parameter, out result);
            }
            return result;
        }

        private string ParseStringParameter(string name) // Parse into string
        {
            return this.request[name];
        }

        private bool ParseBooleanParameter(string name) // Parse to Boolean type
        {
            bool result = false;
            string parameter = this.request[name];
            if (!string.IsNullOrEmpty(parameter))
            {
                bool.TryParse(parameter, out result);
            }
            return result;
        }
        #endregion
    }
}

In this way, you can obtain the request parameters during use.

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