QuestionsAndAnswers
QuestionsAndAnswers

Reputation: 81

How to display JSON data from API in html table

I am trying to create a web page, which shows the results of a search from json API in a html table, when a button is clicked. I have tried to search advices on that, but most examples online show examples where the json data is in a non-dynamic, separate file, instead of being in a dynamic API database location. What should I add to this code to display the data (that is currently displayed in json form) in a html table? I have tried numerous ways (and deleted them from this code) but nothing works.

Here is my code so far:

$(document).ready(function() {
  $('#option-droup-demo').multiselect({
    enableClickableOptGroups: true
  });
});

$(document).ready(function() {
  $("button").click(function() {
    $("#output").load("https://api.finna.fi/v1/search?lookfor=sibelius&limit=3");
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Axios -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>


<div class="jumbotron text-center">
  <h1>Search from libray API database!</h1>
  <p>Search for books, cs's and other stuff.</p>
</div>
<div class="container">
  <div class="example">
  </div>
</div>
</div>
<form>
  <div class="formClass">
    <div id="output">
      <h2>Click button to load new content inside DIV box</h2>
    </div>
    <button type="button">Click to Load Content</button>
    <table>
      <thead>
        <tr>
          <th>Title</th>
          <th>Name of the book</th>
          <th>Subjects</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>data from api</td>
          <td>data from api</td>
          <td>data from api</td>
        </tr>
      </tbody>
    </table>
  </div>
</form>

Upvotes: 0

Views: 1958

Answers (2)

mplungjan
mplungjan

Reputation: 177692

Here is a start

$(document).ready(function() {
  const $output = $("#output");
  $("button").click(function() {
    $.get("https://api.finna.fi/v1/search?lookfor=sibelius&limit=3", function(data) {
      // console.log(data)
      $output.html(
        data.records.map(item => `<tr><td>${item.title}</td><td>${item.buildings[0].translated}</td><td>...</td></tr>`)
      );
    });
  });
});
tr th {
  text-align: center;
  padding: 10px;
  border:1px solid black;
}

tr td {
  padding: 10px;
  border:1px solid black;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>


<div class="jumbotron text-center">
  <h1>Search library API database!</h1>
  <p>Search for books, cd's and other stuff.</p>
</div>
<div class="container">
  <div class="example">
  </div>
</div>
</div>
<form>
  <div class="formClass">
    <div>
      <h2>Click button to load new content inside DIV box</h2>
    </div>
    <button type="button">Click to Load Content</button>
    <table>
      <thead>
        <tr>
          <th>Title</th>
          <th>Name of the book</th>
          <th>Subjects</th>
        </tr>
      </thead>
      <tbody id="output">
      </tbody>
    </table>
  </div>
</form>

Upvotes: 1

I suggest you use axios - https://www.npmjs.com/package/axios by invoking your function making it async-await

Upvotes: 0

Related Questions