如何将从API中提取的数据添加到HTML DataTable?

问题描述:

我正在尝试使用从API收集的信息来完成DataTable。如何将从API中提取的数据添加到HTML DataTable?

我做了一个“捣鼓”这里更容易地帮助和理解我的意思: http://live.datatables.net/jujofoye/3/edit

我从仅包含ID的HTML表开始。然后我使用该ID在rowCallback调用的API,并在表中使用jQuery $('td:eq(1)', nRow).html(json.Title);

function customFnRowCallback(nRow, aData, iDisplayIndex) { 
    var imdbID = aData[0]; 

    fetch("http://www.omdbapi.com/?i="+imdbID+"&plot=short&r=json&tomatoes=true") 
    .then(function(response) { 
    return response.json(); 
    }) 
    .then(function(json) { 
    $('td:eq(1)', nRow).html(json.Title); 
    }) 
    .catch(function(error) { 
    console.log('There has been a problem with your fetch operation: ' + error.message); 
    }); 
} 

写入获取值但这里的问题是,你不能排序的第二列。即使你可以很好地看到数据。我敢打赌,DataTable并不真的知道有新的数据,你最终会排序空值而不是你添加的值。

同样问题的第二个效果(不会显示在我的小提琴中)是折叠的行(行数在宽度不足时折叠)在展开行时也显示为空。
第三个影响是搜索对取出的数据不起作用。

有没有一种方法可以真正将提取的数据添加到DataTable?不美观的是。

(注:所有我能找到的答复是有关填充一个完整的数据表中有一个AJAX请求我只将数据添加到已经填充的DataTable)

+0

任何特别的原因,为什么不先获取数据再建表由彼得川川的建议或使用AJAX是数据表API的一部分? – Bindrid

+0

@Bindrid是的,rowCallback仅运行可见数据。所以,即使你有一百万条记录,api调用也是有限的,你可以在这里看到api只运行10倍:http://live.datatables.net/jujofoye/11/edit –

+0

@Bindrid另一个原因是我没有知道在构建数据集之前何时将返回api响应,这可能需要很多不可预知的时间 –

我采取了不同的方法。我将DataTable ajax与when/done一起使用,所以在处理所有获取之前它不会刷新表。

我设置了排序,因此列表将按字母顺序排列,即使那些不是列表的顺序。 我也从imdb中获得了独特的一组值。

http://jsbin.com/yojoka/edit?html,js,output

<script type="text/javascript"> 
    // Sample return set from 
    var sampleReturn = { "Title": "Seven Samurai", "Year": "1954", "Rated": "UNRATED", "Released": "19 Nov 1956", "Runtime": "207 min", "Genre": "Adventure, Drama", "Director": "Akira Kurosawa", "Writer": "Akira Kurosawa (screenplay), Shinobu Hashimoto (screenplay), Hideo Oguni (screenplay)", "Actors": "Toshirô Mifune, Takashi Shimura, Keiko Tsushima, Yukiko Shimazaki", "Plot": "A poor village under attack by bandits recruits seven unemployed samurai to help them defend themselves.", "Language": "Japanese", "Country": "Japan", "Awards": "Nominated for 2 Oscars. Another 5 wins & 6 nominations.", "Poster": "https://images-na.ssl-images-amazon.com/images/M/[email protected]_V1_SX300.jpg", "Metascore": "98", "imdbRating": "8.7", "imdbVotes": "238,165", "imdbID": "tt0047478", "Type": "movie", "Response": "True" }; 
    var deferreds = []; 
    var newData = []; 
    $(function ($) { 
     var dt = $("#example").DataTable({ 
      columnDefs:[{targets:[0, -1], width:"150px"}], 
      columns: [ 
       { data: "imdbID" }, 
       { data: "Title" }, 
       { "data": "Year" } 
      ], 
      deferLoading: 0, 
      deferRendering: true, 
      "order": [[ 1, "asc" ]], 
      ajax: function (data, cb, setting) { 
       // get the list of ids created by DataTables from the embedded html 
       var curData = $("#example").DataTable().rows().data(); 
       // if you don't clear, you will end up with double entries 
       $("#example").DataTable().clear(); 
       $.each(curData, function (i, item) { 
        var sr = { i: item.imdbID, plot:"short", r:"json", "tomatoes":true}; 
        deferreds.push(
        $.get("http://www.omdbapi.com/", sr) 
        .then(function (response) { 
         // push the response into the global array 
         newData[newData.length] = response; 
        }) 
       ); 
       }); 

       // now make all of the calls. When done, use the callback to return the data and populate the table 
       $.when.apply(null, deferreds) 
        .done(function() { 
         cb({ data: newData }) 
       }); 
      } 
     }); 
    }); 
</script> 
+0

这有效。谢谢! (然而,我失去了懒加载,我需要为此找到一些东西。) –

+0

如果您希望能够对所有列中的列进行排序,您仍然会放弃延迟加载。如果您愿意放弃搜索和排序功能,则可以一次带入数据页面。不幸的是,你的选择是有限的,因为你正在使用omdbapi的界面,他们的选择相当有限。 – Bindrid

您在数据表是正确的不知道表中有什么要排序。 我会说完成表完成后运行该功能。

$('#example').find('tbody tr').each(customFnRowCallback); 

function customFnRowCallback() 
{ 
    var $this = $(this); 
    var imdbID = $this.find('td:eq(0)').text(); 

    fetch("http://www.omdbapi.com/?i="+imdbID+"&plot=short&r=json&tomatoes=true") 
    .then(function(response) { 
    return response.json(); 
    }) 
    .then(function(json) { 
    $this.find('td:eq(1)').text(json.Title); 
    }) 
    .then(function() { 
    $('#example').DataTable(); 
    }) 
    .catch(function(error) { 
    console.log('There has been a problem with your fetch operation: ' + error.message); 
    }); 
} 
+0

这是行得通的,但有两个问题:1)如果我有一百万行,它会做一百万API调用。你知道如何做到这一点只为可见行? 2)我如何知道所有api调用何时完成以运行数据表? –

+0

那么,根据您的原始代码,您正在为每个ID拨打电话。所以我不知道该怎么说。 通常,我们会进行调用,然后根据返回的对象创建整个表。 您的情况的问题是您动态追加数据,所以.DataTable()不知道要排序的内容。 –