【问题标题】:Passing an array with objects in to a datatable将带有对象的数组传递给数据表
【发布时间】:2019-02-27 13:55:51
【问题描述】:

我有一个包含如下对象的数组:

console.log(data);

在控制台中显示:

(40) […]
0: Object { id: 368802, start: "52990", start_id: "ABC", … }
1: Object { id: 329340, start: "52991", start_id: "DEF", … }
2: Object { id: 337521, start: "52992", start_id: "GHI", … }
​...

如何将其传递到数据表中?这似乎不起作用

table_direct = $('#table_direct').DataTable({
    dom: 'Bflrtip',
    ajax: data,
    columns: [
        { "data": "start" },
        { "data": "start_id" }
    ]
});

【问题讨论】:

  • 使用data,而不是ajax,因为您提供的是内容数组。
  • 是的,谢谢!

标签: javascript jquery arrays datatables


【解决方案1】:

您是否尝试过使用数据 (https://datatables.net/manual/data/) 而不是 ajax ?

table_direct = $('#table_direct').DataTable({
    dom: 'Bflrtip',
    data: data,
    columns: [
        { "data": "start" },
        { "data": "start_id" }
    ]
});

我们可以在官方文档中读到:

[
    {
        "name":       "Tiger Nixon",
        "position":   "System Architect",
        "salary":     "$3,120",
        "start_date": "2011/04/25",
        "office":     "Edinburgh",
        "extn":       "5421"
    },
    {
        "name":       "Garrett Winters",
        "position":   "Director",
        "salary":     "$5,300",
        "start_date": "2011/07/25",
        "office":     "Edinburgh",
        "extn":       "8422"
    }
]

$('#example').DataTable( {
    data: data,
    columns: [
        { data: 'name' },
        { data: 'position' },
        { data: 'salary' },
        { data: 'office' }
    ]
} );

【讨论】:

    【解决方案2】:

    这是大错特错...我喜欢你复制文档的方式,但列定义中有一个明显的错误。

    每个列对象的关键是“数据”对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2020-11-08
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 2020-07-12
      相关资源
      最近更新 更多