0

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>

2
  • 2
    I made you a snippet. I changed the weird <div></body> to </table> - please add missing multiselect plugin to make a minimal reproducible example Commented Jun 30, 2021 at 11:17
  • 1
    Also you load too many jQuery js files Commented Jun 30, 2021 at 11:19

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.