Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery数据表服务器端用SQL Server实现ASP.NET_Jquery_Asp.net_Sql Server_Datatable - Fatal编程技术网

JQuery数据表服务器端用SQL Server实现ASP.NET

JQuery数据表服务器端用SQL Server实现ASP.NET,jquery,asp.net,sql-server,datatable,Jquery,Asp.net,Sql Server,Datatable,正在寻找一个使用ASP.NET和jQuery Datatable实现的简单示例。首先,您需要了解数据表及其参数的基本知识。 在ajax上,它向目标方法发送一些参数,并获取四个参数以在数据表中显示数据。我使用了包含参数的DTParameters 行动方法: [HttpPost] public ActionResult loadData(DTParameters param) { //Us IQuerable for server

正在寻找一个使用ASP.NET和jQuery Datatable实现的简单示例。

首先,您需要了解数据表及其参数的基本知识。 在ajax上,它向目标方法发送一些参数,并获取四个参数以在数据表中显示数据。我使用了包含参数的DTParameters

行动方法:

      [HttpPost]
        public ActionResult loadData(DTParameters param)
        {

            //Us IQuerable for server side processings -filterning and pagination
            //i m just doing testing

            var ListData = getList();

            //take and skip record according to pagination
            var takeData = ListData.Skip(param.Start).Take(param.Length).ToList();

            //  IF searching value is not null
            if (param.Search.Value != null && param.Search.Value != "")
            {
                var sendData = takeData.Where(p => p.numero_picklist != null && p.numero_picklist.ToLower().Contains(param.Search.Value.ToLower())
                || p.PFIN != null && p.PFIN.ToLower().Contains(param.Search.Value.ToLower())).ToList();

                DTResult<PicklistDto> result = new DTResult<PicklistDto>
                {
                    draw = param.Draw,
                    data = sendData.ToList(),
                    recordsFiltered = ListData.Count(), // total number of record
                    recordsTotal = ListData.Count(), // total number of record
                };
                return Json(result);

            }
            else
            {
                var sendData = takeData;
                DTResult<PicklistDto> result = new DTResult<PicklistDto>
                {
                    draw = param.Draw,
                    data = sendData.ToList(),
                    recordsFiltered = ListData.Count(), // total number of record
                    recordsTotal = ListData.Count(), // total number of record
                };
                return Json(result);

            }



        } 
[HttpPost]
公共操作结果加载数据(DTParameters参数)
{
//可用于服务器端处理-筛选和分页
//我只是在做测试
var ListData=getList();
//根据页码记录并跳过记录
var takeData=ListData.Skip(param.Start).Take(param.Length).ToList();
//如果搜索值不为空
if(param.Search.Value!=null&¶m.Search.Value!=“”)
{
var sendData=takeData.Where(p=>p.numero\u picklist!=null&&p.numero\u picklist.ToLower().Contains(param.Search.Value.ToLower())
||p.PFIN!=null&&p.PFIN.ToLower().Contains(param.Search.Value.ToLower()).ToList();
DTResult结果=新的DTResult
{
draw=参数draw,
data=sendData.ToList(),
recordsFiltered=ListData.Count(),//记录总数
recordsTotal=ListData.Count(),//记录总数
};
返回Json(结果);
}
其他的
{
var sendData=takeData;
DTResult结果=新的DTResult
{
draw=参数draw,
data=sendData.ToList(),
recordsFiltered=ListData.Count(),//记录总数
recordsTotal=ListData.Count(),//记录总数
};
返回Json(结果);
}
} 
例2:

            IQueryable<ClassName> result = dbconText.tbleName ; // return type to be IQueryable
            var dtaTableResult = result.ToDataTableList(param.Draw, param.Start, param.Length);
            return dtaTableResult;



Make Extension Method ,         
 public static class QueryablExtensions
{
    public static DTResult<T> ToDataTableList<T>(this IQueryable<T>   query, int draw, int skip, int take)
    {
        var count = query.Count();
        var list = query.Skip(skip).Take(take).ToList();
        return new DTResult<T>(list, draw, count, count);
    }


 }
IQueryable结果=dbconText.tbleName;//返回类型为IQueryable
var dtatablesult=result.ToDataTableList(param.Draw、param.Start、param.Length);
返回dtaTableResult;
做延伸法,,
公共静态类QueryablExtensions
{
公共静态DTResult ToDataTableList(此IQueryable查询、int-draw、int-skip、int-take)
{
var count=query.count();
var list=query.Skip(Skip).Take(Take.ToList();
返回新的DTResult(列表、绘制、计数、计数);
}
}
Html和datatbale脚本:

        <!doctype html>
<html>
<head>
    <title>@ViewBag.Title - MVC Datatables App</title>
</head>

<body>
    <div class="container">
        <h3> Report</h3>

            <table class="table table-striped" id="datatab">
                <thead>
                    <tr>
                        <th>
                            numero_picklist
                        </th>
                        <th>
                            PFIN
                        </th>

                    </tr>
                </thead>
                <tbody>


                </tbody>
            </table>
    </div>
</body>

</html>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/dataTables.scroller.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.11.3.min.js"></script>
<script src="~/Scripts/jquery.dataTables.min.js"></script>
<script>

    //initiate dataTables plugin

   $('#datatab')
   //.wrap("<div class='dataTables_borderWrap' />")   //if you are applying horizontal scrolling (sScrollX)
   .DataTable({

       "bProcessing": true,
       "bServerSide": true,
       //"serverSide": true,
       "ajax": {
           "type": "POST",
           "url":'/DataTable/loadData',
           "contentType":'application/json; charset=utf-8',
           'data': function (data) {
               //console.log(data);
               return data = JSON.stringify(data);
           },
           //dataSrc: '',
       },

       "paging": true,
       //"deferRender": true,
       "columns": [
          { "data": "numero_picklist" },
          { "data": "PFIN" },

       ],
       "order": [0, "asc"],
       bAutoWidth: false,

   });
</script> 

@ViewBag.Title-MVC数据表应用程序
汇报
优先选择名单
普芬
//启动数据表插件
$(“#数据表”)
//.wrap(“”//如果应用水平滚动(sScrollX)
.数据表({
“bProcessing”:正确,
“bServerSide”:正确,
//“服务器端”:正确,
“ajax”:{
“类型”:“职位”,
“url”:“/DataTable/loadData”,
“contentType”:“application/json;charset=utf-8”,
“数据”:函数(数据){
//控制台日志(数据);
返回数据=JSON.stringify(数据);
},
//dataSrc:“”,
},
“分页”:正确,
//“延迟渲染”:正确,
“栏目”:[
{“数据”:“数字选择器”},
{“数据”:“PFIN”},
],
“订单”:[0,“asc”],
鲍托维兹:错,
});
创建模态类“DatatablesViewModel”,将其保存并导入控制器

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

///This view model class has been referred from example created by Marien Monnier at Soft.it. All credits to Marien for this class
namespace MVCDatatableApp.Models
{
    /// <summary>
    /// A full result, as understood by jQuery DataTables.
    /// </summary>
    /// <typeparam name="T">The data type of each row.</typeparam>
    public class DTResult<T>
    {
        /// <summary>
        /// The draw counter that this object is a response to - from the draw parameter sent as part of the data request.
        /// Note that it is strongly recommended for security reasons that you cast this parameter to an integer, rather than simply echoing back to the client what it sent in the draw parameter, in order to prevent Cross Site Scripting (XSS) attacks.
        /// </summary>
        public int draw { get; set; }

        /// <summary>
        /// Total records, before filtering (i.e. the total number of records in the database)
        /// </summary>
        public int recordsTotal { get; set; }

        /// <summary>
        /// Total records, after filtering (i.e. the total number of records after filtering has been applied - not just the number of records being returned for this page of data).
        /// </summary>
        public int recordsFiltered { get; set; }

        /// <summary>
        /// The data to be displayed in the table.
        /// This is an array of data source objects, one for each row, which will be used by DataTables.
        /// Note that this parameter's name can be changed using the ajaxDT option's dataSrc property.
        /// </summary>
        public List<T> data { get; set; }
    }

    /// <summary>
    /// The additional columns that you can send to jQuery DataTables for automatic processing.
    /// </summary>
    public abstract class DTRow
    {
        /// <summary>
        /// Set the ID property of the dt-tag tr node to this value
        /// </summary>
        public virtual string DT_RowId
        {
            get { return null; }
        }

        /// <summary>
        /// Add this class to the dt-tag tr node
        /// </summary>
        public virtual string DT_RowClass
        {
            get { return null; }
        }

        /// <summary>
        /// Add this data property to the row's dt-tag tr node allowing abstract data to be added to the node, using the HTML5 data-* attributes.
        /// This uses the jQuery data() method to set the data, which can also then be used for later retrieval (for example on a click event).
        /// </summary>
        public virtual object DT_RowData
        {
            get { return null; }
        }
    }

    /// <summary>
    /// The parameters sent by jQuery DataTables in AJAX queries.
    /// </summary>
    public class DTParameters
    {
        /// <summary>
        /// Draw counter.
        /// This is used by DataTables to ensure that the Ajax returns from server-side processing requests are drawn in sequence by DataTables (Ajax requests are asynchronous and thus can return out of sequence).
        /// This is used as part of the draw return parameter (see below).
        /// </summary>
        public int Draw { get; set; }

        /// <summary>
        /// An array defining all columns in the table.
        /// </summary>
        public DTColumn[] Columns { get; set; }

        /// <summary>
        /// An array defining how many columns are being ordering upon - i.e. if the array length is 1, then a single column sort is being performed, otherwise a multi-column sort is being performed.
        /// </summary>
        public DTOrder[] Order { get; set; }

        /// <summary>
        /// Paging first record indicator.
        /// This is the start point in the current data set (0 index based - i.e. 0 is the first record).
        /// </summary>
        public int Start { get; set; }

        /// <summary>
        /// Number of records that the table can display in the current draw.
        /// It is expected that the number of records returned will be equal to this number, unless the server has fewer records to return.
        /// Note that this can be -1 to indicate that all records should be returned (although that negates any benefits of server-side processing!)
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// Global search value. To be applied to all columns which have searchable as true.
        /// </summary>
        public DTSearch Search { get; set; }

        /// <summary>
        /// Custom column that is used to further sort on the first Order column.
        /// </summary>
        public string SortOrder
        {
            get
            {
                return Columns != null && Order != null && Order.Length > 0
                    ? (Columns[Order[0].Column].Data + (Order[0].Dir == DTOrderDir.DESC ? " " + Order[0].Dir : string.Empty))
                    : null;
            }
        }

    }

    /// <summary>
    /// A jQuery DataTables column.
    /// </summary>
    public class DTColumn
    {
        /// <summary>
        /// Column's data source, as defined by columns.data.
        /// </summary>
        public string Data { get; set; }

        /// <summary>
        /// Column's name, as defined by columns.name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Flag to indicate if this column is searchable (true) or not (false). This is controlled by columns.searchable.
        /// </summary>
        public bool Searchable { get; set; }

        /// <summary>
        /// Flag to indicate if this column is orderable (true) or not (false). This is controlled by columns.orderable.
        /// </summary>
        public bool Orderable { get; set; }

        /// <summary>
        /// Specific search value.
        /// </summary>
        public DTSearch Search { get; set; }
    }

    /// <summary>
    /// An order, as sent by jQuery DataTables when doing AJAX queries.
    /// </summary>
    public class DTOrder
    {
        /// <summary>
        /// Column to which ordering should be applied.
        /// This is an index reference to the columns array of information that is also submitted to the server.
        /// </summary>
        public int Column { get; set; }

        /// <summary>
        /// Ordering direction for this column.
        /// It will be dt-string asc or dt-string desc to indicate ascending ordering or descending ordering, respectively.
        /// </summary>
        public DTOrderDir Dir { get; set; }
    }

    /// <summary>
    /// Sort orders of jQuery DataTables.
    /// </summary>
    public enum DTOrderDir
    {
        ASC,
        DESC
    }

    /// <summary>
    /// A search, as sent by jQuery DataTables when doing AJAX queries.
    /// </summary>
    public class DTSearch
    {
        /// <summary>
        /// Global search value. To be applied to all columns which have searchable as true.
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// true if the global filter should be treated as a regular expression for advanced searching, false otherwise.
        /// Note that normally server-side processing scripts will not perform regular expression searching for performance reasons on large data sets, but it is technically possible and at the discretion of your script.
        /// </summary>
        public bool Regex { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
///这个视图模型类是从Marien Monnier在Soft.it创建的示例中引用的。这门课的所有学分都归Marien
名称空间MVCDatatableApp.Models
{
/// 
///正如jQuery DataTables所理解的那样,这是一个完整的结果。
/// 
///每行的数据类型。
公共类DTResult
{
/// 
///此对象响应的绘图计数器-来自作为数据请求一部分发送的绘图参数。
///请注意,出于安全原因,强烈建议您将此参数强制转换为整数,而不是简单地将其在draw参数中发送的内容回显给客户端,以防止跨站点脚本(XSS)攻击。
/// 
公共int draw{get;set;}
/// 
///过滤前的记录总数(即数据库中的记录总数)
/// 
public int recordsTotal{get;set;}
/// 
///过滤后的记录总数(即应用过滤后的记录总数,而不仅仅是此数据页返回的记录数)。
/// 
公共int记录过滤{get;set;}
/// 
///要在表中显示的数据。
///这是一个数据源对象数组,每行一个,将由数据表使用。
///请注意,可以使用ajaxDT选项的dataSrc属性更改此参数的名称。
/// 
公共列表数据{get;set;}
}
/// 
///可以发送到jQuery DataTables进行自动处理的附加列。
/// 
公共抽象类DTRow
{
/// 
///将dt tag tr节点的ID属性设置为此值
/// 
公共虚拟字符串DT_RowId
{
获取{return null;}
}
/// 
///将此类添加到dt tag tr节点
/// 
公共虚拟字符串DT_RowClass
{
获取{return null;}
}