Datatables.Net - 不填充JSON数据

时间:2016-05-16 11:40:18

标签: c# jquery json datatable

我正在使用Datatables.Net插件到我公司的内部Web应用程序。

Im面临的问题是变量JSON,我可以看到我已经返回了一个JSON响应,并且它根据JSONLint是有效的,但我无法将JSON数组中的信息输入到我的表中尽管遵循了所有数据表中的示例和搜索其帮助站点。

请查看我的代码,让我知道为什么这不会填充表格。

  function populateTable(json, tableId) {
        var id = 'table_' + tableId;
        console.log("Columns In");
        //console.log("table id: " + id + " JSON Response: " + json);
        try {
            var table = $('#' + id).DataTable({
                "data": json.d, 
                "deferRender": true,
                "columns:": [

                    { "data ": "CaseHandlerStaffNumber" },
                    { "data ": "RiskProfileText" },
                    { "data ": "AssignedCheckerStaffNumber" },
                    { "data ": "FeedbackUserStaffNumber" },
                    { "data ": "ComplaintRef" },
                    { "data ": "ChildComplaintRef" },
                    { "data ": "CaseTypeText" },
                    { "data ": "CheckGrade" }
                ]
            });
        } catch (e) {

        }

        try {
            table.columns().every(function () {
                var that = this;
                $('input', this.footer()).on('keyup change', function () {

                    if (that.search() !== this.value) {
                        that
                            .search(this.value)
                            .draw();
                    }
                });
            });

        } catch (e) {

            console.log("Error detected: " + e);
            console.log(e);
        }
    }

- 编辑 -

这是我的JSON数据的一个例子。

{
"data": [{
    "CaseHandlerStaffNumber": "12345678",
    "RiskProfileText": "Low Risk FOS",
    "AssignedCheckerStaffNumber": "77665544",
    "FeedbackUserStaffNumber": null,
    "ComplaintRef": "999999",
    "ChildComplaintRef": "2333",
    "CaseTypeText": "FOS Submission",
    "CheckGrade": "Ungraded"
}]
}

另外,这就是我制作JSON的方式

[System.Web.Services.WebMethod()]
public static object GetDataTables(string checkId, int userId)
{

List<string> listOfColumnns = new List<string>();
listOfColumnns.Add("CaseHandlerStaffNumber");
listOfColumnns.Add("RiskProfileText");
listOfColumnns.Add("AssignedCheckerStaffNumber");
listOfColumnns.Add("FeedbackUserStaffNumber");
listOfColumnns.Add("ComplaintRef");
listOfColumnns.Add("ChildComplaintRef");
listOfColumnns.Add("CaseTypeText");
listOfColumnns.Add("CheckGrade");

int checkStatusId = Convert.ToInt32(checkId.Replace("hidJson_tbl_", ""));

TeamChecks tc = new TeamChecks();

DataTable dtMc = default(DataTable);
dtMc = tc.Get_DatatableFor_GridView(userId, checkStatusId);

DataTable dt = new DataTable();

foreach (void colName_loopVariable in listOfColumnns) {
    colName = colName_loopVariable;
    dt.Columns.Add(string.Format("{0}", colName));
}

foreach (void row_loopVariable in dtMc.Rows) {
    row = row_loopVariable;
    dt.Rows.Add(row("CaseHandlerStaffNumber"), row("RiskProfileText"), row("AssignedCheckerStaffNumber"), row("FeedbackUserStaffNumber"), row("ComplaintRef"), row("ChildComplaintRef"), row("CaseTypeText"), row("CheckGrade"));
}
string jsonResult = null;
jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
jsonResult = jsonResult.Replace("[{", "{\"data\" :[{").Replace("}]", "}]}");


return jsonResult;

}

1 个答案:

答案 0 :(得分:1)

首先,确保在&#34;数据&#34;的末尾删除多余的空间。财产,例如

$scope.mySelection = '';

如果您的数据采用对象数组的形式,则需要使用&#34; title&#34;此处定义的属性https://datatables.net/examples/data_sources/js_array.html例如

{ "data": "caseHandlerStaffNumber" },

如果您正在使用nuget包,它在内部使用Json.NET序列化程序,并选择小写数据集的首字母。因此,您只需在指定数据时使用小写,如下所示:

$('#example').DataTable( {
    data: dataSet,
    columns: [
        { title: "Name" },
        { title: "Position" },
        { title: "Office" },
        { title: "Extn." },
        { title: "Start date" },
        { title: "Salary" }
    ]
} );