Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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
Javascript 在让服务器端处理与Datatables和MVC4一起工作时遇到问题_Javascript_Jquery_Json_Asp.net Mvc 4_Datatable - Fatal编程技术网

Javascript 在让服务器端处理与Datatables和MVC4一起工作时遇到问题

Javascript 在让服务器端处理与Datatables和MVC4一起工作时遇到问题,javascript,jquery,json,asp.net-mvc-4,datatable,Javascript,Jquery,Json,Asp.net Mvc 4,Datatable,您好,我们目前有一个MVC4应用程序,它将大量带有缩略图的记录引入到jQuery Datatables驱动的视图中。当然,在加载所有缩略图时,加载速度非常慢。为了解决这个问题,我试图让它使用服务器端处理,一次只能得到10个结果 为此,我遵循了Datatables文档并对Javascript进行了如下更改: <script> $(document).ready(function () { //Create an array with the values of all the

您好,我们目前有一个MVC4应用程序,它将大量带有缩略图的记录引入到jQuery Datatables驱动的视图中。当然,在加载所有缩略图时,加载速度非常慢。为了解决这个问题,我试图让它使用服务器端处理,一次只能得到10个结果

为此,我遵循了Datatables文档并对Javascript进行了如下更改:

<script>
$(document).ready(function () {
    //Create an array with the values of all the checkboxes in a column */
    $.fn.dataTable.ext.order['dom-checkbox'] = function (settings, col) {
        return this.api().column(col, { order: 'index' }).nodes().map(function (td, i) {
            return $('input', td).prop('checked') ? '1' : '0';
        });
    }

    /* Initialise the table with the required column ordering data types */
    $(document).ready(function () {
        $('.passes').dataTable({

            "serverSide": true,
            "processing": true,
            "ajax": "/Pass/Datatable",



            "columns": [
                { "orderDataType": "dom-text-numeric "},
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-text-numeric" },
                { "orderDataType": "dom-checkbox" },
                { "orderDataType": "dom-text-numeric" }
            ],

            "order": [[5, "desc"]]


        });
    });
});

$(文档).ready(函数(){
//使用列中所有复选框的值创建一个数组*/
$.fn.dataTable.ext.order['dom-checkbox']=函数(设置,列){
返回此.api()列(col,{order:'index'}).nodes().map(函数(td,i){
返回$('input',td).prop('checked')?'1':'0';
});
}
/*使用所需的列排序数据类型初始化表*/
$(文档).ready(函数(){
$('.passs').dataTable({
“服务器端”:正确,
“处理”:对,
“ajax”:“/Pass/Datatable”,
“栏目”:[
{“orderDataType”:“dom文本数字”},
{“orderDataType”:“dom文本数字”},
{“orderDataType”:“dom文本数字”},
{“orderDataType”:“dom文本数字”},
{“orderDataType”:“dom文本数字”},
{“orderDataType”:“dom复选框”},
{“orderDataType”:“dom文本数字”}
],
“订单”:[[5,“说明”]]
});
});
});

这是从控制器中的一个方法获取AJAX源代码,在这里我返回一个JSON结果。我从SQL表中获取信息,然后使用在Github上找到的Datatable解析器将获取的信息解析为JSON,如下所示:

        public JsonResult Datatable(int passesPerPage, HttpRequest Request)
    {
        //default number of passes will be set to 10
        passesPerPage = 10;


        var passes = db.Cards.OrderBy(c => c.Vendor.Name); // fetch the data from data model
        passes.Take(passesPerPage); //Takes the number of passes requested per page so as not to overload the server
        var parser = new DataTablesParser<Card>(Request, passes); //pass the parser the data to parse it into JSON
        return Json(parser.Parse()); // have the parser parse the request parameters and return the Json Result

    }
publicjsonresult数据表(int-passerpage,HttpRequest请求)
{
//默认通过次数将设置为10
passerpage=10;
var passs=db.Cards.OrderBy(c=>c.Vendor.Name);//从数据模型中获取数据
passs.Take(passerpage);//获取每页请求的通过次数,以避免服务器过载
var parser=new datatableparser(请求,传递);//向解析器传递数据以将其解析为JSON
返回Json(parser.Parse());//让解析器解析请求参数并返回Json结果
}

但当我测试网页时,它仍然加载所有记录,并且根本没有分页功能。我想知道我到底做错了什么,因为除了非故意的行为,我没有得到任何类型的错误。如果我能得到任何关于如何调试这个的帮助,或者如果有人能看到我做错了什么,我将非常感激

对分页的datatable属性进行一些更改:

$('.passes').dataTable({
        "serverSide": true,
        "processing": true,
        "ajax": "/Pass/Datatable",
        "bDestroy": true,
        "iDisplayLength": 10,
        "aLengthMenu": [5, 10, 25, 50, 100],
        "sPaginationType": "full_numbers",
        "columns": [
            { "orderDataType": "dom-text-numeric " },
            { "orderDataType": "dom-text-numeric" },
            { "orderDataType": "dom-text-numeric" },
            { "orderDataType": "dom-text-numeric" },
            { "orderDataType": "dom-text-numeric" },
            { "orderDataType": "dom-checkbox" },
            { "orderDataType": "dom-text-numeric" }
        ],
        "order": [[5, "desc"]]
    });

首先,对不起我的英语。 如果这有帮助,那么我可以说,DataTablesParser自动从请求中获取数据,并根据需要对数据进行PAR。来自Datatables.js的请求有一些属性,如startlength(据我所知,它是解析器所需的属性)(),Dataparser根据需要获取这些数据来解析数据。所以你不需要这么做

var passes = db.Cards.OrderBy(c => c.Vendor.Name);
passes.Take(passesPerPage); 
只需使用:

public JsonResult Datatable()
{
    var parser = new DataTablesParser<Card>(Request, SomeIQueryableData); 
    return Json(parser.Parse()); 
}
公共JsonResult数据表() { var parser=新的DataTablesParser(请求,SomeIQueryableData); 返回Json(parser.Parse()); }
谢谢,但它仍在加载10多条记录,我还收到一个确认框,说明DataTables出现AJAX错误。我觉得我试图从控制器中提取的内容有问题。当我检查服务器错误日志时,这是我从数据表返回的ASP.NET错误。值不能为null。参数名称:String,导致错误的行是:第46行:返回Json(parser.Parse());//让解析器解析请求参数并返回Json结果如果出现ajax错误,那么可能是因为ajax返回的结果不是插件所期望的格式。您需要深入研究dataTables tutotial,以了解如何使用预期的格式保存数据。。。