【问题标题】:How to load a Jquery data-table from a JSON response如何从 JSON 响应加载 Jquery 数据表
【发布时间】:2016-04-11 12:28:15
【问题描述】:

我有一个类似这样的 Json 数据

 var data = '{"aaData":[{"rrno":"RR201600001","name":"Vikram","dob":"2016-04-11","gender":"Male","job_profile":"Buisness Associate","graduation":"B.Tech\/B.E.","total_exp":1,"status":"Accepted"},
{"rrno":"RR201600002","name":"Rahul","dob":"1992-10-13","gender":"Male","job_profile":"Buisness Associate","graduation":"B.Tech\/B.E.","total_exp":3,"status":"Rejected"}]}';

然后我像这样在 DataTable 中加载这些数据

var table = $('#dataTable1').DataTable();
      table.ajax.url(url).load();

我的HTML是这样的

  <table id="dataTable1" class="table  table-bordered table-striped-col">
  <thead>
    <tr>
      <th>Sourcing ID</th>
      <th>Name</th>
      <th>Dob</th>
      <th>Gender</th>
      <th>Job Profile</th>
      <th>Basic / Graduation</th>
      <th>Total Experience</th>
      <th>Final Status</th>
    </tr>
  </thead>
</table>

但我收到一个弹出窗口作为警报提示

DataTables 警告:表 id=dataTable1 - 请求的未知参数 '0' 表示第 0 行

【问题讨论】:

  • 你有一个字符串而不是一个对象。尝试删除开始和结束's。

标签: jquery json datatables


【解决方案1】:

你可以这样试试:

<script type="text/javascript">
$(document).ready(function () {
    $('#dataTable1').dataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            'url':url,
            "dataSrc": "Responses"
        }
    });
});

【讨论】:

  • nops 它不起作用我只是在桌子上看到处理字并且没有任何内容正在加载并且控制台中还有 Uncaught TypeError: Cannot read property 'length' of undefined 错误
  • 我认为你的 json 格式不正确。确保格式正确。
  • 我已经发布了 Json 并检查了它是否有效,并且使用此链接 jsonlint.com 显示正确
  • 仔细检查,在 jsonlint.com 中显示错误。错误:第 1 行解析错误:var data = { "aaDat ^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'跨度>
  • 我正在更新答案中的屏幕截图,它显示了对我来说有效的 Json
【解决方案2】:

你好,你可以这样写

var dataSet = [
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ],
[ "Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000" ]];

$(document).ready(function() {
$('#example').DataTable( {
    data: data ,
    columns: [
        { title: "Name" },
        { title: "Position" },
        { title: "Office" },
        { title: "Extn." },
        { title: "Start date" },
        { title: "Salary" }
    ]
} );

});

更多参考请看这个链接:https://datatables.net/examples/data_sources/js_array.html

【讨论】:

  • 但是如何使用 Ajajx 自动加载数据,我有一个大表,将每个转换为数组会很费力
  • 您可以使用来自 Ajax 的数据通过调用数据表中的操作从表中加载大数据我认为这个链接会有所帮助:datatables.net/examples/data_sources/ajax.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2021-11-09
  • 2015-09-23
  • 2022-12-07
相关资源
最近更新 更多