javascript - 在 DataTable 上表示 Json 对象数据对我来说不起作用

标签 javascript jquery json datatables

我正在开发一个java web应用程序(Spring Frame work)。一旦点击某个 URL,就会创建一个 Json 对象。我可以访问这些数据,并且能够对其进行 console.log。但我需要显示这个 Json 对象,它是一个以表格格式包含 1000 多个记录的数组。我不确定的是如何去做。我已经使用本教程( https://datatables.net/examples/ajax/null_data_source.html )做了一些事情,但没有成功。我对这个概念很陌生,我的问题可能真的很垃圾。但我想知道也许我可以从这里获得一些帮助。

    <!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:p="http://primefaces.org/ui"
>    
<h:head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src ="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

    <link rel="stylesheet" href="resource/css/jquery.dataTables.min.css"></link>

</h:head>

<h:body>
    <button id="processData">ProcessData</button>



    <div id="wrapper">
        <div id="header">
            <h2>List of processed data</h2>
        </div>
    </div>



    <table id="dataTable" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>ID1</th>
                <th>ID2</th>
                <th>Number Of Matching Terms</th>
                <th>Matching Terms </th>
                <th>Original Terms in Data Source One</th>
                <th>Original Terms in Data Source Two</th>
                <th>Acceptance Action</th>
                <th>Decline Action</th>

            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>ID1</th>
                <th>ID2</th>
                <th>Number Of Matching Terms</th>
                <th>Matching Terms </th>
                <th>Original Terms in Data Source One</th>
                <th>Original Terms in Data Source Two</th>
                <th>Acceptance Action</th>
                <th>Decline Action</th>
            </tr>
        </tfoot>
    </table>

    <script src="js/mainJs.js"></script>

</h:body>

这是我的js代码:

$(document).ready(function(){

    $("#processData").click(function (){
        $.getJSON('http://localhost:8080/gtlc/PVJson', function(data){
            console.log(data);

        });

        var table = $('#dataTable').DataTable( {
            "ajax": data,
            "columnDefs": [ {
                "targets": -1,
                "data": null,
                "defaultContent": "<button>Click!</button>"
            } ]
        });

    })  

});

</html>

当我查看控制台时,我可以看到以下内容:

enter image description here

这是我得到的错误 enter image description here

这是我的 json 对象的一个​​小样本

"records": [{
    "id1": 1200314690100003429,
    "id2": 1045,
    "matchingTerms": ["adaptor"],
    "orginalTermsTable1": ["AC ADAPTOR FOR JDE", "www.greenlightcorp.net", "AC ADAPTOR FOR JDE", ""],
    "orginalTermsTable2": ["Greenlight Technologies, Inc.", "GRC Adaptor for People Soft", "Customized software for SAP, therefore version not specified"]
}, {
    "id1": 1200314690100003429,
    "id2": 1046,
    "matchingTerms": ["adaptor"],
    "orginalTermsTable1": ["AC ADAPTOR FOR JDE", "www.greenlightcorp.net", "AC ADAPTOR FOR JDE", ""],
    "orginalTermsTable2": ["Greenlight Technologies, Inc.", "GRC Adaptor for Oracle", "Customized software for SAP, therefore version not specified"]
}]

我想知道是否有人可以给我一些关于如何获取 json 对象并将其表示在数据表上的提示

最佳答案

问题是 DataTable 在 ajax 请求后一微秒被初始化。

发送 Ajax 请求时,可能需要 1、2、3 或 10 秒才能返回数据...因此,在 Ajax 响应返回之前,data 是未定义的。

将 DataTable 初始化移到回调函数中应该可以工作。

FIDDLE

$(document).ready(function() {
  var data = {
    "records": [{
      "id1": 1200314690100003429,
      "id2": 1045,
      "matchingTerms": ["adaptor"],
      "orginalTermsTable1": ["AC ADAPTOR FOR JDE", "www.greenlightcorp.net", "AC ADAPTOR FOR JDE", ""],
      "orginalTermsTable2": ["Greenlight Technologies, Inc.", "GRC Adaptor for People Soft", "Customized software for SAP, therefore version not specified"]
    }, {
      "id1": 1200314690100003429,
      "id2": 1046,
      "matchingTerms": ["adaptor"],
      "orginalTermsTable1": ["AC ADAPTOR FOR JDE", "www.greenlightcorp.net", "AC ADAPTOR FOR JDE", ""],
      "orginalTermsTable2": ["Greenlight Technologies, Inc.", "GRC Adaptor for Oracle", "Customized software for SAP, therefore version not specified"]
    }]
  };
  var table = $('#dataTable').DataTable({
    "data": data.records,
    "columns": [{
        "data": "id1"
      },
      {
        "data": "id2"
      },
      {
        "data": "matchingTerms",
        "render": function(data, type, row, meta) {
          return data.length;
        }
      },
      {
        "data": "matchingTerms"
      },
      {
        "data": "orginalTermsTable1"
      },
      {
        "data": "orginalTermsTable2"
      },
      {
        "data": "",
        "render": function(data, type, row, meta) {
          return "<button>Click</button>";
        }
      },
      {
        "data": "",
        "render": function(data, type, row, meta) {
          return "<button>Click</button>";
        }
      }
    ]

  });

});

更好的解决方案:

FIDDLE

$(document).ready(function() {
  var table = $('#dataTable').DataTable({
    "ajax": {
        "url" : "http://localhost:8080/gtlc/PVJson",
      "dataSrc" : "records"
    },
    "columns": [{
        "data": "id1"
      },
      {
        "data": "id2"
      },
      {
        "data": "matchingTerms",
        "render": function(data, type, row, meta) {
          return data.length;
        }
      },
      {
        "data": "matchingTerms"
      },
      {
        "data": "orginalTermsTable1"
      },
      {
        "data": "orginalTermsTable2"
      },
      {
        "data": "",
        "render": function(data, type, row, meta) {
          return "<button>Click</button>";
        }
      },
      {
        "data": "",
        "render": function(data, type, row, meta) {
          return "<button>Click</button>";
        }
      }
    ]

  });


});

关于javascript - 在 DataTable 上表示 Json 对象数据对我来说不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49121589/

相关文章:

javascript - 如何在静态博客网站上使用下拉菜单按年和月一起添加过滤器?

javascript - 使用 jQuery 添加基于浏览器滚动位置的 CSS 类 - 寻找更模块化的方法

javascript - jQuery - 输入文本属性未显示在页面上

javascript - 从客户端填充时,不会触发 Dropdown 的 SelectedIndexChanged 事件

php - 在注册期间规范化/验证用户的 'City' 输入

Javascript - 仅返回对象数组中的唯一值

Javascript - 将数组结果过滤到下拉列表中

json - Angularjs 无法在 Bootstrap 选择器中工作

javascript - 将 JSON 转换为字符串时如何解决此 Javascript 错误?

java - 为什么 Jackson 的 pretty-print 功能不起作用?