【问题标题】:Adding JSON info to a Datatable by using rows.add使用 rows.add 将 JSON 信息添加到数据表
【发布时间】:2018-04-06 04:53:44
【问题描述】:

问题是我有一个运行良好的数据表,它添加了来自 swapi.co API 的所有 87 个星球大战字符,信息采用如下 JSON 结构:

  {
"count": 87, 
"next": "https://swapi.co/api/people/?page=2", 
"previous": null, 
"results": [
    {
        "name": "Luke Skywalker", 
        "height": "172", 
        "mass": "77", 
        "hair_color": "blond", 
        "skin_color": "fair", 
        "eye_color": "blue", 
        "birth_year": "19BBY", 
        "gender": "male", 
        "homeworld": "https://swapi.co/api/planets/1/", 
        "films": [
            "https://swapi.co/api/films/2/", 
            "https://swapi.co/api/films/6/", 
            "https://swapi.co/api/films/3/", 
            "https://swapi.co/api/films/1/", 
            "https://swapi.co/api/films/7/"
        ], 
        "species": [
            "https://swapi.co/api/species/1/"
        ], 
        "vehicles": [
            "https://swapi.co/api/vehicles/14/", 
            "https://swapi.co/api/vehicles/30/"
        ], 
        "starships": [
            "https://swapi.co/api/starships/12/", 
            "https://swapi.co/api/starships/22/"
        ], 
        "created": "2014-12-09T13:50:51.644000Z", 
        "edited": "2014-12-20T21:17:56.891000Z", 
        "url": "https://swapi.co/api/people/1/"
    }

这很好用,我这样添加:

   var table = $('#example').DataTable( {
    "columns": [
        { "data": "name" },
        { "data": "height" },
        { "data": "hair_color" },
        { "data": "skin_color" },
        { "data": "gender" }
    ]
    } );

  $.ajax({
 url: 'https://swapi.co/api/people/',
 dataType: 'json',
 success: function(json){
   table.rows.add(json.results).draw();
  }
});

这很好用,但现在我想选择一行(从数据表中选择一个字符)并从 JSON 中提取其 url,因此我可以使用来自字符 JSON 的不同信息填充另一个数据表,如下所示:

  $('#example tbody').on('click', 'tr', function () {
    var data = table.row( this ).data();
    alert( 'You clicked on '+data.url+'\'s row' );
     $.ajax({
 url: data.url,
 dataType: 'json',
 success: function(json){
   tableDos.rows.add(json.name).draw();

  }
  });

    } );

除了要将其添加到数据表中的行之外,一切都很好。这个 JSON 是这样的:

    {
"name": "Luke Skywalker", 
"height": "172", 
"mass": "77", 
"hair_color": "blond", 
"skin_color": "fair", 
"eye_color": "blue", 
"birth_year": "19BBY", 
"gender": "male", 
"homeworld": "https://swapi.co/api/planets/1/", 
"films": [
    "https://swapi.co/api/films/2/", 
    "https://swapi.co/api/films/6/", 
    "https://swapi.co/api/films/3/", 
    "https://swapi.co/api/films/1/", 
    "https://swapi.co/api/films/7/"
], 
"species": [
    "https://swapi.co/api/species/1/"
], 
"vehicles": [
    "https://swapi.co/api/vehicles/14/", 
    "https://swapi.co/api/vehicles/30/"
], 
"starships": [
    "https://swapi.co/api/starships/12/", 
    "https://swapi.co/api/starships/22/"
], 
"created": "2014-12-09T13:50:51.644000Z", 
"edited": "2014-12-20T21:17:56.891000Z", 
"url": "https://swapi.co/api/people/1/"
 }

数据表是这样的:

   var tableDos = $('#exampleDos').DataTable( {
    "columns": [
        { "data": "name" },
             { "data": "gender" }


    ]
    } );

它不起作用,数据表显示此错误http://datatables.net/tn/4

【问题讨论】:

  • 我猜这是因为您要求的键/值对不在第二次调用返回的 JSON 中?要检查这一点,只需将"defaultContent": "<i>Not set</i>" 添加到您的列对象 - 如果这不起作用,我们会进一步研究,但我应该想象就是这样:datatables.net/reference/option/columns.defaultContent
  • 我要的密钥在 JSON 中,我会阅读你链接的内容并尝试一下

标签: javascript jquery json datatables


【解决方案1】:

您刚刚在此处添加 name 时遇到了 ajax 问题:tableDos.rows.add(json.name).draw();

我有几分钟的空闲时间,所以我通过JSFiddle 看了更长时间,这似乎有效:

$(document).ready(function() {
  var table = $('#example').DataTable({
    "columns": [{
        "title": "Name",
        "data": "name"
      },
      {
        "title": "Height",
        "data": "height"
      },
      {
        "title": "Hair Colour",
        "data": "hair_color"
      },
      {
        "title": "Skin Colour",
        "data": "skin_color"
      },
      {
        "title": "Gender",
        "data": "gender"
      }
    ]
  });
  var tableDos = $('#exampleDos').DataTable({
    "columns": [{
        "title": "Name",
        "data": "name"
      },
      {
        "title": "Gender",
        "data": "gender"
      }
    ]
  });
  $.ajax({
    url: 'https://swapi.co/api/people/',
    dataType: 'json',
    success: function(json) {
      table.rows.add(json.results).draw();
    }
  });
  $('#example tbody').on('click', 'tr', function() {
    var data = table.row(this).data();
    console.log(data);
    $.ajax({
      url: data.url,
      dataType: 'json',
      success: function(json) {
        tableDos.row.add(json).draw();
      }
    });
  });
});

您正在添加单个对象,因此这将起作用:tableDos.rows.add([json]).draw(); 或者,我使用的方法:tableDos.row.add(json).draw();

希望对您有所帮助。

【讨论】:

  • 太好了,刚刚用我的文档试了一下,效果很好,最后它很简单,哈哈,谢谢老兄,
【解决方案2】:

https://datatables.net/forums/discussion/48819/help-filling-datatable-with-json-from-api

凯文的例子不起作用? http://live.datatables.net/midaqate/1/edit

如果凯文想不通 - 不知道还能告诉你什么。

对于我的服务器端数据库,我使用了常规的 sqli 插入方法。发现效率更高。

【讨论】:

  • 是的,它帮了很多忙前 10 个字符,但现在我的问题是,当有人单击该行时,它将从该人发送 url 以发出 Ajax 请求,例如,当我单击 Luke Skywalker 时,它将返回“/people/1” JSON 但不像“/people/” JSON,所以它不像我的第一个表那样工作。
  • Ajax 中的列可能会导致错误 4。或者尝试将您的 json 切换为对象关联射线。 “在这种情况下,您需要返回一个 json 对象(关联数组),其中列名作为键 - 要么丢失整个“列”:表 init 中的声明。您只提供一个普通数组,但数据表正在寻找键,因为您指定了一个数据属性“:stackoverflow.com/questions/49683076/…
  • 好的。只是重新阅读您的评论可能实际上并没有帮助(尽管可能是您可能会收到错误 #4 的原因)。所以有时你不需要来自 url 的 get_id 是你在说什么?
  • 对不起,伙计,我在手机上,无法表达自己很威尔斯哈哈。我的实际问题是,当有人单击第一个表(具有 87 个字符且 id 为“example”的表)时,它会提示执行此操作的方法:当有人单击一行时,它将从选择的字符并将其传递给 AJAX 调用,以用“电影、车辆和星舰”填充第二个表 这个方法在我的帖子中是带有 $.(#example tbody)...的方法。你可以在我的邮政。问题是它正在拨打电话,但无法填满第二张桌子。
猜你喜欢
  • 2020-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
相关资源
最近更新 更多