Webpart javascript 不适用于数组

Webpart javascript does not work for array

我不知道 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 来测试它。我想我将不得不这样做。

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

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);