无法将行添加到jQuery数据表

时间:2018-12-19 04:56:08

标签: jquery asp.net datatables

一旦在模式弹出窗口上单击“提交”按钮,我试图将行添加到jQuery数据表中。我将数据从弹出窗口转换为JSON字符串,并将其传递给函数以填充数据表,但始终收到有关“请求的未知参数'名称'”的数据表警告。我检查以确保列数和数据点数相同,因为这似乎是导致此错误的常见原因。

这是我尝试过的:

$(document).on("click", "#btnNewEduSubmit", function (event) {debugger
    var isValid = validateEducationSubmit();

    if (isValid) {
        var name = $('#tbName').val();
        var location = $('#tbLocation').val();
        var degree = $('#ddlDegree').find("option:selected").text();
        var degreeID = $('#ddlDegree').val();
        var field = $('#tbField').val();
        var startDate = $('#tbEduStartDate').val();
        var enddate = $('#tbEduEndDate').val();
        var notes = $('#taNotes').val();

        var params = JSON.stringify("{ 'Name': '" + name + "', 'Location': '" + location + "', 'Degree': '" + degree + "', 'DegreeID':'" + degreeID + "', 'Field': '" + field + "', 'StartDate': '" + startDate + "', 'EndDate': '" + enddate + "', 'Notes': '" + notes + "' }");
        populateTable(params, tblEdu);
    }
});

function populateTable(parameters, table) {debugger
    var jResult = JSON.parse(parameters);
    table.row.add(jResult).draw(); <--- Error here, i can see 'Name' parameter
    table.columns([8]).visible(false);
    table.draw(false);
}

var tblEdu = $("#EducationTable").DataTable({
    jQueryUI: true,
    data: [],
    dom: 't',
    order: [[4, "asc"]],
    autoWidth: false,
    "oLanguage": {
        "sEmptyTable": "No Education Info Was Found."
    },
    "columns": [
        {
            "data": "Name"
        }, {
            "data": "Location"
        }, {
            "data": "Degree"
        }, {
            "data": "Field"
        }, {
            "data": "StartDate"
        }, {
            "data": "EndDate"
        }, {
            "data": "Notes"
        }, {
            "render": function (data, type, row) {
                // This doesn't work either when I click on "delete" link!
                var cellContent =
                    "<a href='javascript:' onclick=\"tblEdu.row().remove().draw(false) \">Delete</a>";
                return cellContent;
            }
        }, {
            "data": "DegreeID"
        }
    ],
    "columnDefs": [
        {
            "targets": [8],
            "visible": "false",
            "searchable": "false"
        }
    ]
});

在错误弹出窗口关闭后,将空行添加到数据表中。

1 个答案:

答案 0 :(得分:0)

我猜错误是在您的json stringfy中,然后是解码。如果要将javascript对象转换为json,则键上应该类似于{ name: "John", age: 30, city: "New York" }-> no''。如果您使用的是“ {'Name':'“ + name +”',...}“,则它已经是一个json字符串,您可以将其直接发送到DataTable。