1

I need to display a list of 100+ football player numbers, names, weight, age, and heights. I would rather not copy/paste the same exact div I have set up and change them manually. I was wondering if there is a way to display each set of data in the html layout I already coded? The only thing I can find on google is Javascript generating it's own table. Here is an example:

Here's the array:

var playerArray = [
    ["#25","Player1", "Weight1", "Height1", "Age1", "Position1"],
    ["#99","Player2", "Weight2", "Height2", "Age2", "Position2"],
    ["#77","Player3", "Weight3", "Height3", "Age3", "Position3"],
    ["#63","Player4", "Weight4", "Height4", "Age4", "Position4"],
    ["#43","Player5", "Weight5", "Height5", "Age5", "Position5"],
    ];

and here is my html where I want the data displayed:

<div class="rosterlist">
            <div class="p_name">[[NUMBER]] <span class="light_text2">/ [[NAME]]</span></div>
            <div class="roster_line_2"><div class="p_pos">[[POSITION]]</div><div class="p_height">[[HEIGHT]]</div><div class="p_grade">[[AGE]]</div><div class="p_weight">[[WEIGHT]]</div></div>
        </div>

If it's not possible, just let me know and I'll get to my copying and pasting:(

1
  • This is easy to do also without a library. Commented Jun 22, 2013 at 6:15

5 Answers 5

2

This is best solved through data binding and templating. I recommend using a template based framework like KnockoutJS. This will give you the ability to specify a single entry that gets repeated for each entry

Link: http://knockoutjs.com/

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

1 Comment

Awesome, never heard of that before. Looks like it should do it!
2

As Stano states, this is straight-forward to do without a library:

var i, k, html = '', line = '';
var template = ''+
  '<div class="rosterlist">' + 
    '<div class="p_name">[[0]] <span class="light_text2">/ [[1]]</span></div>'+
    '<div class="roster_line_2">'+
      '<div class="p_pos">[[5]]</div>'+
      '<div class="p_height">[[3]]</div>'+
      '<div class="p_grade">[[4]]</div>'+
      '<div class="p_weight">[[2]]</div>'+
    '</div>'+
  '</div>'+
'';
var data = [
  ["#25","Player1", "Weight1", "Height1", "Age1", "Position1"],
  ["#99","Player2", "Weight2", "Height2", "Age2", "Position2"],
  ["#77","Player3", "Weight3", "Height3", "Age3", "Position3"],
  ["#63","Player4", "Weight4", "Height4", "Age4", "Position4"],
  ["#43","Player5", "Weight5", "Height5", "Age5", "Position5"],
];

for( i=0; i<data.length; i++ ) {
  line = template;
  for( k=0; k<data[i].length; k++ ) {
    line = line.replace('[['+k+']]', data[i][k]);
  }
  html += line;
}

document.getElementById('output').innerHTML = html;

And in your markup:

<div id="output"></div>

Comments

1

I'm personally a fan of Handlebars

Here's a fiddle

http://jsfiddle.net/BZ2nZ/

the HTML:

<div id="container"></div>

<script type="text/x-handlebars-template" id="rosterlistTemplate">
<div class="rosterlist">
    {{#each this}}
    <div class="p_name">{{number}} <span class="light_text2">/ {{name}}</span>
    </div>
    <div class="roster_line_2">
        <div class="p_pos">{{position}}</div>
        <div class="p_height">{{height}}</div>
        <div class="p_grade">{{age}}</div>
        <div class="p_weight">{{weight}}</div>
    </div>
    {{/each}}
</div>
</script>

The data:

var playerArray = [
    ["#25","Player1", "Weight1", "Height1", "Age1", "Position1"],
    ["#99","Player2", "Weight2", "Height2", "Age2", "Position2"],
    ["#77","Player3", "Weight3", "Height3", "Age3", "Position3"],
    ["#63","Player4", "Weight4", "Height4", "Age4", "Position4"],
    ["#43","Player5", "Weight5", "Height5", "Age5", "Position5"],
    ];  

Convert the data:

var data = _(playerArray).map(function(playerInfo){
    return _.object(['number','name','weight', 'height', 'age', 'position'], playerInfo);
});

Putting the template to use :

var template = Handlebars.compile($('#rosterlistTemplate').html());
$('#container').html(template(data));

I'm using underscore to convert the format of the data you have into something like this

[
    {number: xx, name: xx, weight: xx, height: xx, age: xx, position: xx},
    {number: xx, name: xx, weight: xx, height: xx, age: xx, position: xx},
    {number: xx, name: xx, weight: xx, height: xx, age: xx, position: xx},
]  

If you can get the json in that format then you don't need underscore and can skip the 'convert data' step altogether.

Comments

1

Working jsFiddle Demo

JavaScript

<script>
    var playerArray = [
        ["#25","Player1", "Weight1", "Height1", "Age1", "Position1"],
        ["#99","Player2", "Weight2", "Height2", "Age2", "Position2"],
        ["#77","Player3", "Weight3", "Height3", "Age3", "Position3"],
        ["#63","Player4", "Weight4", "Height4", "Age4", "Position4"],
        ["#43","Player5", "Weight5", "Height5", "Age5", "Position5"],
    ];

    window.onload = function () {
        var template = document.getElementById('template').innerHTML;
        var list = document.getElementById('list');

        for (var i = 0, l = playerArray.length; i < l; i++) {
            var values = {
                'NUMBER': playerArray[i][0],
                'NAME': playerArray[i][1],
                'WEIGHT': playerArray[i][2],
                'HEIGHT': playerArray[i][3],
                'AGE': playerArray[i][4],
                'POSITION': playerArray[i][5],
            };

            var t = template;
            for (var p in values) {
                t = t.replace('[[' + p + ']]', values[p]);
            }

            list.innerHTML += t;
        }

    };
</script>

HTML

<div id="list"></div>

<script id="template" type="text/html">
    <div class="rosterlist">
        <div class="p_name">[[NUMBER]] <span class="light_text2">/ [[NAME]]</span></div>
        <div class="roster_line_2"><div class="p_pos">[[POSITION]]</div><div class="p_height">[[HEIGHT]]</div><div class="p_grade">[[AGE]]</div><div class="p_weight">[[WEIGHT]]</div></div>
    </div>
</script>

Comments

1

You can traverse the array like this:

var str = '';
var len = playerArray.length;
for (var i = 0; i < len; i++) {
    str += '<div class="p_name">' + playerArray[i][0] +
        '/ <span class="light_text2">' + playerArray[i][1] + '</span> \
</div> \
<div class="roster_line_2"> \
    <div class="p_pos">' + playerArray[i][5] + '</div> \
    <div class="p_height">' + playerArray[i][3] + '</div> \
    <div class="p_grade">' + playerArray[i][4] + '</div> \
    <div class="p_weight">' + playerArray[i][2] + '</div> \
</div>';
}
document.getElementById('rosterlist').innerHTML = str;

Full javascript code here: http://jsfiddle.net/VuKmv/ (compatible also with IE6+)

2 Comments

Wait, you've actually started up IE6 to test this? And I thought I was anal-retentive because I still keep a IE7 around.
Tested it in IETester, Mr Listy Rimmsy.

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.