使用javascript修改SharePoint 2013列表标题列

时间:2015-03-01 18:04:56

标签: javascript sharepoint sharepoint-2013

我想使用javascript修改视图中的SharePoint 2013列表列标题。我在stackoverflow找到了一个代码,但是我想引用带有显示名称/内部名称的列,而不是列位置。

(function () {


    function preTaskFormRenderer(renderCtx) {
       modifyHeaderData(renderCtx);       
    }


    function modifyHeaderData(renderCtx)
    {
      var viewTitle = renderCtx.viewTitle;
      var linkTitleField = renderCtx.ListSchema.Field[1];
      linkTitleField.DisplayName = viewTitle + ':' + linkTitleField.DisplayName;
    }

    function registerRenderer()
    {
      var ctxForm = {};
      ctxForm.Templates = {};
      ctxForm.OnPreRender = preTaskFormRenderer;
      SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctxForm);
    } 
    ExecuteOrDelayUntilScriptLoaded(registerRenderer, 'clienttemplates.js');

})();

1 个答案:

答案 0 :(得分:0)

示例:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

   SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
     OnPreRender: function(ctx) {

        var field = getFieldByDisplayName(ctx,"Task Name"); 
        if(field)
            field.DisplayName = "Task Name:::"; 
     }
   }); 

});


function getFieldByDisplayName(ctx,name)
{
    var result  = ctx.ListSchema.Field.filter(function(f){return f.DisplayName == name;} ); //find field by display name
    return result.length > 0 ? result[0] : null;
}

结果

enter image description here