2014-10-07 62 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

相关问题