dynatable not creating table from remote JSON

2019-09-05 17:35发布

问题:

Here's feature-table.JSON in the same directory as the HTML file:

[
  {
    "band": "Weezer",
    "song": "El Scorcho"
  },
  {
    "band": "Chevelle",
    "song": "Family System"
  }
]

Here's my HTML file:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.11.1.min.js">
<script type="text/javascript" src="jquery.dynatable.js"></script>
<script type="text/javascript">
$.getJSON("feature-table.JSON", function(data) {
    alert(data);
    $("#feature-table").dynatable({
        dataset: {
            records: data
        }
    });
});
</script>
</head>
<body>
<table id="feature-table">
  <thead>
    <th>band</th>
    <th>song</th>
  </thead>
  <tbody>
  </tbody>
</table>
</body>
</html>

The alert pops up with the correct JSON data so i know it's finding it. I've tried: version 2 of jQuery, uploading and using URLs of the js files to make sure the files are in the right place, $.ajax but then $.getJSON after reading Load remote JSON from Dynatable, and various other things. I'm out of ideas. What am I overlooking?

回答1:

I discovered I needed to have my JavaScript inside $(document).ready(function(){...}).



回答2:

Hope this helps.

Did you also include the metadata as per the documentation for the JSON array.

    {
      "records": [
        {
          "someAttribute": "I am record one",
          "someOtherAttribute": "Fetched by AJAX"
        },
        {
          "someAttribute": "I am record two",
          "someOtherAttribute": "Cuz it's awesome"
        },
        {
          "someAttribute": "I am record three",
          "someOtherAttribute": "Yup, still AJAX"
        }
      ],
      "queryRecordCount": 3,
      "totalRecordCount": 3
    }