简体   繁体   English

在将行添加到DataTable时遇到问题

[英]Having trouble adding row to DataTable

I am building a jquery datatable with JavaScript object. 我用JavaScript对象建立一个jquery数据表。 Table builds fine. 表建立良好。 Then, on a user's imput, I need to add a row. 然后,在用户的输入上,我需要添加一行。 This is where I am having a problem. 这是我遇到的问题。 I have researched this on Datatables.net and on SO. 我已经在Datatables.net和SO上对此进行了研究。 Everything I have read indicates that what I am doing should work, but it doesn't. 我读过的所有内容都表明我正在做的事情应该起作用,但事实并非如此。 No row is added. 没有添加行。 Here is my code when DataTable is initialized. 这是我初始化DataTable时的代码。

$("#partsList").DataTable({
        data: data,
        "order": [[1, "asc"]],
        "scrollY": "800px",
        "scrollCollapse": true,
        "paging": false,
        columns: [
            {sTitle: "InvtId", data: "invtid", "defaultContent": '', 'className': 'invtId'},
            {sTitle: "Line Nbr", data: "linenbr", 'className': 'lineNbr', "defaultContent": 'TEST'},
            {sTitle: "BF", data: "BFval", "defaultContent": ''},
            {sTitle: "BU", data: "BUval", "defaultContent": ''},
            {sTitle: "CnvFact", data: "cnvfact"},
            {sTitle: "Origin", data: "origin"},
            {sTitle: "Line Tot", data: "linetot"},
            {sTitle: "Description", data: "descr"},
            {sTitle: "Quantity Ordered", data: "ordqty"},
            {sTitle: "Quantity Ship", data: "qtyship"},
            {sTitle: "Unit", data: "unitdesc"},
            {sTitle: "Sales Price", data: "slsprice"},
            {sTitle: "Wood Cost", data: "wood"},
            {sTitle: "Treatment Cost", data: "treat"},
            {sTitle: "Adjustments", data: "trtadj"},
            {sTitle: "Misc", data: "misc"},
            {sTitle: "Freight", data: "freight"}
        ]
    });

Then, after a user's input, I am collecting data. 然后,在用户输入之后,我正在收集数据。 This is an example of data being passed to add the new row. 这是传递数据以添加新行的示例。 This isn't the entire object. 这不是整个对象。 Just trying to make sure that this is clear to read. 只是要确保清楚阅读。 All fields are covered in passing. 所有字段均已通过。

Object {invtid: "APGC1DBTR061008", Bf: 880, Bu: 0.88, linenbr: 66536, cnvfact: 0, Adj: "-45"Bf: 880, Freight: "22"}

And this is my code to add the new row. 这是我添加新行的代码。 I am passing the data into the method with the variable name data... 我正在将数据与变量名称数据传递给方法...

var tbl = $("#partsList").DataTable();
        tbl.row.add([
            data.invtid,
            data.linenbr,
            data.Bf,
            data.Bu,
            data.cnvfact,
            data.origin
        ]).draw();

Any reason why this isn't working? 有什么原因不起作用? I have tried and tried and searched everywhere and can't find the problem. 我已经尝试了很多次,并在各处搜索并找不到问题。

您添加了一个数组,但是表中有一个对象。

From the doc on row.add() : row.add()文档中

If a data structure is used (ie array or object) it must be in the same format as the other data in the table (ie if your table uses objects, pass in an object with the same properties here!). 如果使用的是数据结构(即数组或对象),则其格式必须与表中的其他数据相同(即,如果表使用对象,请在此处传递具有相同属性的对象!)。

You'll notice that your structure doesn't match the initial structure in several ways. 您会注意到,您的结构在几种方面与初始结构不匹配。 Specifically, you start with an object and try to add an array, there isn't any Bf or Bu in the original table--you have them as BFval and BUval --and a number of properties are missing. 具体来说,你开始与一个对象,并尝试添加一个数组,没有任何BfBu原始表-你有他们的BFvalBUval --and许多属性的缺失。 (I don't know whether the add() method will fill in missing properties with blanks or not; perhaps you can try it out and see.) (我不知道add()方法是否会用空白填充缺少的属性;也许您可以尝试一下看看。)

So first, change your original table create from this: 因此,首先,从此更改您的原始表创建:

$("#partsList").DataTable({ //etc.

to this: 对此:

var tbl = $("#partsList").DataTable({ //etc.

Then get rid of this: 然后摆脱这个:

var tbl = $("#partsList").DataTable();

and just start with this: 并以此开始:

tbl.row.add({ //etc.

And make sure that your data matches. 并确保您的数据匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM