2014-10-07 60 views
0

我在sharepoint的顯示模板中有以下代碼,我有一個對象數組,並且需要獲得以下結果。如何迭代對象數組並打印出其中一個屬性

Name1 
Name2 
Name3 

因此,我可以使用工具提示替換Sharepoint多人用戶字段的默認呈現。

不過,我不知道如何進行迭代,然後拼接:

截圖: enter image description here

代碼:

// List View - Substring Long String Sample 
// Muawiyah Shannak , @MuShannak 

(function() { 

    // Create object that have the context information about the field that we want to change it's output render 
    var projectTeamContext = {}; 
    projectTeamContext.Templates = {}; 
    projectTeamContext.Templates.Fields = { 
     // Apply the new rendering for Body field on list view 
     "Project_x0020_Team": { "View": ProjectTeamTemplate } 
    }; 

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(projectTeamContext); 

})(); 

// This function provides the rendering logic 
function ProjectTeamTemplate(ctx) { 

    var projectTeamValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; 

    //newBodyvalue should have the list of all display names and it will be rendered as a tooltip automaticlaly 

    return "<span title='" + projectTeamValue + "'>" + newBodyValue + "</span>"; 

} 
+0

它自動由Sharepoint填充。如果你看到截圖,你會看到它的一些對象的數組,我只對Names屬性感興趣。 – 2014-10-07 11:31:18

回答

1

你可以在 「地圖」 的屬性值從projectTeamValue對象數組成一個新的數組,然後將這些數值「連接」在一起(在本例中使用「,」作爲分隔符):

var newBodyValue = projectTeamValue.map(function(person) { 
    return person.value; 
}).join(", "); 

如果您projectTeamValue陣列看起來像:

[{ value: "Name1" }, { value: "Name2" }, { value: "Name3" }] 

然後newBodyValue將是:

"Name1, Name2, Name3" 

旁註:Array.prototype.map() IE 8中不可用和下面,但應該工作在其他瀏覽器中。

+0

謝謝。作品完美 – 2014-10-07 11:52:07

相關問題