Hello
I have problem with my Objects. My object have many props and method,
one of this is td (screen reprezentation of the object). I observe
click event to
obj.td where I do many things on this object and change
td (screen reprezentation). My question : how find object where is
attach clicked td ?
Now I setAttribute to td something like obj_id, when td is clicked
first I read this attribute from td, find proper object by this id, do
what to do and finnaly change state of
obj.td. This works fine but
when I have very many obj (like thousand), find object takes many time
(find object is longer then do what to do).
Here is simple example :
// example START
Event.observe(window, 'load', function() {
onLoad();
});
var objTrs = [];
var msg = null;
function onLoad(){
msg = document.createElement("div");
msg.innerHTML = "Click on cell";
$(document.body).appendChild(msg);
var table = document.createElement("table");
var tableBody = document.createElement("tbody");
table.appendChild(tableBody);
$(document.body).appendChild(table);
table.border="1"
for(var i = 0; i < 10; i++){
var myTr = {};
myTr.counter = 0;
myTr.tr = document.createElement("tr");
myTr.objTds = [];
objTrs.push(myTr);
for (var j = 0; j < 10; j++){
var myTd = {};
myTd.counter = 0;
myTd.td = document.createElement("td");
myTd.td.setAttribute('tr_id',i);
myTd.td.setAttribute('td_id',j);
myTd.td.innerHTML = myTr.counter + "_" + myTd.counter;
myTr.objTds.push(myTd);
myTr.tr.appendChild(myTd.td);
Event.observe(myTd.td, 'click', function() {
// find tr object in array
var tr_id = this.getAttribute("tr_id");
var myTr = objTrs[tr_id];
myTr.counter++;
// find proper td in myTr
var td_id = this.getAttribute("td_id");
var myTd = myTr.objTds[td_id];
myTd.counter++;
var msgTxt = "you click " + myTr.counter + " times on this TR
and " + myTd.counter + " on this TD";
myTd.td.innerHTML = myTr.counter + "_" + myTd.counter;
msg.innerHTML = msgTxt;
});
}
tableBody.appendChild(myTr.tr);
}
}
// example STOP
Best regards
maalek