ASP.NET Core 3.1 中的数据表服务器端处理 [英] Datatables server-side processing in ASP.NET Core 3.1

查看:22
本文介绍了ASP.NET Core 3.1 中的数据表服务器端处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 AJAX 调用的数据表 的新手.我需要为数千条记录实现服务器端处理.我正在学习教程,但我对服务器端处理感到困惑.

I am new to Datatables with AJAX calls. I need to implement server-side processing for thousands of records. I was following a tutorial but I got confused somewhere with server side processing.

我在尝试从数据源呈现数据时遇到错误.让我发布所有相关代码,希望您能帮助我确定我哪里出错了.由于我是 Datatables 和 Ajax 的新手,我将欣赏更多带有示例的答案、带有代码的文本答案或指向演示 ASP.NET CORE 3.1 中的服务器端处理的教程的链接.

I am getting an error while trying to render the data from the data source. Let me post all the relevant code and hope that you help me identify where I am getting it wrong. Since I am new to Datatables and Ajax, I will appreciate more answers with examples, textual answers accompanied by code or link(s) to a tutorial demostrating server side processing in ASP.NET CORE 3.1.

感谢您的时间和帮助.

注意.我从教程中获取了实现和配置,并尝试对其进行自定义.

NB. I took the implementation and configurations from a tutorial and tried to customize it.

以下是 HTML 表格及其模型:

@model IEnumerable<StudentApplications>
<table id="custom-datatable" class="mb-5 display table table-bordered" style="width:100%">
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.ApplicationId)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Firstname)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Surname)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.ApplicationFor)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.SubmissionDate)
                        </th>
                        <th></th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.ApplicationId)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Firstname)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Surname)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.ApplicationFor)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.SubmissionDate)
                        </th>
                        <th></th>
                    </tr>
                </tfoot>
            </table>

以下是将 HTML 表初始化为数据表和一些配置的 javascript 文件:

$(document).ready(function () {
    $('#custom-datatable').DataTable({

        // Design Assets
        stateSave: true,
        autoWidth: true,
        // ServerSide Setups
        processing: true,
        serverSide: true,
        // Paging Setups
        paging: true,
        // Searching Setups
        searching: { regex: true },
        // Ajax Filter
        ajax: {
            url: "/myapplications/data",
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: function (d) {
                return JSON.stringify(d);
            }
        },
        // Columns Setups
        columns: [
            { data: "ApplicationId" },
            { data: "Firstname" },
            { data: "Surname" },
            { data: "ApplicationFor" },
            { data: "SubmissionDate" }

            //// For Student.Id
            //{ "data": "applicationId", "name": "ApplicationId" },
            //// For Student.Firstname
            //{ "data": "firstname", "name": "Firstname" },
            //// For Student.Surname
            //{ "data": "surname", "name": "Surname" },
            //// For Student.ApplicationFor
            //{ "data": "applicationFor", "name": "ApplicationFor" },
            //// For Student.SubmissionDate
            //{ "data": "submissionDate", "name": "SubmissionDate" }
        ],
        // Column Definitions
        columnDefs: [
            { targets: "no-sort", orderable: false },
            { targets: "no-search", searchable: false },
            {
                targets: "trim",
                render: function (data, type, full, meta) {
                    if (type === "display") {
                        data = strtrunc(data, 10);
                    }

                    return data;
                }
            },
            { targets: "date-type", type: "date-eu" },
            {
                targets: 10,
                data: null,
                orderable: false
            },
        ]
    });

});

获取数据的方法如下:

