【问题标题】:Datatable shows empty data in row数据表在行中显示空数据
【发布时间】:2018-09-19 17:36:33
【问题描述】:

我有一个数据表,它有两行。第一行直接来自数据库并呈现在 UI 上。第二行是通过 javascript 中的 addRow 方法添加的。请看下面的代码:

这是 html 源代码中的表格行:

这是访问该行的代码:

 var table = "";

 table = $("#orders").DataTable({
                ajax: {
                    url: "/api/dfddsfs?id="+ id,
                    dataSrc: ""
                },
                columns: [
                    {

                        data: "product.description",
                    },
                    {

                        data: "quantity"
                    },
                    {

                        data: "product.productPrice"
                    },
                    {
                        data: "discount"
                    },
                    {
                        data: "product.isTaxable"
                    },
                    {
                        data: "finalPrice"
                    },
                    {
                        data: "lineItemTotal"
                    },
                    {
                        data: "product.description",
                        render: function (data, type, product) {

                            return "<span class='glyphicon glyphicon-trash js-delete' data-lineitem-id=" + data + "></span>";


                        }
                    }
                ]
            });


 $('#addRow').click(function () {
    if ($("[name='Product.description']").valid()) {
        //Create a row if not null
        var rowHtml = $("#newRow").find("tr")[0].outerHTML;
        //Add row to the table
        $('#orders').DataTable().row.add($(rowHtml)).draw();
        //Set the first column to product description
        $('#orders tr:first-child td:first-child').first().html($("#product").val());
        //Set second column to quantity
        $('#orders tr:first-child td:nth-child(2)').append("2");

        //Set third column to price
        $('#orders tr:first-child td:nth-child(3)').first().html("123");
       //Set fourth column to dicount
        $('#orders tr:first-child td:nth-child(4)').append("10");
        //Set fifth column as a checkbox
        $('#orders tr:first-child td:nth-child(5)').append("<label><input type='checkbox' value=''></label>");
        //clear the product input text
        $('#product').val('');
    }
    else
        alert("Please choose a product");
});




     $('#save').click(function (evt) {

           table.rows().every(function (rowIdx, tableLoop, rowLoop) {
                   var data = this.data();
                   console.log("data is " + JSON.stringify(data));
            });

      });

这是控制台结果:

data is <br/>
data is {"product":{"description":"","productPrice":"","isTaxable":""},"quantity":"","discount":"","finalPrice":"","lineItemTotal":""}
8:352 data is {"delete":"<i class='fa fa-pencil-square' aria-hidden='true'></i> <i class='fa fa-minus-square .js-delete' aria-hidden='true'></i>","id":21,"quantity":12,"discount":12,"product":{"id":501,"upc":null,"description":"Eggs","restockQty":26,"productPrice":40000,"isTaxable":false,"productCost":11000,"image":"","productCode":"01 B 04","packing":20,"productWholesalePrice":20000},"productId":501,"salesOrder":null,"salesOrderId":8,"lineTaxes":9,"isTaxable":true,"lineItemTotal":999,"finalPrice":9999}

如您所见,结果显示给数据对象。一个有价值观,一个没有价值观。

【问题讨论】:

    标签: jquery datatable


    【解决方案1】:

    您在单击保存按钮时再次调用.DataTable()。您必须在按钮单击之外初始化数据表,将其存储在变量中并使用相同的变量来迭代行

    var table = $('#orders').DataTable();// store previously initialised datatable in variable
    
    $('#save').click(function (evt) {
          //var table = $('#orders').DataTable(); -- remove it
           table.rows().every(function (rowIdx, tableLoop, rowLoop) {
                   var data = this.data();
                   console.log("data is " + JSON.stringify(data));
            });
    
      });
    

    【讨论】:

    • 遵循了您的建议,但仍然得到空值。请查看我的编辑。
    • 这应该可以,但需要检查浏览器是否有任何其他错误。您能否在代码 sn-p 或 JSFiddle 上重新创建您的问题,以便我可以查看它
    • 如果您手头没有表实例,var table = $('#orders').DataTable() 是正确的。
    • 请检查我编辑的问题。我可以直接从 db 查看数据,但不能查看我手动添加的数据。我实际上可以在 html 中看到两行,但我无法访问手动添加的行的值。
    • Datatable 接受 json 中的数据并将其呈现在 UI 上,当您迭代行时它在内部使用相同的 json,但它不会在您手动添加的 JSON 中找到数据,这就是 table.rows.every 的原因不为你工作。您可以创建自己的代码来读取表格行并放入 json 或纯文本,而不是使用 dataable API。
    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多