【发布时间】:2016-02-25 09:47:45
【问题描述】:
我不知道 Sharepoint 脚本,我的同事也不知道 JavaScript。 他使用在http://www.wonderlaura.com/Lists/Posts/Post.aspx?ID=228 上找到的脚本来设置名为 Progress1 的列的行,以显示红色或绿色图像;
// JS Link script to show good and bad in colors
// Upload this file in _catalogs/masterpages as a JavaScript Display Template
(function () {
var patientFieldCtx = {};
// Define template variable
patientFieldCtx.Templates = {};
// Define your required fields and functions to call in each case.
// In our case the field is Progress
// Override Function is PatientProgressViewTemplate
patientFieldCtx.Templates.Fields = {
"Progress1": {
"View": PatientProgressViewTemplate
},
};
// Register the template override with SP2013
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(
patientFieldCtx
);
})();
// Override Progress Level field with color based on conditional value
function PatientProgressViewTemplate(ctx) {
var _progressValue = ctx.CurrentItem.Progress1;
if (_progressValue == 'Good') //field value
{
//return "<font style='color:green'>" + _progressValue + "</font>";
return "<div style='text-align: center;' ><img src='../../SiteAssets/Green_Light_Icon.png' height='16' width='16'/>";
}
if (_progressValue == 'Bad')
{
return "<div style='text-align: center;' ><img src='../../SiteAssets/Red_Light_Icon.png' height='16' width='16'/>";
}
}
在同一个视图中,还有一个名为 Progress2 的列,其中需要应用相同的脚本。 所以我建议;
// JS Link script to show good and bad in colors
// Upload this file in _catalogs/masterpages as a JavaScript Display Template
(function () {
var patientFieldCtx = {};
var columns = ["Progress1", "Progress2"];
for (var index = 0; index < columns.length; index++) {
// Define template variable
patientFieldCtx.Templates = {};
// Define your required fields and functions to call in each case.
// In our case the field is Progress
// Override Function is PatientProgressViewTemplate
patientFieldCtx.Templates.Fields = {
columns[index]: {
"View": PatientProgressViewTemplate
},
};
// Register the template override with SP2013
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(patientFieldCtx);
}
})();
// Override Progress Level field with color based on conditional value
function PatientProgressViewTemplate(ctx) {
var _progressValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
if (_progressValue == 'Good') //field value
{
//return "<font style='color:green'>" + _progressValue + "</font>";
return "<div style='text-align: center;' ><img src='../../SiteAssets/Green_Light_Icon.png' height='16' width='16'/>";
}
if (_progressValue == 'Bad')
{
return "<div style='text-align: center;' ><img src='../../SiteAssets/Red_Light_Icon.png' height='16' width='16'/>";
}
}
但是这不起作用。没有任何线索说明原因。 我不得不承认,虽然我有 VS,但我并没有使用 VS 来测试它。我怀疑我必须这样做。
【问题讨论】:
-
我想你说的是 JSLink。 JSLink 出现在列、视图等上,您在哪里注册脚本?无论如何,我认为你不能这样做:
columns[index]但我不确定.. -
是的,这是关于 JSLint,我刚刚在问题中添加了标签。我不知道脚本在哪里注册。
-
我有更新的解决方案,它应该工作如下=)
标签: javascript sharepoint jslint