我正在循环数组并向datatable添加行。我需要为循环中计算的行中的每个单元格添加一个类。我该如何添加课程?
table.clear()
.draw();
for(i=0;i<dbData.length;i++){
// Voice total, CSSR, DCR, MOSMS, MOSuccess, MTSMS, MTSuccess, GPRS attach, Attach success, Attach net success, PDP, PDP Success, PDP network success
// 1 2 3 4 5 6 7 8 9 10 11 12 13
colvcssr = "";
if (cdbl_if_not_null ( dbData[i]['MAX(VOICECSSR)'] ) >= 99 && cdbl_if_not_null ( dbData[i]['MAX(VOICE)'] ) > 0) {
colvcssr = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(VOICECSSR)'] ) < 99 && cdbl_if_not_null ( dbData[i]['MAX(VOICE)'] ) > 0) {
colvcssr = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(VOICECSSR)'] ) < 98 && cdbl_if_not_null ( dbData[i]['MAX(VOICE)'] ) > 0) {
colvcssr = "bg-red";
}
colvdcr = '';
if (cdbl_if_not_null ( dbData[i]['MAX(VOICEDCR)'] ) <= 075 && cdbl_if_not_null ( dbData[i]['MAX(VOICE)'] ) > 0) {
colvdcr = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(VOICEDCR)'] ) > 075 && cdbl_if_not_null ( dbData[i]['MAX(VOICE)'] ) > 0) {
colvdcr = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(VOICEDCR)'] ) > 1 && cdbl_if_not_null ( dbData[i]['MAX(VOICE)'] ) > 0) {
colvdcr = "bg-red";
}
colsmsmo = '';
if (cdbl_if_not_null ( dbData[i]['MAX(MOSMSSUCCESS)'] ) >= 99 && cdbl_if_not_null ( dbData[i]['MAX(MOSMSSUCCESS)'] ) > 0) {
colsmsmo = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(MOSMSSUCCESS)'] ) < 99 && cdbl_if_not_null ( dbData[i]['MAX(MOSMSSUCCESS)'] ) > 0) {
colsmsmo = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(MOSMSSUCCESS)'] ) < 98 && cdbl_if_not_null ( dbData[i]['MAX(MOSMSSUCCESS)'] ) > 0) {
colsmsmo = "bg-red";
}
colsmsmt = '';
if (cdbl_if_not_null ( dbData[i]['MAX(MTSMSSUCCESS)'] ) >= 99 && cdbl_if_not_null ( dbData[i]['MAX(MTSMSSUCCESS)'] ) > 0) {
colsmsmt = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(MTSMSSUCCESS)'] ) < 99 && cdbl_if_not_null ( dbData[i]['MAX(MTSMSSUCCESS)'] ) > 0) {
colsmsmt = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(MTSMSSUCCESS)'] ) < 98 && cdbl_if_not_null ( dbData[i]['MAX(MTSMSSUCCESS)'] ) > 0) {
colsmsmt = "bg-red";
}
coldattach = '';
if (cdbl_if_not_null ( dbData[i]['MAX(ATTACHSUCCESS)'] ) >= 95 && cdbl_if_not_null ( dbData[i]['MAX(ATTACH)'] ) > 0) {
coldattach = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(ATTACHSUCCESS)'] ) < 95 && cdbl_if_not_null ( dbData[i]['MAX(ATTACH)'] ) > 0) {
coldattach = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(ATTACHSUCCESS)'] ) < 90 && cdbl_if_not_null ( dbData[i]['MAX(ATTACH)'] ) > 0) {
coldattach = "bg-red";
}
coldpdp = '';
if (cdbl_if_not_null ( dbData[i]['MAX(PDPSUCCESS)'] ) >= 95 && cdbl_if_not_null ( dbData[i]['MAX(PDP)'] ) > 0) {
coldpdp = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(PDPSUCCESS)'] ) < 95 && cdbl_if_not_null ( dbData[i]['MAX(PDP)'] ) > 0) {
coldpdp = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(PDPSUCCESS)'] ) < 90 && cdbl_if_not_null ( dbData[i]['MAX(PDP)'] ) > 0) {
coldpdp = "bg-red";
}
coldpdpexcue = '';
if (cdbl_if_not_null ( dbData[i]['MAX(PDPNETWORKSUCCESS)'] ) >= 95 && cdbl_if_not_null ( dbData[i]['MAX(PDPNETWORKSUCCESS)'] ) > 0) {
coldpdpexcue = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(PDPNETWORKSUCCESS)'] ) < 95 && cdbl_if_not_null ( dbData[i]['MAX(PDPNETWORKSUCCESS)'] ) > 0) {
coldpdpexcue = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(PDPNETWORKSUCCESS)'] ) < 90 && cdbl_if_not_null ( dbData[i]['MAX(PDPNETWORKSUCCESS)'] ) > 0) {
coldpdpexcue = "bg-red";
}
coldattachexcue = '';
if (cdbl_if_not_null ( dbData[i]['MAX(ATTACHNETWORKSUCCESS)'] ) >= 95 && cdbl_if_not_null ( dbData[i]['MAX(ATTACHNETWORKSUCCESS)'] ) > 0) {
coldattachexcue = "bg-green";
}
if (cdbl_if_not_null ( dbData[i]['MAX(ATTACHNETWORKSUCCESS)'] ) < 95 && cdbl_if_not_null ( dbData[i]['MAX(ATTACHNETWORKSUCCESS)'] ) > 0) {
coldattachexcue = "bg-amber";
}
if (cdbl_if_not_null ( dbData[i]['MAX(ATTACHNETWORKSUCCESS)'] ) < 90 && cdbl_if_not_null ( dbData[i]['MAX(ATTACHNETWORKSUCCESS)'] ) > 0) {
coldattachexcue = "bg-red";
}
var span = '';
if( dbData[i] ['SWITCH'].substr ( 0, 3 ) == "BSR" ){
span = '<span><a href="javascript:vssalert()"><img src=../common/images/noun_5467.svg border=0 alt="Show Sites connected to this switch" style="width:15px;height:15px;opacity:0.50"></a></span>';
}else{
span = '<span><a href="sites.asp?switch='+dbData[i]['SWITCH']+'"><img src="../common/images/noun_5467.svg" border=0 alt="Show Sites connected to this switch" style="width:15px;height:15px;"></a> </span>';
}
table.row.add([
dbData[i]['SWITCH'],
'<a href="javascript:showgraphdiv(\'voicevolume'+dbData[i]['SWITCH']+'>\',\''+dbData[i]['SWITCH']+'\',\'VoiceVol\')">'+dbData[i]['MAX(VOICE)']+'</a>',
'<a href="javascript:showgraphdiv(\'voicesuccess'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'VoicePer\')">'+formatnumber_if_not_null(dbData[i]['MAX(VOICECSSR)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'voicedcr'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'VoicePer\')">'+formatnumber_if_not_null(dbData[i]['MAX(VOICEDCR)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'moattemptsms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSVol\')">'+dbData[i]['MAX(MOSMS)']+'</a>',
'<a href="javascript:showgraphdiv(\'mosuccesssms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(MOSMSSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'mtattemptsms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSVol\')">'+dbData[i]['MAX(MTSMS)']+'</a>',
'<a href="javascript:showgraphdiv(\'mtsuccesssms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(MTSMSSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datavolumeattach'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerVol\')">'+dbData[i]['MAX(ATTACH)']+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccessattach'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(ATTACHSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccessattach'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(ATTACHNETWORKSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datavolumepdp'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerVol\')">'+dbData[i]['MAX(PDP)']+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccesspdp'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(PDPSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccesspdp'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(PDPNETWORKSUCCESS)'],3)+'</a>'
]).draw();
}//end for
答案 0 :(得分:1)
您可以在循环中尝试这样的事情:
var rowNode = table.row.add([
dbData[i]['SWITCH'],
'<a href="javascript:showgraphdiv(\'voicevolume'+dbData[i]['SWITCH']+'>\',\''+dbData[i]['SWITCH']+'\',\'VoiceVol\')">'+dbData[i]['MAX(VOICE)']+'</a>',
'<a href="javascript:showgraphdiv(\'voicesuccess'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'VoicePer\')">'+formatnumber_if_not_null(dbData[i]['MAX(VOICECSSR)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'voicedcr'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'VoicePer\')">'+formatnumber_if_not_null(dbData[i]['MAX(VOICEDCR)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'moattemptsms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSVol\')">'+dbData[i]['MAX(MOSMS)']+'</a>',
'<a href="javascript:showgraphdiv(\'mosuccesssms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(MOSMSSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'mtattemptsms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSVol\')">'+dbData[i]['MAX(MTSMS)']+'</a>',
'<a href="javascript:showgraphdiv(\'mtsuccesssms'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'SMSPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(MTSMSSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datavolumeattach'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerVol\')">'+dbData[i]['MAX(ATTACH)']+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccessattach'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(ATTACHSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccessattach'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(ATTACHNETWORKSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datavolumepdp'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerVol\')">'+dbData[i]['MAX(PDP)']+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccesspdp'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(PDPSUCCESS)'],3)+'</a>',
'<a href="javascript:showgraphdiv(\'datasuccesspdp'+dbData[i]['SWITCH']+'\',\''+dbData[i]['SWITCH']+'\',\'BearerPer\')">'+formatnumber_if_not_null(dbData[i]['MAX(PDPNETWORKSUCCESS)'],3)+'</a>'
]).draw().node();
// if you need you can add some class to the row
//$(rowNode).addClass('rowClass_' + i);
// add class for cells
$(rowNode).find('td').eq(0).addClass('myClass_0');
$(rowNode).find('td').eq(1).addClass('myClass_1');
// and so on...
// or in loop:
// for(var j = 0; j < 13; j++) {$(rowNode).find('td').eq(j).addClass('myClass_' + j);}