Displaying JavaScript Array Elements in an Aligned Table Format

Unfortunately, I accidentally deleted my previous post and now need to repost it.

I have a code that is functioning well, thanks to the great help I received here. However, the output of the array players is not neatly aligned with each other in a table format. Setting a fixed width only aligns the numbers but does not arrange them like a table would.

My inquiry is: how can I modify this code (array players output) to achieve a nicely lined up display?

<html>
<head>
<title>Lotto van Terwijn</title>
<body>
<p><img src="../lotto/images/terwijn.png" width="547" height="188"></p>
<style type="text/css">
      body {
        font-family: Verdana, Arial, Helvetica, sans-serif;
        color: #000000;
        background-color: #FFFFFF;
      }
      .name {
        color: #000000;
        font-weight: bold;
        margin-right: 0.5em;
      }
      .picks, .picks * {
        display: inline;
        margin: 0;
        padding: 0;
        list-style-type: none;
      }
      .picks * {
        margin: auto 0.25em;
        color: Yellow;
      }
      .win { color: #ffcc00; font-weight: bold }
      .loss { color: #ff0000; font-weight: bold }
      .drawNum, #Draws H3 {
          margin-bottom: 0;
      }
    </style>
<script type="text/javascript" src="http://jquery.com/src/jquery-latest.js"></script>
<div id="players"></div>
<div id="draws"></div>
<script type="text/javascript">

var players = {
Joop   : ["6","8","16","18","26","28","32","36","38","41"],
Marijke: ["7","10","14","18","24","29","30","34","39","40"],
Michel : ["4","5","11","16","21","27","33","36","42","44"],
Mario  : ["6","9","18","25","32","35","39","40","43","45"],
Diana  : ["2","6","8","17","22","23","33","36","42","45"],
Agnes  : ["3","5","10","15","26","29","32","37","41","44"],
Chris  : ["5","7","8","9","11","12","16","28","30","32"],
Jeannette: ["1","2","4","7","8","11","13","28","30","38"],
Wieger: ["1","2","3","7","10","13","14","22","23","27"],
Anita: ["6","13","15","17","21","26","32","33","43","45"],
Thea: ["1","3","5","7","10","17","19","20","22","38"],
Danny: ["3","7","11","15","22","28","32","37","40","43"],
Cindy: ["2","4","16","18","21","24","33","38","41","44"],
Hanneke: ["1","3","4","12","18","21","25","30","36","40"],
Willem: ["3","9","17","21","27","33","35","39","41","42"]
};

var draws = [ {

when: 'Datum: Zaterdag 08-08-2009',
picks:[2, 13, 15, 18, 21, 41]
},

{
when: 'Datum: Zaterdag 15-08-2009',
picks:[6, 19, 24, 25, 35, 37]
},

{
when: 'Datum: Zaterdag 22-08-2009',
picks:[8, 17, 23, 26, 37, 42]
}
];

var buildPlayers = function(){
var cont = $("#players");
for(player in players){
var html = ["<div>","<span class='name'>"+player+"</span>", "<ol class='picks'>"];
for(var i = 0; i < players[player].length; i++){
html.push("<li class='loss pick_"+players[player][i]+"'>"+players[player][i]+"</li>");
}

html.push("</ol>","</div>");
cont.append(html.join(""));
}
};

var buildDraws = function(){
var cont = $("#draws");
for(var i = 0; i < draws.length; i++){
var html = ["<div class='draw'>","<h4 class='drawNum'>Trekking "+(i+1)+"</h3>","<div class='date'>"+draws[i].when+"</div>","<ol class='picks'>"];

for(var j = 0; j < draws[i].picks.length; j++) {
      var img = '<img src="http://www.lotto.nl/static/images/ballen/lotto/l'
                     + draws[i].picks[j]
                     + '.jpg" alt="'
                     + draws[i].picks[j]
                     + '" />';
      html.push("<li>"+img+"</li>");
      showWin(draws[i].picks[j]);
  }

html.push("</ol>","</div>");
cont.append(html.join(""));
}
};

var showWin = function(winNum){
$(".pick_"+winNum).removeClass("loss").addClass("win");
};

$(function(){
buildPlayers();
buildDraws();
});
</script>


</body>

</html>

Answer №1

 <script type="text/javascript" src="http://jquery.com/src/jquery-latest.js"></script>

<div id="players"></div>
<div id="draws"></div>

<script type="text/javascript">

(function() {

 var players = {
    Sarah   : ["9","12","17","19","28","31","35","39","41","44"],
    Laura: ["4","7","11","15","22","26","29","34","38","40"],
    Mike : ["3","6","10","14","23","27","33","36","45"],
    Mark  : ["8","9","18","25","32","35","37","38","42"],
    Emma  : ["2","5","13","16","21","24","32","36","43","45"],
    Olivia  : ["1","7","8","17","20","23","34","37","42","45"],
    Daniel  : ["5","9","14","18","22","29","33","38","41"],
    Sophie: ["4","6","8","9","11","12","16","28","30","32"],
    Rebecca: ["1","2","3","7","10","13","15","22","23","27"],
    Nathan: ["2","3","5","11","13","14","21","24","33","36"],
    Grace: ["6","13","15","17","20","26","32","35","44"],
    Benjamin: ["1","3","5","7","10","17","19","20","22","38"],
    Hannah: ["3","7","11","15","22","28","32","37","40","43"],
    Lily: ["2","4","16","18","21","24","33","38","41","44"],
    Jackson: ["1","3","4","12","18","21","25","30","36","40"],
    Evelyn: ["3","9","17","21","27","33","35","39","41","42"]
},

draws = [
    {
    when: 'Datum: Zaterdag 08-08-2009',
          picks:[1, 11, 13, 18, 21, 44]
    },

    {
    when: 'Datum: Zaterdag 15-08-2009',
      picks:[4, 17, 25, 26, 35, 37]
    },

    {
    when: 'Datum: Zaterdag 22-08-2009',
      picks:[6, 15, 23, 26, 38, 42]
    }
];

var buildPlayers = function(){
    var cont = $("#players"), table = $('<table></table>');
    for( player in players ){
    if ( players.hasOwnProperty( player ) ) {
        var tr = $('<tr><th>' + player + '</th></tr>').appendTo(table),
            len = players[player].length;

        for ( var i = 0; i < len; i++) {
            var td = $('<td/>').text( players[player][i] )
            .appendTo ( tr );
        }

        cont.append( table );
    }
    }
};

var buildDraws = function(){
    var cont = $("#draws");
    for(var i = 0; i < draws.length; i++){
    var html = ["<div class='draw'>","<h4 class='drawNum'>Trekking "+(i+1)+"</h3>","<div class='date'>"+draws[i].when+"</div>","<ol class='picks'>"];

    for(var j = 0; j < draws[i].picks.length; j++) {
        var img = '<img src="http://www.lotto.nl/static/images/ballen/lotto/l'
        + draws[i].picks[j]
        + '.jpg" alt="'
        + draws[i].picks[j]
        + '" />';
        html.push("<li>"+img+"</li>");
        showWin(draws[i].picks[j]);
    }



    html.push("</ol>","</div>");
    cont.append(html.join(""));
    }
};

var showWin = function(winNum){
    $(".pick_"+winNum).removeClass("loss").addClass("win");
};

$(function(){
    buildPlayers();
    buildDraws();
    });

})();
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Update the CSS styling of a parent div based on the active state of specific child divs

I have a class with 4 distinct columns. div class="mainContent"> <div class="left-Col-1" *ngIf="data-1"> </div> <div class="left-Col-2" *ngIf="!data-1"> ...

The PHP code embedded within the HTML document and triggered by an AJAX request failed to

Here is an example of my ajax call: function insertModal(link) { $.ajax({ url: link, cache: false, dataType: "text", success: function(data) { $("#modalInput").html(data); }, error: function (request, status, error) { ...

Navigate to the middle of the visible area

Is there a way to automatically center a div when it is clicked, so that it scrolls to the middle of the browser viewport? I have seen examples using anchor points but I want to find a different solution. Any ideas on how to accomplish this without using ...

Navigating nested loops within multidimensional arrays

Currently, I am experimenting with nested loops to search through nested arrays in order to find a specific value called "codItem". Below is a test model for the array (as I do not have access to the original fetch request on weekends): let teste = [{ it ...

Build a Search Suggestions feature with Node Package Manager (NPM) and Model-View-Controller (M

Stepping into the exciting world of MVC core and harnessing NPM for JavaScript packages has been a learning curve. However, I've encountered an issue that requires some deliberation on the best course of action for resolution. To provide context, I ha ...

What could be the reason that step 3 of the AngularJS Tutorial is not functioning correctly?

During Step 3 of the AngularJS Tutorial, an additional e2e test is recommended to enhance the example: it('should display the current filter value within an element with id "status"', function() { expect(element('#status').text() ...

The significance of API Input Validation and Steering Clear of Lengthy Conditional Statements

Currently, I am working on ensuring that my API functions correctly even in cases of bad or missing data. At the moment, I have an if statement that checks for any missing inputs. If an input is missing, it returns false, otherwise there is a large else b ...

Utilizing HTML injection to access both the Chrome API and global variables

I am currently developing a new Chrome Extension and am diving into the process for the first time. My extension involves injecting an HTML sidebar into web pages, adding JavaScript functions to the header, and allowing users to interact with buttons on th ...

Introduce a timeout in the ajax request

I'm currently facing an issue with adding a 2-second delay between the loader icon and the success message displaying the data as html. I attempted to use setTimeout in my ajax code with a specified delay number, but it doesn't seem to be workin ...

What could be causing my router UI in angular.js to malfunction?

Having an issue with routing not functioning as intended, here is the relevant code: $urlRouterProvider. otherwise('/list'); $stateProvider. state('home', { abstract: true, views: { 'header': { templateUrl: &apos ...

Performing an AJAX GET request to the API after a set time interval

The API is constantly updating with live values, so I am attempting to fetch the data every second and display it on the webpage. Although I used a GET request call every N seconds using set_interval(), the values only load once and do not update with eac ...

How can I transform area, city, state, and country into latitude and longitude using Google Maps API v3?

Is there a way to retrieve the latitude and longitude for a string that includes area name, city name, state name, and country name using Google Maps API V3? ...

Struggling to get the knockout js remove function to function properly within a nested table structure

I've been encountering issues while trying to eliminate the formulation elements with the 'Delete comp' link. I can't seem to figure out why it's not functioning as expected. Moreover, the 'Add another composition' link o ...

Tips for including shared information across different ionic tabs

Is there a way to include some shared content, like a canvas, in between the content of different tabs in Ionic? I want this common content to be displayed across all tabs, while each tab still shows its own dynamic data below it. I attempted to add the c ...

Utilizing ReactJS to fetch data from Material-UI's <TableRow/> component upon selection - a comprehensive guide

I've integrated Material-UI's <Table/> (http://www.material-ui.com/#/components/table) component with <TableRow/> containing checkboxes in a ReactJS project. While I can successfully select rows by checking the boxes, I am struggling ...

Showing hidden errors in specific browsers via JavaScript

I was struggling to make the code work on certain browsers. The code you see in the resource URL below has been a collection of work-around codes to get it functioning, especially for Android browsers and Windows 8. It might be a bit sketchy as a result. ...

Redux - a method of updating state through actions

Hello, I am currently working on developing a lottery system and I have a question regarding the best approach to modify state using action payloads. Let's consider the initial state: type initialCartState = { productsFromPreviousSession: Product[ ...

Obtain non-numeric parameters from the URL in Angular 2 by subscribing to

How do I handle subscribing to a non-numeric parameter from a URL? Can the local variable inside my lambda function params => {} only be a number? Here's my code: getRecordDetail() { this.sub = this.activatedRoute.params.subscribe( ...

I am unable to determine if I have already selected a List Item

My goal is to have a functionality where clicking on "Download Drivers" will open the list, and clicking again will close it. This should be achieved with onclick events only, no hover effects. Additionally, I want the list to remain open even if I click o ...

Google Analytics in Next.js Missing Page Title Configuration

I recently set up Google Analytics on my Next.js website, but I'm encountering a strange issue where the analytics are not detecting my webpages and showing as (not set). Everything else seems to be functioning properly. I've double-checked that ...