我有一段代碼,我正在與用戶可以選擇下載餅圖的「切片」或整個事情。jQuery/Javascript布爾
當他們點擊切片時,我發送一個名爲source
的變量,告訴我它來自哪個切片。
現在我正在處理「全部下載」按鈕。源代碼簡單地爲all
。
這是我的循環看起來如果用戶選擇slice
,如:
// Loop over our data
$.each(seed.dataset,function(key, value){
if(this[col] == slice){
table += '<tr>';
table += '<td>'+this.escalationID+'</td>';
table += '<td>'+this.escReasonID+'</td>';
table += '<td>'+this.reasonText+'</td>';
table += '<td>'+this.escCreatedBy+'</td>';
table += '<td>'+this.escCreatorFirst+'</td>';
table += '<td>'+this.escCreatorLast+'</td>';
table += '</tr>';
}
});
循環內的IF
語句是什麼控制了包括表變量中的數據。我的問題是,如何選擇何時執行此IF語句或忽略它(允許將所有數據添加到表var)?
顯而易見的是將它包裝在另一個IF語句中,但我覺得可能有更清晰的方法,而不必複製所有的表數據。
最終結果將是一個bool
說if source=All
包括所有行else if source=slice
執行IF語句,只包括我們正在尋找的數據。
UPDATE
基本上我正在尋找一個更清潔的方式來做到這一點:
// Loop over our data
$.each(seed.dataset,function(key, value){
if(source == 'slice'){
if(this[col] == slice){
table += '<tr>';
table += '<td>'+this.escalationID+'</td>';
table += '<td>'+this.escReasonID+'</td>';
table += '<td>'+this.reasonText+'</td>';
table += '<td>'+this.escCreatedBy+'</td>';
table += '<td>'+this.escCreatorFirst+'</td>';
table += '<td>'+this.escCreatorLast+'</td>';
table += '</tr>';
}
// We are downloading all so ignore the constraint
}else{
table += '<tr>';
table += '<td>'+this.escalationID+'</td>';
table += '<td>'+this.escReasonID+'</td>';
table += '<td>'+this.reasonText+'</td>';
table += '<td>'+this.escCreatedBy+'</td>';
table += '<td>'+this.escCreatorFirst+'</td>';
table += '<td>'+this.escCreatorLast+'</td>';
table += '</tr>';
}
});
我現在正在添加它來測試它,但從我可以告訴,如果'ignoreAll'爲false,沒有數據將被包含? – SBB
如果'ignoreAll'爲false,它會檢查'this [col] == slice'條件,因爲它處於OR模式而不是AND模式。如果第二個條件是真的,那麼它會添加行到表。 –
Gotcha,我有一個錯字;很棒! – SBB