2017-02-14 1 views
2

Ich habe ein Array mit 3 ObjekteWie kann ich Schaltflächen basierend auf der Anzahl der Objekte in meinem Array erstellen?

map.la = [object, object, object]; 

ich eine Taste für jedes dieser Objekte erstellen möchten. Der Grund, warum ich dybamicaly die Knöpfe verursachen muss, ist, weil die Zahl der Gegenstände von Zeit zu Zeit schwankt.

Wie kann ich jedes Objekt durchlaufen und eine Schaltfläche dafür erstellen und dann die QuickInfo der Schaltfläche so einstellen, dass sie der Namenseigenschaft in jedem Objekt entspricht?

die Taste:

this._selectionbutt = new SegButton({ 
    items: [ 

     //each of the below items (this.selection) should be added here to items array. because we have 3 buttons there 
     //should be three unique items here. the items should have the same name as the variable we instantiate the 
     //button with. 

     this._oSelection 
    ] 
}); 

this.selection = new SegButtItems({ 
    icon: "map", 
    tooltip: //this should be the name property of each item 
    press: this._handleSelection(); 
}); 

Sobald der Benutzer auf die Schaltfläche sie die gleiche Funktion genommen wird auf eine der Schaltflächen auswählt (this._handleSelection()) und diese Funktion müssen den Namen Eigenschaft überprüfen Zeichenfolge z "map333" und das ist es.

Jede mögliche Führung sehr appreicated werden :)

Danke

Antwort

0

Hier ist ein einfaches Beispiel:

HTML-Code. Erstellen div, um wich wir Schaltflächen hinzufügen wird

<div id="buttons"></div> 

Js Code:

//create objects 

var Object1 = { 
    icon: "map", 
    tooltip: "Press Me", 
    press: 'console.log("Button1 pressed")' 
}; 
var Object2 = { 
    icon: "map", 
    tooltip: "Press Me", 
    press: 'console.log("Button2 pressed")' 
}; 
var Object3 = { 
    icon: "map", 
    tooltip: "Press Me", 
    press: 'console.log("Button3 pressed")' 
}; 

//add objects to array 
var ar = [Object1, Object2, Object3]; 
//add buttons for every object in array 
ar.forEach(function(v) { 
    var button= document.createElement('button'); 
    button.type= 'button'; 
    button.appendChild(document.createTextNode(v.tooltip)); 
    button.setAttribute('onClick',v.press); 
    document.getElementById("buttons").appendChild(button); 
}); 
+0

Vielen Dank für Ihre Antwort, aber es müssen alle Javascript sein und es muss die „this._selectionbutt“ oben beschrieben verwenden. –

Verwandte Themen