// loading datatable
        [HttpPost]
        [Route("myapplications/data")]
        public async Task<IActionResult> Data([FromBody] DtParameters dtParameters)
        { 
            searchBy = dtParameters.Search?.Value;

            // if we have an empty search then just order the results by Id ascending
            var orderCriteria = "ApplicationId";
            var orderAscendingDirection = true;

            if (dtParameters.Order != null)
            {
                // in this example we just default sort on the 1st column
                orderCriteria = dtParameters.Columns[dtParameters.Order[0].Column].Data;
                orderAscendingDirection = dtParameters.Order[0].Dir.ToString().ToLower() == "asc";
            }

            var result = context.AspNetStudentApplications.AsQueryable();

            if (!string.IsNullOrEmpty(searchBy))
            {
                result = result.Where(r => r.ApplicationId != null && r.ApplicationId.ToUpper().Contains(searchBy.ToUpper()) ||
                                           r.Firstname != null && r.Firstname.ToUpper().Contains(searchBy.ToUpper()) ||
                                           r.Surname != null && r.Surname.ToUpper().Contains(searchBy.ToUpper()) ||
                                           r.ApplicationFor != null && r.ApplicationFor.ToUpper().Contains(searchBy.ToUpper()) ||
                                           r.SubmissionDate != null && r.SubmissionDate.ToUpper().Contains(searchBy.ToUpper()));
            }

            result = orderAscendingDirection ? result.OrderByDynamic(orderCriteria, DtOrderDir.Asc) : result.OrderByDynamic(orderCriteria, DtOrderDir.Desc);

            // now just get the count of items (without the skip and take) - eg how many could be returned with filtering
            var filteredResultsCount = await result.CountAsync();
            var totalResultsCount = await context.AspNetStudentApplications.CountAsync();

            return Json(new DtResult<StudentApplications>
            {
                Draw = dtParameters.Draw,
                RecordsTotal = totalResultsCount,
                RecordsFiltered = filteredResultsCount,
                Data = await result
                    .Skip(dtParameters.Start)
                    .Take(dtParameters.Length)
                    .ToListAsync()
            });
        }

下面是 DtParameters 类和其他与 Datatables 相关的类:

/// <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>
    [JsonProperty("draw")]
    public int Draw { get; set; }

    /// <summary>
    /// Total records, before filtering (i.e. the total number of records in the database)
    /// </summary>
    [JsonProperty("recordsTotal")]
    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>
    [JsonProperty("recordsFiltered")]
    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 ajax option's dataSrc property.
    /// </summary>
    [JsonProperty("data")]
    public IEnumerable<T> Data { get; set; }

    /// <summary>
    /// Optional: If an error occurs during the running of the server-side processing script, you can inform the user of this error by passing back the error message to be displayed using this parameter.
    /// Do not include if there is no error.
    /// </summary>
    [JsonProperty("error", NullValueHandling = NullValueHandling.Ignore)]
    public string Error { get; set; }

    public string PartialView { 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>
    [JsonProperty("DT_RowId")]
    public virtual string DtRowId => null;

    /// <summary>
    /// Add this class to the dt-tag tr node
    /// </summary>
    [JsonProperty("DT_RowClass")]
    public virtual string DtRowClass => null;

    /// <summary>
    /// Add the data contained in the object to the row using the jQuery data() method to set the data, which can also then be used for later retrieval (for example on a click event).
    /// </summary>
    [JsonProperty("DT_RowData")]
    public virtual object DtRowData => null;

    /// <summary>
    /// Add the data contained in the object to the row dt-tag tr node as attributes.
    /// The object keys are used as the attribute keys and the values as the corresponding attribute values.
    /// This is performed using using the jQuery param() method.
    /// Please note that this option requires DataTables 1.10.5 or newer.
    /// </summary>
    [JsonProperty("DT_RowAttr")]
    public virtual object DtRowAttr => 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 => Columns != null && Order != null && Order.Length > 0
        ? (Columns[Order[0].Column].Data +
           (Order[0].Dir == DtOrderDir.Desc ? " " + Order[0].Dir : string.Empty))
        : null;

    /// <summary>
    /// For Posting Additional Parameters to Server
    /// </summary>
    public IEnumerable<string> AdditionalValues { get; set; }

}

/// <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>
    /// Search value to apply to this specific column.
    /// </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; }
}

这是我的空表:

以下是我在尝试加载数据时遇到的错误:

我哪里弄错了?

这里是发生异常的地方:

@HMZ 实现,我看到空记录(下图):

我看到了这个提醒

卡在处理中

当行数改变时,我看到这个空表:

推荐答案

您的代码中需要改进的地方:

Something need to be improved in your code:

  1. asp.net core中的DataTable默认渲染json数据的首字母是小写,所以需要修改如下:

  1. DataTable in asp.net core render the json data with a lowercase initial letter by default,so you need to change like below:

