dieser Code ist eine TabelleOrganisieren Elemente in einer Tabelle mit DOM - JavaScript
var table = document.createElement("table");
if ((lines > 0) && (columns> 0)) {
var tbody = document.createElement("tbody");
var tr, td;
for (var i = 0; i < lines; i++) {
tr = document.createElement("tr");
for (var j = 0; j < columns; j++) {
td = document.createElement("td");
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
document.body.appendChild(table);
wie dieses Bild 1
class Panel
und function
zu putAndShowEquipment
function Panel(id, line, column) {
this.id = id;
this.lines = line;
this.columns = column;
this.equipments = [];
for(var i = 0; i < lines; i++{
this.equipments[i] = [];
}
}
Panel.prototype.putAndShowEquipment = function(line, column, equipment) {
if ((line >= 0) && (line < this.lines)
&& (column >= 0) && (column < this.columns)
&& ((equipament === void 0) || (equipament instanceof Equipament))) {
this.equipaments[line][column] = equipament;
if (equipament) {
equipament.show(this.cell(line, column));
} else {
(new View()).show(this.cell(line, column));
}
}
return this;
}
zu erstellen
und ich möchte dieses Bild gemacht haben 2
und ich kann gemacht, wenn ich
tun//panel with 4 lines and 4 columns to 16 equipments
(new Panel("panel",4,4))
.putAndShowEquipment (0, 0, new Equipament())
.putAndShowEquipment (0, 1, new Equipament())
.putAndShowEquipment (1, 0, new Equipament())
.putAndShowEquipment (1, 1, new Equipament())
.putAndShowEquipment (2, 0, new Equipament())
.putAndShowEquipment (2, 1, new Equipament())
...
;
aber ich versuche, die lines
und columns
mit for
und legte die Equipaments zu schaffen, ich vielleicht Einschränkung denken, wenn vier ist Spalte, Wechsel zu einer anderen Linie, ich habe alles bereit, jetzt ist nur eine Angelegenheit der Berechnungen
//four lines
for (var i = 0; i < lines; i++) {
//four columns
for (var j = 0; j < columns; j++) {
//if column is four
if (j === 4) {
//change line
//this.equipaments.lenght = 16 equipments
panel.putAndShowEquipment(i + 1, j, this.equipaments[i]);
} else {
panel.putAndShowEquipment(i, j, this.equipaments[i]);
}
}
}
Irgendwelche Vorschläge?
Ein bisschen schwer zu verstehen, aber wenn es 4 Spalten gibt, dann wird 'j' nie gleich' 4' innerhalb der Schleife, weil die Schleife gestoppt hat. Ich bin mir nicht sicher, warum Sie die 'if'-Anweisung überhaupt brauchen, aber dann wissen wir nicht, was der Wert von' this' in Ihrem letzten Codeblock ist. Scheint so, als sollte es nur 'panel.putAndShowEquipment (i, j,/* ???? * /)' sein, aber es gibt nicht genug Informationen, um zu wissen, woher das 3. Argument kommen soll. –
Auch in der 'putAndShowEquipment' Methode scheint' (line
@squint, danke, ich sehe den Fehler, ich bearbeite die '(Zeile