我想通过在sharepoint的JS链接中上传一个javascript文件来自定义sharepoint中的列表。所以我写了下面的代码。代码的目的是获取列Nom_x0020_Du_x0020_Projet列的值,该列是我想在sharepoint中自定义的列表的列的内部名称,实际上是一个链接,后面跟着它的描述。因此,代码将该列中项的值拆分为具有两个值的数组:地址和描述。
(function(){
// Create object that have the context information about the field that we want to change it's output render
var nom_projetContext = {};
nom_projetContext.Templates = {};
nom_projetContext.Templates.Fields = {
//Apply the new redenring for nom du projet field on the list view
"Nom_x0020_Du_x0020_Projet" : {"View" : nomProjetModifie}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides( nomProjetModifie);
})();
var adresse;
//this function provides our purpose
function nomProjetModifie(ctx){
var nomComplet = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
//slice nomComplet into to substrings
var arrayOfSubstrings = nomComplet.split(",");
adresse = arrayOfSubstrings[0];
var description = arrayOfSubstrings[1];
return "<span onclcik='redirection()'>"+description+"</span>";
}
function redirection(){
document.location.href = adresse;
}
但无论我做什么,我都有同样的错误“SPClientTemplates未定义”。我该如何解决这个问题?
我找到了如何解决这个问题。首先,我们必须声明如下:
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function(){
//All our code goes here
})
所以最终的代码就像我的问题一样
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function(){
(function(){
// Create object that have the context information about the field that we want to change it's output render
var nom_projetContext = {};
nom_projetContext.Templates = {};
nom_projetContext.Templates.Fields = {
//Apply the new redenring for nom du projet field on the list view
"Nom_x0020_Du_x0020_Projet" : {"View" : nomProjetModifie}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides( nomProjetModifie);
})();
var adresse;
//this function provides our purpose
function nomProjetModifie(ctx){
var nomComplet = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
//slice nomComplet into to substrings
var arrayOfSubstrings = nomComplet.split(",");
adresse = arrayOfSubstrings[0];
var description = arrayOfSubstrings[1];
return "<span onclcik=\'redirection()\'>"+description+"</span>";
}
function redirection(){
document.location.href = adresse;
}
})
它工作,我不久有这个错误信息