1

I am trying to pass the value of an input a user provides to a bootstrap table. User provides a name and a score. Currently it is being displayed right below the table instead of within it. How would I go about doing this. I've tried to append it using javascript but cant seem to figure it out. Thanks

2
  • Here is my codepen for this project. codepen.io/iamsok/pen/YweEmb Commented Jan 24, 2016 at 21:36
  • 1
    if you want it to be displayed within the table append the result to the table, than to a seperate element Commented Jan 24, 2016 at 21:41

2 Answers 2

3

You have to append data you have in rankings to the table as rows trs using the following line :

 $('table').append('<tr><td>' + rankPosition + '</td><td> ' +rankings[i].name + '</td><td>'
 + rankings[i].score + ' pts</td></tr>');

Note : Add $('table tbody').empty() before the loop to reset table content.

Hope this helps.


$(document).ready(function() {
  var scoreboard;
  var rankings = [];
  var displayScoreboard = function(rankings) {
    var rankPosition = 1;
    var tieScore = 0
    var previousScore = 0;
    $('#rankings').html('')
    $('table tbody').empty();

    for (var i = 0; i < rankings.length; i++) {
      if (i > 0 && rankings[i-1].score !== rankings[i].score) {
        rankPosition = rankPosition + tieScore +1;
      }
      $('table').append('<tr><td>' + rankPosition + '</td><td> ' +rankings[i].name + '</td><td> ' + rankings[i].score + ' pts</td></tr>')
    }
  }
  var sortRankings = function(rankings) {
    rankings.sort(function(a, b) {
      return b.score-a.score;
    });
  };
  $('#add').on('click', function() {
    var playerName = $('#eq-name').val();
    var playerScore = $('#eq-score').val();

    if (playerName.length > 0 && playerScore.length > 0) {
      var playerStats = {name: playerName, score: playerScore};
      rankings.push(playerStats);
      sortRankings(rankings);
      displayScoreboard(rankings);
      $('#eq-name').val('');
      $('#eq-score').val('');
    } else {
      alert("Please enter a name and a score");
    }
  });

  $('#clear').on('click', function() {
    $('#rankings').html('');
    rankings = [];
  });
});
.eq-input {
  display:block;
  text-align:center;
  vertical-align:middle;
  width:100%;
  height:100%;
  padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
    <meta charset="utf-8">
    <title>EverQuoteTest</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/main.scss">
  </head>
  <body>

    <div class="container eq-input">
      <input type="text" id="eq-name" placeholder="Name">
      <input type="number" id="eq-score" placeholder="Score" min=0>
      <br>
      <br>
      <button class="btn btn-success" id="add">Add To Rankings</button>
      <button class="btn btn-danger" id="clear">Clear Rankings</button>
      <a id="add-game" class="btn btn-default ">Add Game</a>
    </div>

    <div class="container">
      <div class="col-md-8 col-md-offset-4">
        <h3>Leaderboard</h3>
      </div>
      <table class="table" border="1">
        <thead>
          <tr>
            <th>Rank</th>
            <th>Name</th>
            <th>Score</th>
          </tr>
        </thead>
      </table>
      <ul class="list-unstyled" id="rankings"></ul>
    </div>

    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <!-- <script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.0.0/lodash.min.js"></script> -->
    <script src="js/main.js"></script>
  </body>
</html>

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

Comments

1

Add this code:

$('.table')
    .append('<tr><td>' + rankPosition + '</td><td>' + rankPosition + '</td><td>' +rankings[i].name + ', ' + rankings[i].score + ' pts</tr>');

Fiddle: http://codepen.io/anon/pen/dGdabG

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.