user2209090
user2209090

Reputation: 154

Append text in table while retaining existing data

I am working on a app that plays music files. Part of the app lists tracks available to be played in a rails table:

<table class="table table-hover">
  <thead>
    <tr>
      <th>#</th>
      <th>Title</th>
      <th>Featured Artist</th>
      <th>Length</th>
      <th>Size</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <% album.tracks.each do |t| %>
  <tbody>
    <tr>
      <td><%= t.track_number.to_s.rjust(2, '0') %></td>
      <td><%= t.title %></td>
      <td><%= t.artists.map{ |a| [a][0].name }.join(", ") %></td>
      <td id="time"><%= "#{((t.length) / 60)}:""#{(t.length % 60).to_s.rjust(2, '0')}" %></td>
      <td><%= "#{t.size} MB" %></td>
      <td>
        <div class="btn-group btn-group-hover pull-left" data-id="<%= t.id%>" data-url="<%= t.track_path%>">
          <a class="btn btn-mini icon-play"></a>
          <a class="btn btn-mini icon-volume-up"></a>
          <a class="btn btn-mini icon-trash"></a>
        </div>
      </td>
      <td><%= rating_for t, "sound" %></td>
      <td></td>
    </tr>
  </tbody>
  <% end %>
</table>

When I click the play button and the track begins to play, I want to append the track time column with the current track time data so it looks like this: 4:34/00:10 Where the time on the left I have in the table already and I want to append the new data after the /.

My javascript code for soundmanager looks like this:

 whileplaying: function() {
   var track_time = $('#time').html();
   var seconds = Math.round(this.position/1000);
   var r = seconds % 60;
   var m = Math.floor(seconds / 60);
   var duration = (m < 10 ? '0' + m : m) + ":" + (r < 10 ? '0' + r : r);
   $('#time').html('<p>' + duration + '</p>');

Problem is that this current code completely replaces the current track time value instead of appending it, and if I use

$('#time').append('<p>' + duration + '</p>');

or

$('#time').text('<p>' + duration + '</p>');

It writes a new value in addition to the existing value which eventually scrolls across the page instead of simply replacing the existing data with the new values in place.

Any ideas on how to do this? I'm very new to development and been stuck on this for about 2 days now.

Thanks.

Upvotes: 0

Views: 259

Answers (2)

Ross
Ross

Reputation: 3070

If you're allowed to tweak the template:

Add a span into your template:

`<span class="duration"></span>`

makes

`<td id="time"><%= "#{((t.length) / 60)}:""#{(t.length % 60).to_s.rjust(2, '0')}" %><span class="duration"></span></td>`

and then is targeted by:

`$('#time').find('.duration').html(newVal)`

Then you can simply target that instead of updating your entire #time text.

As an aside, you're using an ID selector #time in what looks like a repeating table row, so you'll experience problems with duplicate IDs in your page as you move forward. Also investigate $.data() for storing initial values into the element's attributes on rendering which you can then access instead of pulling out of the text content.

Upvotes: 1

LeGrande
LeGrande

Reputation: 230

Every time you call $.append(), it's going to append the new duration on to the end of the element creating the problem you're seeing. When you call $.html() it replaces everything within the element with the duration you're passing to it.

What you might do is create a span within the element and use $.html() to replace the value of the duration.

<td id="time">10:05 <span id="duration"></span></td>

<script>
  whileplaying: function() {
   ...
   $('#duration').html(duration);
  }
</script>

Upvotes: 0

Related Questions