【问题标题】:Webpart javascript does not work for arrayWebpart javascript 不适用于数组
【发布时间】: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


【解决方案1】:

您应该像这样在代码的一部分中设置字段:

patientFieldCtx.Templates.Fields = {
        "Progress1" : {
            "View": PatientProgressViewTemplate
        },
        "Progress2" : {
            "View": PatientProgressViewTemplate
        }
    };

对于数组,试试这个解决方案:

    var patientFieldCtx = {};
    var columns = ["Progress1", "Progress2"];
    patientFieldCtx.Templates = {};
    patientFieldCtx.Templates.Fields = {};
    for (var index = 0; index < columns.length; index++) { 
            patientFieldCtx.Templates.Fields[columns[index]]= {"View": PatientProgressViewTemplate};
    }
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(patientFieldCtx);

【讨论】:

  • 评估解决方案不起作用,但您的第一个答案确实有效。非常感谢你的帮忙。理想情况下,我希望 eval 解决方案能够正常工作,因为我们可能也希望应用许多领域。也许不同之处在于 eval 的工作是在一个循环中,这会导致问题吗?无论如何,取得一些进展是件好事。
猜你喜欢
  • 2019-06-22
  • 2020-08-31
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 2011-10-07
  • 1970-01-01
  • 2013-04-18
相关资源
最近更新 更多