0

请帮忙

我正在使用 datatables jQuery Plugin 来显示一个表格,它显示所有条目而没有分页的问题。请帮助这是我的 index.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>
<html>    
    <head>   
        <title>jQuery DataTables/ASP.NET MVC Integration</title>
        <link href="~/Content/dataTables/demo_table.css" 
           rel="stylesheet" type="text/css" />        
          <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <script src="~/Scripts/jQuery.dataTables.min.js" 
           type="text/javascript"></script>
        <script src="~/Scripts/index.js" 
           type="text/javascript"></script>
    </head>
    <body>
        <div id="container">
            <div id="demo">
                <h2>Index</h2>
                <table id="myDataTable" class="display">
                    <thead>
                        <tr>
                            <th>firstName</th>
                            <th>lastName</th>
                        </tr>
                    </thead>
                    <tbody> 
                    </tbody>
                </table>
            </div>
        </div>
    </body>
</html>

这是我的控制器

  public ActionResult AjaxHandler(JQueryDataTableParamModel param)
        {
            List<string[]> myList = new List<string[]>();

            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" }); 
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" }); 
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" }); 
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" }); 
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" }); 
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            myList.Add(new[] { "bla", "blabla" });
            return Json(new
                            {
                                sEcho = param.sEcho,
                                iTotalRecords = myList.Count(),
                                iTotalDisplayRecords = myList.Count(),
                                aaData = myList
                            },
                        JsonRequestBehavior.AllowGet);
        }

这是js文件

$(document).ready(function () {

    var oTable = $('#myDataTable').dataTable({
        "bServerSide": true,
        "sAjaxSource": "Home/AjaxHandler", 
        "bProcessing": true,
        "aoColumns": [
                        { "sName": "firstName" },
                        { "sName": "LastName" },
                     ]
    });
});

即使我尝试修改这个 js 文件,但仍然显示所有数据而没有分页

$(文档).ready(函数 () {

var oTable = $('#myDataTable').dataTable({
    "bServerSide": true,
    "sAjaxSource": "Home/AjaxHandler", 
    "bProcessing": true,
    "aLengthMenu": [[25, 50, 75, -1], [25, 50, 75, "All"]],
    "iDisplayLength": 25,
    "aoColumns": [
                    { "sName": "firstName" },
                    { "sName": "LastName" },
                 ]
});

});

4

1 回答 1

2

看起来您没有使用“sDom”,这是一个数据表选项。详情请参考这里:https ://datatables.net/usage/options

你的 Js 文件:

$("#myDataTable").dataTable({
    "bServerSide": true,
    "sAjaxSource": "/Home/AjaxHandler",
    "bProcessing": false,
    "sPaginationType": "full_numbers",            
    "aLengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
    "iDisplayLength": 10,
    "sDom": '<"table_top"fl<"clear">>,<"table_content"t>,<"table_bottom"ip<"clear">>',
    "aoColumns": [
                    {"sName": "firstName"},
                    {"sName": "LastName"}
    ]
});

AjaxHandler 动作:

public ActionResult AjaxHandler(JQueryDataTableParamModel param)
{
    List<string[]> myList = new List<string[]>();

    myList.Add(new[]{"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] {"bla", "blabla" });
    myList.Add(new[] { "bla", "blabla" });
    myList.Add(new[] { "bla", "blabla" });
    myList.Add(new[] { "bla", "blabla" });

    var displayedLists = myList.Skip(param.iDisplayStart).Take(param.iDisplayLength);
    var result = from c in displayedLists select c;
    return Json(new
    {
        sEcho = param.sEcho,
        iTotalRecords = myList.Count(),
        iTotalDisplayRecords = myList.Count(),
        aaData = result
    },JsonRequestBehavior.AllowGet);
}
于 2013-10-26T02:09:15.360 回答