数据表未显示响应 [英] Datatables not displaying the response

查看:77
本文介绍了数据表未显示响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AJAX数据表从api获取数据,数据已成功检索并显示在网络"标签中,但未在数据表中呈现

I am trying to get data from an api using AJAX Datatables, the data is retrieved successfully and is being displayed in the network tab but it is not rendered in the DataTable

这是我的AJAX通话

"ajax": {
            "url": "https://api.tidex.com/api/3/info",
            "type":'POST',
            "dataSrc": "pairs",
    },
    columns: [
         { data: 'pairs' },
         { data: 'server_time' },
         { data: 'server_time' },
         { data: 'server_time' },
         { data: 'server_time' },
         { data: 'server_time' },
    ],
    columnDefs: [
       {
           "render": function (data, type, row) {
                return '<a href="">'+data+'</a>';

           }, "targets": 6
       },
    ]

我尝试以常规方式渲染它,但失败了,然后我尝试使用columnDefs渲染它,但也失败了.

I tried rendering it in the normal way but I failed, then I tried rendering it using columnDefs but that also failed.

推荐答案

API响应是对象集合或字典,无法对DataTables进行解析.这是有道理的,因为您希望表数据按索引而不是诸如"doge_btc"之类的名称排序.

The API response is an object collection or dictionary, which is not parseable for DataTables. It makes sense since you would expect table data to be ordered by indexes, not by names such as "doge_btc".

因此,您必须将响应转换为对象数组.幸运的是,这很容易,您可以在 dataSrc 回调中完成此操作:

So you must transform the response to an array of objects. Fortunately this is rather easy, you can do that in the dataSrc callback :

var table = $('#example').DataTable({
   ajax: {
     url: "https://api.tidex.com/api/3/info",
     dataSrc: function(d) {
      var data = [];
      for (var item in d.pairs) {
         data.push(d.pairs[item])
      }
      return data
     }
   },
   columns: [
    { data: "decimal_places", title: "decimal_places" },
    { data: "min_price", title: "min_price" },
    { data: "max_price", title: "max_price" },
    { data: "min_amount", title: "min_amount" },
    { data: "max_amount", title: "max_amount" },
    { data: "min_total", title: "min_total" },
    { data: "hidden", title: "hidden" },
    { data: "fee", title: "fee" }
   ]
})  

就这样-> http://jsfiddle.net/wnoemmte/

Thats it -> http://jsfiddle.net/wnoemmte/

这篇关于数据表未显示响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