列:[{ 数据:applicationId"},{ 数据:名字"},{数据:姓氏"},{ 数据:applicationFor"},{ 数据:提交日期"}]

columns: [ { data: "applicationId" }, { data: "firstname" }, { data: "surname" }, { data: "applicationFor" }, { data: "submissionDate" } ]

默认的 DataTable 包含分页、排序和搜索.

The default DataTable contains the paging,sorting and searching.

当我测试你的代码时,它会在前端收到以下错误信息(你可以在浏览器中按 F12 来检查错误信息).不确定你是否会得到这个错误,因为DataTable的版本可能不同:

When i testing your code,it would get the following error message in the frontend(You could press F12 in browser to check the error message).Not sure whether you would get this error because the version of DataTable may be different:

类型错误:无法设置未定义的属性nTf"

TypeError: Cannot set property ‘nTf’ of undefined

我搜索错误并得出结论,它是由表尾中th 个元素的数量与表头中th 个元素的数量不同引起的.但是我发现你的代码中 theadtfoot 的数量是一样的.

I search the error and conclude the result that it is caused by number of th elements in the table footer differs from number of th elements in the table header.But I find the numbers of thead and tfoot are the same in your code.

与传统的DataTable有点不同的是你在theadtfoot的两端都有空的th.所以我有一个尝试,然后当我删除 tfoot 中的 th 但保留 thead 中的 th 时它起作用:

A bit different from the traditional DataTable is that you have empty th in both end of the thead and tfoot.So I have a try,then it works when i remove the th in tfoot but maintain the th in thead:

<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ApplicationId)
        </th>
        ...
        <th></th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ApplicationId)
        </th>
        ....
        @*<th></th>*@  //remove this...
    </tr>
</tfoot>

这是一个如下所示的工作演示:

Here is a working demo like below:

型号:

public class StudentApplications
{
    public int ApplicationId { get; set; }
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public string ApplicationFor { get; set; }
    public string SubmissionDate { get; set; }
}

查看:

@model IEnumerable<StudentApplications>
<table id="custom-datatable" class="mb-5 display table table-bordered" style="width:100%">
     <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ApplicationId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Firstname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Surname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ApplicationFor)
            </th>
            <th>
                Date
            </th>
            <th></th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ApplicationId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Firstname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Surname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ApplicationFor)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.SubmissionDate)
            </th>
            @*<th></th>*@
        </tr>
    </tfoot>
</table>
@section Scripts
{
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
    <script>
        $(document).ready(function () {
            $('#custom-datatable').DataTable({
                ajax: {
                    url: "/Home/data",
                    type:"POST"
                },
                columns: [
                    { data: "applicationId" },
                    { data: "firstname" },
                    { data: "surname" },
                    { data: "applicationFor" },
                    { data: "submissionDate" }
                ]

            });

        });
    </script>
}

控制器:

[HttpPost]
public ActionResult Data()
{
    var model = new List<StudentApplications>()
    {
        new StudentApplications(){ ApplicationId=1, ApplicationFor="aa", Firstname="Rena", SubmissionDate="2020-8-9", Surname="Ni"},
        new StudentApplications(){ ApplicationId=2, ApplicationFor="bb", Firstname="Sherry", SubmissionDate="2020-10-7", Surname="Chen"},
        new StudentApplications(){ ApplicationId=3, ApplicationFor="cc", Firstname="Xing", SubmissionDate="2020-5-10", Surname="Zou"},
        new StudentApplications(){ ApplicationId=4, ApplicationFor="dd", Firstname="a", SubmissionDate="2020-5-10", Surname="Zou"},
        new StudentApplications(){ ApplicationId=5, ApplicationFor="ee", Firstname="b", SubmissionDate="2020-5-10", Surname="sd"},
        new StudentApplications(){ ApplicationId=6, ApplicationFor="ff", Firstname="c", SubmissionDate="2020-5-10", Surname="df"},
        new StudentApplications(){ ApplicationId=7, ApplicationFor="gg", Firstname="d", SubmissionDate="2020-5-10", Surname="Zdfgou"},
        new StudentApplications(){ ApplicationId=8, ApplicationFor="hh", Firstname="e", SubmissionDate="2020-5-10", Surname="dfg"},
        new StudentApplications(){ ApplicationId=9, ApplicationFor="ii", Firstname="f", SubmissionDate="2020-5-10", Surname="dfg"},
        new StudentApplications(){ ApplicationId=10, ApplicationFor="jj", Firstname="g", SubmissionDate="2020-5-10", Surname="vbc"},
        new StudentApplications(){ ApplicationId=11, ApplicationFor="kk", Firstname="h", SubmissionDate="2020-5-10", Surname="hj"},
        new StudentApplications(){ ApplicationId=12, ApplicationFor="ll", Firstname="i", SubmissionDate="2020-5-10", Surname="hjk"},
        new StudentApplications(){ ApplicationId=13, ApplicationFor="mm", Firstname="j", SubmissionDate="2020-5-10", Surname="tyu"},
    };
    return Json(new {Data = model});
}

Startup.cs:

Startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();                
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

结果:

这篇关于ASP.NET Core 3.1 中的数据表服务器端处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