4

how do I create a html table tilemap from an array? For example when I want the "0"s to be a "td" with a green background and 20px width and height and the "1"s to be a "td" with a brown background with the same size? Could anyone give an example for this array? Additionally, I would like to know how to insert a picture into a specific "td" element? for example a tree picture in the "td" element on the position table[0][0] with a green background, thanks.

var table = [
[0,1,1,1,0],
[0,1,0,0,0],
[0,1,1,0,0],
[0,0,1,0,0],
[1,1,1,0,0]
]
bady
  • 99
  • 8

1 Answers1

3

var arr = [                                        // the array
  [0, 1, 1, 1, 0],
  [0, 1, 0, 0, 0],
  [0, 1, 1, 2, 2],
  [0, 0, 1, 0, 2],
  [1, 1, 1, 0, 0]
];

var table = document.createElement("table");       // create a table element
var tbody = document.createElement("tbody");       // create a tbody element
table.appendChild(tbody);                          // add the tbody element to the table element

arr.forEach(function(sub, j) {                     // for each sub-array sub in the array arr (j is the index of this row)
  var row = document.createElement("tr");          // create a row element
  tbody.appendChild(row);                          // add the row element to the tbody element
  sub.forEach(function(num, i) {                   // for each number num in the array sub (i is the index of this column)
    var cell = document.createElement("td");       // create a cell element
    row.appendChild(cell);                         // add the cell element to this row element
    cell.className = num === 1? "brown": "green";  // if the number num is 1 then set the class to .brown, otherwise set it to .green
    cell.id = "cell-" + i + "-" + j;               // add an id to each cell so you can select it later
  });
});

// use table as you wish
document.body.appendChild(table);                  // in this example append it to the body

// ##################################################################################
// JUST AN EXAMPLE: (THE BELLOW CODE IS JUST TO GIVE YOU SOME INSIGHTS ON YOUR QUEST)
// ##################################################################################

var img = document.createElement("img");           // create the img element
img.src = "http://placehold.it/15";                // set its src

function moveTo(x, y) {                            // get a x and y and move the image to the cell at that pstion
  if(x < 0 || x > 4) return;                       // if x is out of bound, abort
  if(y < 0 || y > 4) return;                       // if y is out of bound, abort

  document.getElementById("cell-" + x + "-" + y).appendChild(img); // move the image img to cell-x-y (x from the left, and y from the top)
}

moveTo(2, 2);                                      // move to the third-third cell at start

document.addEventListener("click", function(e) {   // just an example: when clicking a td element, move the image to that cell
  var target = e.target;
  if(target.tagName === "TD")
    moveTo(target.id.match(/\d+/), target.id.match(/\d+$/));
});
.brown, .green {
  width: 20px;
  height: 20px;
}

.brown {
  background: brown;
}

.green {
  background: green;
}
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • thanks, but could you maybe give me also an example of a function, which goes trough the created table and inserts a picture for example a tree in front of a green background? for example when I have a second array with trees = [ [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0] ] – bady May 02 '17 at 12:28
  • 1
    @bady You can use one array for the whole thing: If the number is `1` then the color will be `brown`, otherwise (any other number), will be `green`. If the number is `2` then add an image to the cell. Check the answer, I've edit it. – ibrahim mahrir May 02 '17 at 15:13
  • thanks man, it's almost what I was looking for, just one little function is missing. I meant for example now that I have the picture in the table of the DOM, i just need a function which can insert and remove pictures from a specific position of the table. So just in case I have created this table with the pictures and afterwards I want to move a picture from one position to another in the table, then I need to remove it from the last position and insert it in the new position. Thanks a lot again. – bady May 02 '17 at 17:44
  • @bady You're welcome! Check the edit. If you're trying to make a game, it would be better to use **canvas**. – ibrahim mahrir May 02 '17 at 18:27