javascript jQuery DataTables 加载客户端对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6589351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:12:14  来源:igfitidea点击:

jQuery DataTables loading a client-side array of objects

javascriptjquerydatatables

提问by kingrichard2005

I'm using the .data() function in jQuery to attach a set of records, returned from the server, to a DOM element on my page. The records are stored as an array of Objects. The code is as follows:

我在 jQuery 中使用 .data() 函数将从服务器返回的一组记录附加到我页面上的 DOM 元素。记录存储为对象数组。代码如下:

    //Attached returned data to an HTML table element
    $('#measTable').data('resultSet', resultSet);

    //Get stored data from HTML table element
var results = $('#measTable').data('resultSet');

//Construct the measurement table
data_table = $('#measTable').dataTable({
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "bProcessing": true,
    "bDeferRender": true,
    "aaData": [ results ],
    "aoColumns": [
                { "mDataProp": "Field1" },
                { "mDataProp": "Field2" },
                { "mDataProp": "Field3" },
                { "mDataProp": "Field4" }
            ]
});

I then fetch the data from the element and proceed to load it into the datatable. But this doens't seem to work and always returns with the error Requested unknown parameter "`Field1" from the data source at row 0. Is it possible to load data into datatables in this manner?

然后我从元素中获取数据并继续将其加载到数据表中。但这似乎不起作用,并且总是从第 0 行的数据源返回错误Requested unknown parameter "`Field1"。是否可以以这种方式将数据加载到数据表中?

UPDATE:

更新:

Here is a sample of the result object array

这是结果对象数组的示例

results = 
    0: Object
       Field1: "2011/04/23"
       Field2: 8
       Field3: "Hello"
       Field4: "World"
       __proto__: Object
    1: Object
       Field1: "2011/03/25"
       Field2: 6
       Field3: "Hello"
       Field4: "Everyone"
       __proto__: Object
...etc.

回答by kingrichard2005

Allan, the developer of DataTables, was able to answer my question in the following post in the DataTables forum. In case the link doesn't work, the issue turned out to be a simple syntax error.

DataTables 的开发者 Allan 能够在 DataTables论坛的以下帖子中回答我的问题。如果链接不起作用,则问题证明是一个简单的语法错误。

Instead of "aaData": [ results ],it needs to be "aaData": results,.

而不是"aaData": [ results ],它需要是"aaData": results,

Thank you for your help Allan.

谢谢你的帮助艾伦。

回答by Nicola Peluchetti

Well, aaData (as it name suggests using hungarian notation) expects an array of arrays, so if you fetch him an array of objects that is why it complains.

嗯,aaData(顾名思义使用匈牙利表示法)需要一个数组数组,所以如果你给他取一个对象数组,这就是它抱怨的原因。

回答by Pablo Ezequiel Leone

Add to your definition the following:

将以下内容添加到您的定义中:

$('#measTable').dataTable({
    ...
    "columns": [
        { "data": "field1" },
        { "data": "field2" },
        { "data": "field3" }
    ]
});

You should match the table columns with the columnsarray.

您应该将表列与数组相匹配。

That's it!

而已!