【问题标题】:Server-side datatable processing but data not showing服务器端数据表处理但数据不显示
【发布时间】:2016-08-31 21:51:52
【问题描述】:

目前正在尝试获取使用 C# 和 MVC 的服务器端数据表。 我已经到了使用 javascript 中的 ajax 调用从服务返回数据的地步,但数据表仍在处理中......

而且返回的数据看起来也不错:

Javascript:

var accountTable = $("#accountTable").DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        url: '/Entity/AjaxGetJsonData?entityId=' + $("#AdviserId").val(),
        type: "GET",
        columns: [
            { "data": "Date", "orderable": true },
            { "data": "OtherEntity", "orderable": false },
            { "data": "Description", "orderable": false },
            {
                "sDefaultContent": "",
                "sClass": "dtAlignRight",
                "mRender": function (data, type, row) {
                    if (row.Amount.charAt(1) == '-') {
                        var negativeAmount = row.Amount.slice(2);
                        return "-$" + negativeAmount;
                    }
                    else {
                        return row.Amount;
                    }
                }
            }
        ],
        success: function (msg) {
            //do something here
        }
    }
});

C# 返回数据:

[Authorize]
    public ActionResult AjaxGetJsonData(int entityId, int draw)
    {
        List<TransactionView> transactions = CommissionService.GetDefaultTransactions(entityId);
        List<DataItem> items = TransformToDataItem(transactions);
        string search = Request.QueryString["search[value]"];
        int sortColumn = -1;
        //string sortDirection = "asc";
        if (Request.QueryString["order[0][column]"] != null)
        {
            sortColumn = int.Parse(Request.QueryString["order[0][column]"]);
        }
        DataTableData table = new DataTableData();
        table.draw = draw;
        table.recordsTotal = items.Count;
        table.data = items;
        table.recordsFiltered = items.Count;

        return Json(table, JsonRequestBehavior.AllowGet);
    }

其他相关的东西:

public List<DataItem> TransformToDataItem(List<TransactionView> transactions)
{
    List<DataItem> results = new List<DataItem>();
    foreach (TransactionView view in transactions)
    {
        DataItem item = new DataItem();
        item.Date = view.Date;
        item.OtherEntity = view.OtherEntity.Trim();
        item.Description = view.Description.Trim();
        item.Amount = view.Amount;
        results.Add(item);
    }
    return results;
}

public class DataItem
{
    public string Date { get; set; }
    public string OtherEntity {get; set;}
    public string Description { get; set; }
    public string Amount { get; set; }
}

public class DataTableData
{
    public int draw { get; set; }
    public int recordsTotal { get; set; }
    public int recordsFiltered { get; set; }
    public List<DataItem> data { get; set; }
}

我是否缺少某些东西导致数据表始终在处理并且从不使用数据填充表?

【问题讨论】:

  • 我不认为 columns 应该在 ajax 内部
  • 同意愚蠢的错误。谢谢戴夫
  • 您可能会发现使用我的数据表包装器库会有所帮助:aspdatatables.azurewebsites.net

标签: javascript c# jquery datatable


【解决方案1】:

Columns 应该是 DataTable 的子代,而不是 ajax 的子代。此外,success 可能是不必要的。请参阅reference中的示例

var accountTable = $("#accountTable").DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        url: '/Entity/AjaxGetJsonData?entityId=' + $("#AdviserId").val(),
        type: "GET"
    },
    columns: [
         { "data": "Date", "orderable": true },
         { "data": "OtherEntity", "orderable": false },
         { "data": "Description", "orderable": false },
         {
              "sDefaultContent": "",
              "sClass": "dtAlignRight",
              "mRender": function (data, type, row) {
                    if (row.Amount.charAt(1) == '-') {
                         var negativeAmount = row.Amount.slice(2);
                         return "-$" + negativeAmount;
                    }
                    else {
                         return row.Amount;
                    }
              }
         }
    ]

});

【讨论】:

  • 啊。完美的。一个愚蠢的幼稚错误哈哈哈。哇,我永远不会接受那个哈哈哈。这是漫长的一天。非常感谢大卫 :)
猜你喜欢
  • 1970-01-01
  • 2021-10-11
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 2019-12-16
相关资源
最近更新 更多