2015-02-08 83 views
0

新增Angular和javascript,並且一直困擾着我的大腦2天。我有一個HTML表格,我需要顯示和隱藏複選框。每行有5個複選框。什麼將可見取決於FieldName的數組中的FieldName和索引。我已經評論了下面的代碼,我希望它能解釋我所嘗試過的。根據數組中的值顯示/隱藏複選框

HTML頁面上的角代碼看起來是這樣的,具有範圍從0到4的數。 - 納克放映=「ShowCheckbox(info.FieldName,0)」

JavaScript的樣子 - -

$scope.ShowCheckbox = function (FieldName, index) {  
// FieldName is "MandatedMaterials", "AgencyAgmt", etc. 

    var MandatedMaterials = [true, true, false, false, false]; 
    var AgencyAgmt = [true, true, false, false, false]; 

    return FieldName[index]; 
    // This is what I really really want to work. 
    // Only MandatedMaterials[2] and AgencyAgmt[3] return false 

    // My experiments to narrow down the problem. 
    //if (FieldName == "MandatedMaterials")   
    // works, so FieldName is coming from index.cshtml 

    //if (index == 3) 
    // works, same thing as above 

    //if (MandatedMaterials[0] == true)    
    // works, in that it showed all checkboxes 

    //if (AgencyAgmt[0] == true)      
    // works, same as above 

    //if (FieldName[0] == true)      
    // NOPE -- should show all checkboxes, but instead none 

    //if (FieldName[0])        
    // works, in that it shows all checkboxes 

// For testing purposes I had "if" code to return true or false for the 
// commented out statements above. This meant those bits of code would 
// show all the checkboxes. 
} 

如果您可以幫助我獲得顯示和隱藏的相應複選框,我將非常感激。謝謝。

+0

你能展示一段HTML代碼嗎? – 2015-02-08 23:43:13

回答

0

不能在傳遞一個字符串,然後使用是作爲一個數組。 如果您通過ShowCheckBox("AgencyAgmt", 3), 事實上,您返回「AgencyAgmt」的第三個字符,您返回'n' 因此您的檢查失敗。

你應該這樣定義:

var chkBoxValues = {}; 
chkBoxValues.MandatedMaterials = [true, true, false, false, false]; 
chkBoxValues.AgencyAgmt = [true, true, false, false, false]; 
return chkBoxValues[fieldName]; 

這將是工作。

+0

謝謝,@stevenfrog。你是一個巨大的幫助。我不得不花費幾個小時與它搏鬥,以讓我的頭部纏繞它,這裏是javascript代碼的最終結果 - $ scope.ShowCheckbox = function(fieldName,index){ var chkBoxValues = {}; chkBoxValues.MandatedMaterials = [true,true], chkBoxValues.AgencyAgmt = [true,true] chkBoxValues.HOADocs = [,true] return(chkBoxValues [fieldName])[index]; } – 2015-02-09 19:17:40