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

Placing the template code underneath the existing code within the Handlebars layout.hbs file

I'm currently working on a project using Express Handlebars. I have a template called foo.hbs that contains some JavaScript code which I need to insert below the script tags in the layout.hbs file: <!DOCTYPE html> <html> <head> ...

Learn how to bypass the problem of self-signed certificates in request-promise

In my node application, I am utilizing the request-promise module to make API calls. You can find more information about it here. import request from 'request-promise'; let options = { method: GET, json: true, ...

Download the ultimate HTML package with a built-in chart.js component directly from your nuxt app

I have a task to create a compact Nuxt application that produces a basic HTML file containing informative sections about the services offered to the clients of my organization. The main purpose is to generate an HTML file that can be easily downloaded and ...

How can I achieve unique spacing between rows in material-ui grid components?

What is the most effective method for creating spacing between specific rows in a grid using material-ui? Consider the following grid with spacing={0}: GridContainer row1 GridItem GridItem row2 GridItem GridItem row3 GridItem GridItem row4 Gr ...

Ensuring all fetch requests are completed successfully before dispatching an action in React-Redux

I'm currently developing a React application and utilizing Redux to manage the state. Here is a snippet of the code: category-arrows.component.jsx: import React, { Component } from 'react'; import { connect } from 'react-redux'; i ...

Displaying Child Component in Parent Component After Click Event on Another Child Component: How to Implement Angular Parent/Children Click Events

After delving into Angular 7 for a few weeks, I find myself faced with the challenge of toggling the visibility of a child component called <app-child-2> within a Parent component named <parent>. This toggle action needs to be triggered by a cl ...

Issue encountered while attempting to save a value in localStorage

I am encountering an issue while trying to save and read the value of a button in the panel. The error message I receive is - Unable to set property 'adl' of undefined or null reference. This is my first time working with localStorage, so I' ...

Is it possible to include multiple eventTypes in a single function call?

I have created a function in my service which looks like this: public refresh(area: string) { this.eventEmitter.emit({ area }); } The area parameter is used to update all child components when triggered by a click event in the parent. // Child Comp ...

ReactJS - Continuous Loop invoking Encapsulated Function

I keep encountering the same issue with an infinite loop in my code, but I can't figure out why. Currently, I am working with reactJS version 16.5.2 The infinite loops tend to occur when you try to use SetState where it is not allowed (such as in th ...

Changing the close button icon in highslide popups

Utilizing highslide together with highcharts, I need to customize the functionality of the close button. Specifically, I want to trigger an additional function when a user clicks on the "X" button. Upon inspecting the "X" button, this is what appears in m ...

Guide to combining an Angular 2 application with a Django application

Currently, I have been working through the Tour of Heroes tutorial. The structure of my Django app can be simplified as shown below: apps/my_app/migrations/ apps/my_app/__init__.py apps/my_app/urls.py apps/my_app/views.py frontend_stuff/js/ javascript ...

JavaScript function to convert a string of characters into a readable time format

Is there a way to input a string in an 'input type="time"' field in my HTML code? <label class="item item-input"> <span class="input-label">Departure Time </span> <input type="time" ng-model="heur ...

What is the process for invoking a server-side Java function from HTML with JavaScript?

Can someone help me figure out the best way to invoke a Java method from HTML (I'm working with HTML5) using JavaScript? I attempted using Applets but that approach didn't yield any results. My goal is to extract the value from a drop-down menu i ...

Having trouble configuring Travis; crashes just before installation begins

Today I attempted to set up Travis on my GitHub project but encountered a few roadblocks (refer to the screenshot). I experimented with different configurations in .travis.yml, such as the one below: language: node_js node_js: - "8.11.2" sudo: false b ...

Using a ternary condition within the ngClick directive

Trying to implement different functionalities based on the type of device. The setting tab is clickable, and in the desktop version, clicking should redirect to a default URL. However, on mobile devices, clicking the same link should open a modal window. ...

Utilizing JQUERY and AJAX for conditional statements

I am currently in the process of creating a basic chat bot. At this point, the bot replies when the user inputs a pre-defined question. However, I am trying to figure out how to program the chatbot to respond with a "sorry, I don't understand" message ...

Stop button from being clicked inside a div when mouse hovers over it

I am facing an issue with a div containing a mouseenter event and a button inside it with a click event. The concept is that when the user hovers over the div triggering the mouseenter event, the div becomes "active", allowing the button to be visible and ...

Encountered an Uncaught ChunkLoadError with Vercel Next.js: Chunk 0 failed to load

Upon removing the node modules and package-lock.json files, I encountered the error mentioned above when attempting to reload any page. The project works fine after being restarted for the first time. However, upon reloading the page again, it displays a b ...

Creating a versatile function for rendering content

I am working on a unique calendar feature that involves using checkboxes for filtering purposes. So far, I have managed to get all the filters functioning correctly by triggering my render event in this manner: //Initiate render event when checkbox is cli ...

How can we ensure that pointer events return the same coordinates as touch events when the viewport is zoomed in?

I attempted to utilize pointer events (such as pointerdown) instead of using a combination of touch events (e.g. touchstart) and mouse events (e.g. mousedown) to determine the input event coordinates. var bodyElement = document.body; bodyElement.addEvent ...