当某些列为空时,用json数组填充数据表

时间:2018-10-06 06:59:21

标签: javascript jquery json datatables populate

我正在尝试从js填充数据表。目前,我可以执行此操作,但是我的当前数据在最后几行中具有空白列,因此这些列将首先填充。我尝试填充这些空白列,然后按顺序填充数据。 我的样本数据是:

var dataSet =  { "SlNo": 1, "InvTransactionMasterID": 4, "VType": 'SI', "Form": null, "VNo": 333, "Prefix": null, "Date": null, "Ledger": "Total CashSale", "TotalGross": 23444.00, "BillDiscount": null, "GrandTotal": null, "Product": null, "Qty": null, "Rate": null, "Rent": null, "CashReceived": null, "Area": null, "Employee": null, "Remarks": null, "VehicleName": null, "VehicleNam": null, "Time": null }, 
{ "SlNo": null, "InvTransactionMasterID": null, "VType": null, "Form": null, "VNo": null, "Prefix": null, "Date": null, "Ledger": "Total CreditSale", "TotalGross": 0, "BillDiscount": null, "GrandTotal": null, "Product": null, "Qty": null, "Rate": null, "Rent": null, "CashReceived": null, "Area": null, "Employee": null, "Remarks": null, "VehicleName": null, "VehicleNam": null, "Time": null }];

当前输出屏幕截图:

enter image description here

它显示最后一行的第一行和第一行的第二行。当我为“ SlNo”列提供一个值而不是null时,输出是准确的。

要填充的JS代码:

$('#example').DataTable({                
            data: dataSet,
            columns:
            [
                    {
                        "data": "SlNo",
                        "defaultContent": ""
                    },
                    {
                        "data": "InvTransactionMasterID",
                        "defaultContent": ""
                    },
                    {
                        "data": "VType",
                        "defaultContent": ""
                    },
                    {
                        "data": "Sl",
                        "defaultContent": ""
                    },
                    {
                        "data": "Form",
                        "defaultContent": ""
                    },
                    {
                        "data": "VNo",
                        "defaultContent": ""
                    },
                    {
                        "data": "Prefix",
                        "defaultContent": ""
                    },
                    {
                        "data": "Date",
                        "defaultContent": ""
                    },
                    {
                        "data": "Ledger",
                        "defaultContent": ""
                    },
                    {
                        "data": "TotalGross",
                        "defaultContent": ""
                    },
                    {
                        "data": "BillDiscount",
                        "defaultContent": ""
                    },
                    {
                        "data": "GrandTotal",
                        "defaultContent": ""
                    },
                    {
                        "data": "Product",
                        "defaultContent": ""
                    },
                    {
                        "data": "Qty",
                        "defaultContent": ""
                    },
                    {
                        "data": "Rent",
                        "defaultContent": ""
                    },
                    {
                        "data": "Rate",
                        "defaultContent": ""
                    },
                    {
                        "data": "CashReceived",
                        "defaultContent": ""
                    },
                    {
                        "data": "Area",
                        "defaultContent": ""
                    },
                    {
                        "data": "Employee",
                        "defaultContent": ""
                    },
                    {
                        "data": "Remarks",
                        "defaultContent": ""
                    },
                    {
                        "data": "VehicleName",
                        "defaultContent": ""
                    },
                    {
                        "data": "VehicleNam",
                        "defaultContent": ""
                    },
                    {
                        "data": "Time",
                        "defaultContent": ""
                    }
            ]
        });

如何以正确的顺序填充?在某些行中,“ SlNo”列的值为空,因此无法填充该列。

1 个答案:

答案 0 :(得分:0)

您只需要添加一个ID列并使用此 FROM_HTML_MODE_COMPACT = 63; FROM_HTML_MODE_LEGACY = 0; FROM_HTML_OPTION_USE_CSS_COLORS = 256; FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32; FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16; FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2; FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8; FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4; FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1; 选项DataTable,下面是一个示例:

https://datatables.net/reference/option/orderFixed

orderFixed: [ID_column_number, 'asc']