【问题标题】:populate datatable with json array when some columns are empty当某些列为空时,使用 json 数组填充数据表
【发布时间】:2018-10-06 06:59:21
【问题描述】:

我正在尝试从 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 }];

当前输出截图:

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

要填充的 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”列会有空值,因此无法填充该列。

【问题讨论】:

    标签: javascript jquery json datatables populate


    【解决方案1】:

    您只需要添加一个 ID 列并使用此DataTable 选项orderFixed: [ID_column_number, 'asc'],这是一个示例:

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

    $('#example').dataTable( {
        "orderFixed": [ 0, 'asc' ]
    } );
    

    【讨论】:

    • 我认为 id 列应该在所有行中都有数据?我无法为此添加新列。尝试在“slno”列上排序,但结果仍然相同。
    • @faheemKurikkal 请编辑您的评论有很多错误,所以我看不懂,尝试使用谷歌翻译
    • @BenAyoub 当我的“id”列在所有行中都有值时,您的解决方案正在工作。但我的问题是,就像在我的示例代码中一样,会有一些行中没有“id”值。所以我不能使用'id'来订购桌子。还有其他方法吗?只想像在数组数据集中一样填充表格。
    猜你喜欢
    • 2019-06-01
    • 2014-02-05
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 2011-02-02
    • 2018-12-27
    • 1970-01-01
    相关资源
    最近更新 更多