简体   繁体   中英

How to fix .click function to be more dynamic in nature

function add(obj) {
    for (var i = 0; i < obj.length; i++) {
        var elements = Object.keys(obj[i]);
        var host = obj[i].host;
        for (var key in obj[i]) {
            if (key === "host")
                continue;
            else {
                $("#table").append('<tr id=' + key + host.replace(/\./g, "") + '><th>' + key + '</th><td>' + host + '</td></tr>');
                var id = key + host.replace(/\./g, "");
                for (var j = 0; j < obj[i][key].length; j++) {
                    var cellId = id + Object.keys(timeList[j]).toString().split(" ").join("-").replace(/:/g,"-");
                    var list = obj[i][key][j];
                    var flag = aggregateTags(list);
                    if (flag == "OKAY")
                    $("#" + id).append('<td><img id ='+cellId+' src="/static/img/dashboard_green.gif" style="padding-top: 20px"></td>');
                    else if (flag == "WARNING")
                    $("#" + id).append('<td><img id ='+cellId+' src ="/static/img/dashboard_yellow.gif" style="padding-top: 20px"></td>');
                    else
                    $("#" + id).append('<td><img id ='+cellId+' src ="/static/img/dashboard_red.gif" style="padding-top: 20px"></td>');
                    $("#"+cellId).click(function(){ console.log(" i clicked on " +this.id);show(list)});
                }
            }
        }
    }
}

This function basically adds rows in a table dynamically. Each row was j cells. The problem with my $("#"+cellId).click function is that on clicking any cell , the results displayed are that of the last cell in that row. This is because , the "list" I have passed as an argument to the show function is always corresponding to the last cell of that row.

Basically : if these are the 4 lists corresponding to every cell for a given row :

list [{"host":"example.com","metric":"PENDING_COUNT","value":{"15-10-2014 19:30_0":1},"tags":"","alert":"OKAY"}]
 list [{"host":"example.com","metric":"PENDING_COUNT","value":{"15-10-2014 19:25_0":1},"tags":"","alert":"OKAY"}]
 list [{"host":"example.com","metric":"PENDING_COUNT","value":{"15-10-2014 19:20_0":1},"tags":"","alert":"OKAY"}]
 list [{"host":"example.com","metric":"PENDING_COUNT","value":{"15-10-2014 19:15_0":1},"tags":"","alert":"OKAY"}] 

The only list getting passed to the show() function [ no matter which cell of that row is clicked ] is the last 'list' .

ie :

list [{"host":"example.com","metric":"PENDING_COUNT","value":{"15-10-2014 19:15_0":1},"tags":"","alert":"OKAY"}] 

Can someone tell me how to fix this problem?

Thanks in advance!

You need to wrap your click handler in a closure and pass in list .

(function(list){
    $("#"+cellId).click(function(){ console.log(" i clicked on " +this.id);show(list)});
})(list);

This makes a locally scoped list to prevent all iterations from using the same list. Without a closure, all of the click handlers are seeing the same list, because they are picking it up from the higher scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM