2

我的方法有些麻烦ajax.reload()- 没有任何反应。我已经用这个 JavaScript 进行了测试:

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": {
            "async": false,
            "url": "arrays.txt",
            "dataSrc": function(json){return json;} // really necessary ?
        }
    } );

    $('#reload').click(function () {
        table.ajax.reload(function(data){
            console.log(data);
        }, false);
    } );
} );

arrays.txt 内容:

[
        [
      "Tiger Nixon",
      "System Architect",
      "Edinburgh",
      "5421",
      "2011/04/25",
      "$320,800"
    ],
    [
      "Garrett Winters",
      "Accountant",
      "Tokyo",
      "8422",
      "2011/07/25",
      "$170,750"
    ]
]

和 html 内容:

<button id="reload">reload</button>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
</table>

如果我将“您的”代码(dataTables.js)更改为

if(callback){
  var api = new _Api( settings );
  callback( api.ajax.json() );
}

代替

if(callback){
  var api = new _Api( settings );
  api.one( 'draw', function(){
    callback( api.ajax.json() );
  });
}

这个对我有用...

实际上,如果您再次单击该按钮,它会起作用,但这不是解决方案。

4

1 回答 1

0

您的代码工作正常。

我已删除async: false,这似乎没有必要,但是代码也可以使用此选项。

需要选项dataSrc,因为您将数组数组作为数据返回,但可以将其简化为dataSrc: "". 从手册

请注意,如果您的 Ajax 源仅返回要显示的数据数组而不是对象,请将此参数设置为空字符串。

请参阅下面的示例进行演示。

$(document).ready(function () {
   // AJAX emulation for demonstration only
   $.mockjax({
      url: 'arrays.txt',
      responseTime: 200,
      response: function(settings){
        this.responseText = [
        [
           "Tiger Nixon",
           "System Architect",
           "Edinburgh",
           "5421",
           new Date(),
           "$320,800"
        ],
        [
          "Garrett Winters",
          "Accountant",
          "Tokyo",
          "8422",
          new Date(),
          "$170,750"
        ]
      ]
     }
   });

   var table = $('#example').DataTable({
       "ajax": {
           url: "arrays.txt",
           dataSrc: ""
       }
   });
  
   $('#reload').click(function () {
       table.ajax.reload(function(data){
           console.log(data);
       }, false);
   } );
});
<!DOCTYPE html>
<html>

<head>
  <meta charset="ISO-8859-1">

  <link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
  <script src="http://vitalets.github.com/x-editable/assets/mockjax/jquery.mockjax.js"></script>
  
</head>

<body>
<button id="reload">reload</button>
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>

  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
  <tbody>
  </tbody>
</table>
</body>
</html>

于 2015-07-14T16:15:50.050 回答