1

我正在尝试将 JSON 从 URL 加载到 Tabulator 表中。我在这里有一个 jsfiddle https://jsfiddle.net/liostse/2tdtyL6d/

var tableData = [];
$.getJSON('http://88.99.13.199:3000/regionsdata', function(mydata) {
  mydata.forEach(function(val) {
    var regdata = {};
    regdata.measure_code = val.measure_code;
    regdata.totalbent = val.totalbent;
    regdata.totalddent = val.totalddent;
    regdata.totaldd = val.totaldd;
    regdata.pctpliromes = val.pctpliromes;
    tableData.push(regdata);
  });
});
$("#mytable").tabulator({
  data: tableData,
  layout: "fitColumns",
  tooltipsHeader: false,
  columns: 
    [{title: "Measure",field: "measure_code",sorter: "string",frozen: true},
    {title: "totalbent",field: "totalbent"},
    {title: "totalddent",field: "totalddent"},
    {title: "totaldd",field: "totaldd"},
    {title: "pctpliromes",field: "pctpliromes"}],
});

请注意,如果我使用硬编码数据,它可以工作:

var tableData = [
   {measure_code:"Μ1",totalbent:"", totalddent:"", totaldd:"", pctpliromes:""},
   {measure_code:"Μ2",totalbent:"", totalddent:"", totaldd:"", pctpliromes:""},
   {measure_code:"Μ3",totalbent:"", totalddent:"", totaldd:"", pctpliromes:""},
   {measure_code:"Μ19",totalbent:"", totalddent:"", totaldd:"", pctpliromes:""},
   {measure_code:"Μ20",totalbent:"", totalddent:"", totaldd:"", pctpliromes:""},
   {measure_code:"Μ97",totalbent:"", totalddent:"", totaldd:"", pctpliromes:""},
   ];

任何帮助都会很棒!

4

2 回答 2

2

getJSON 是异步的,因此当您调用 .tabulator 时,您的数据还不存在。您必须在 getJSON 成功函数中调用 .tabulator ,例如:

$.getJSON('http://88.99.13.199:3000/regionsdata', function(mydata) {
  mydata.forEach(function(val) {
    ...
  });
  $("#mytable").tabulator({
    ...
  });
});
于 2018-05-13T16:35:43.683 回答
0

无需在 Tabulator 之外解析您的数据,您可以调用setData函数并将您的 url 直接传递给该函数。Tabulator 将负责其余的工作。

$("#mytable").tabulator("setData", " http://88.99.13.199:3000/regionsdata ");

于 2018-09-24T20:50:37.293 回答