【问题标题】:How to bind Openweather map API data in DataTables?如何在 DataTables 中绑定 Openweather 地图 API 数据?
【发布时间】:2018-01-11 23:01:51
【问题描述】:

我正在使用DataTables 并想在其中显示我的城市数据,但我没有做到。

例如,如果我打电话给this URL,那么我会以 JSON 格式获取城市数据(来自 openweathers map api)。我想简单地在我的数据表上显示这些数据。 请帮忙。

我的代码:

$.ajax({
type: "POST",
url: "http://api.openweathermap.org/data/2.5/group?id=2643741,2644688,2633352,2654675,2988507,2990969,2911298,2925535,2950159,3120501,3128760,5128581,4140963,4930956,5106834,5391959,5368361,5809844,4099974,4440906&appid=de6d52c2ebb7b1398526329875a49c57&units=metric",
dataType: "json",
success: function (result, status, xhr) {

    $('#weatherTable').DataTable({
        data: JSON.stringify(result),
        columns: [
            { data: 'id' },
            { data: 'name' }
        ],
        "pageLength": 3
    });
},
error: function (xhr, status, error) {
    console.log("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
}

});

我只是得到错误:

DataTables 警告:表 id=weatherTable - 请求未知 第 0 行第 0 列的参数“id”。有关此的更多信息 错误,请看http://datatables.net/tn/4

请帮忙?

【问题讨论】:

  • 为什么在尝试将数据插入表之前对其进行字符串化?
  • @Falk 我在没有搅拌的情况下尝试了它,但它仍然没有用。

标签: javascript jquery datatables


【解决方案1】:

使用ajax.dataSrc 选项指定包含表数据的属性。

var table = $('#example').DataTable({
    ajax: {

        url: 'https://api.openweathermap.org/data/2.5/group?id=2643741,2644688,2633352,2654675,2988507,2990969,2911298,2925535,2950159,3120501,3128760,5128581,4140963,4930956,5106834,5391959,5368361,5809844,4099974,4440906&appid=de6d52c2ebb7b1398526329875a49c57&units=metric',
        dataSrc: 'list',
        method: 'POST'
    },
    columns: [
        { data: "id" },
        { data: "name" },
        { data: "main.temp" }
    ]                       
});

有关代码和演示,请参阅this example

【讨论】:

  • 谢谢。还有一件事-如何在数据表中的每个城市的“主”节点下绑定“临时”?
猜你喜欢
  • 1970-01-01
  • 2018-07-30
  • 2016-03-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多