Rusty on JS and could use a sharp eye to see what I’m missing.
Have tried both recommendations for JS formats for color coding rows and can’t get either to work.
- Added to the Misc JS Link field of the WebPart of the view I’d like to color code
- Uploaded to my scripts folder
- New column is called LiveDate; need to highlight any future dates
- Using SP 2013
Path used to point to JS file: ~SiteAssets/Scripts/targetingcolor2.js
JS code attempt #1:
var rightNow = new Date(); // rightNow has the date and time value of the moment it was created function colorCodeRows() { SPClientTemplates.TemplateManager.RegisterTemplateOverrides({ OnPostRender: function (ctx) { // get today's date var today = new Date(); // zero out the time portion so we will only compare days today.setHours(0,0,0,0); var rows = ctx.ListData.Row; for (var i = 0; i < rows.length; i++) { // get the date set in your date YourDateField var itemDate = new Date(rows[i]['LiveDate']); // zero out the time portion so we only compare days itemDate.setHours(0,0,0,0); var rowId = GenerateIIDForListItem(ctx, rows[i]); var row = document.getElementById(rowId); if (itemDate >= today) { row.style.backgroundColor = '#FFFFE0'; } } } }); } RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens('~SiteAssets/Scripts/targetingcolor.js'), colorCodeRows); colorCodeRows();
JS Code Attempt #2:
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() { SPClientTemplates.TemplateManager.RegisterTemplateOverrides({ OnPostRender: function(ctx) { // get today's date var today = new Date(); // zero out the time portion so we will only compare days today.setHours(0,0,0,0); }; var rows = ctx.ListData.Row; for (var i = 0; i < rows.length; i++) { // get the date set in your date YourDateField var itemDate = new Date(rows[i]['LiveDate']); // zero out the time portion so we only compare days itemDate.setHours(0,0,0,0); var rowId = GenerateIIDForListItem(ctx, rows[i]); var row = document.getElementById(rowId); if (itemDate >= today) { row.style.backgroundColor = '#FFFFE0'; } } }); });
Any help would be appreciated!