2

I have this specific requirement to color code the due date field if it's value is less than 180 days (6 months) compared to today's date.

Is this possible with Javascript/JSLink ?

Thanks

Luis Carvalho
  • 483
  • 1
  • 11
  • 27

2 Answers2

3

Yes absolutely. Here's a quick code example that I mostly borrowed from here:

(function () {
    var statusFieldCtx = {};

    statusFieldCtx.Templates = {};
    statusFieldCtx.Templates.Fields = {
        "DueDate": {
            "View": ColorCodeDueDate
            }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFieldCtx);
})();

function ColorCodeDueDate(ctx) {
    var dueDate = new Date(ctx.CurrentItem.DueDate);

    var now = new Date();
    var then = new Date();
    then.setDate(now.getDate() + 180);

    // if there's no due date don't render anything
    if (dueDate == 'undefined' || !dueDate) {
        return '';
    }
    else if (dueDate >= now && dueDate <= then) { // we are within 180 days
        return "<div style='background-color:red;color:white'>" + dueDate.toLocaleDateString() + "</div>";
    }
    else {  // we are outside of 180 days
        return "<div>" + dueDate.toLocaleDateString() + "</div>";
    }
}

If your field's internal name is "MaxEndDate", change the line

"DueDate:" {

to

"MaxEndDate": {

and then change the line

var dueDate = new Date(ctx.CurrentItem.DueDate);

to

var dueDate = new Date(ctx.CurrentItem.MaxEndDate);

The color is just defined using the inline CSS style of the div markup you are returning. In the code example above, I am returning a div with a red background and white text. If all you need is red text on the default background, you would do something like

return "<div style='color:red'>" + dueDate.toLocaleDateString() + "</div";
Dylan Cristy
  • 12,712
  • 3
  • 31
  • 71
  • The color should be RED if the value is within 180 days, i.e 179 days. what piece in the code do I need to modify? my field is actually named "MaxEndDate" – Luis Carvalho Mar 01 '16 at 17:20
  • 1
    this section, style='background-color:red;color:white' gives you white text on a red backgroud, if you just want red text, then do color:red – Eric Alexander Mar 01 '16 at 17:38
  • @Eric Alexander. to target my field "MaxEndDate", do I only replace "DueDate" in statusFieldCtx.Templates.Fields section ? I read somewhere that this where the field is set for manipulation. – Luis Carvalho Mar 01 '16 at 17:50
  • @LuisCarvalho please see my update, you need to reference your real field name in two places. – Dylan Cristy Mar 01 '16 at 17:51
  • @Dylan Cristy , for some reason the MaxEnddate is now incorrectly formating the date. the format should be DD/MM/YYYY. if I edit the field value, the format is changed to MM/DD/YYYY. Any idea why this is happening? – Luis Carvalho Mar 02 '16 at 13:47
  • The difference in formats sounds like a localization setting, maybe. But, I can't tell you why it would be formatted one way and then change only after an edit. – Dylan Cristy Mar 02 '16 at 13:57
  • it is only happing in this field. I have other date fileds in this list and it is fine. As is said, prior to this code the field was formatting the date correctly. In fact, when I remove the JSLink the field works correctly again – Luis Carvalho Mar 02 '16 at 14:17
  • Then it probably has to do with how the date is getting formatted by using the date.ToLocaleDateString() method. You should investigate other ways of getting formatted dates from Javascript Date objects in order to get the formatting that you want. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Dylan Cristy Mar 02 '16 at 14:20
  • I just need to output the date to this format DD/MM/YYYY. – Luis Carvalho Mar 02 '16 at 14:30
  • Try dueDate.toLocaleDateString("en-GB"). Please also read this web page about toLocaleDateString() so you can learn about how to use toLocaleDateString() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString – Dylan Cristy Mar 02 '16 at 14:38
  • Unfortunately this code is completing messing my dates. even when I type the English US format, i.e. MM/DD/YYYY it does not always work correctly. when I use 08/25/2016 it will not accept the date (red marks it). also tried dueDate.toLocaleDateString("en-GB") but still does not work. – Luis Carvalho Mar 03 '16 at 10:28
  • Date entry has nothing to do with how CSR displays it; the Site (settings) Locale setting determines how you can enter dates – Danny '365CSI' Engelman Mar 03 '16 at 11:11
  • @Danny Engelman. My Locale is english united kingdom. As I said I have not problems if I remove the code. Please read the entire post.. – Luis Carvalho Mar 03 '16 at 11:31
  • I am getting somewhere. it seams date if I change my Locale to English US, then the code works correctly – Luis Carvalho Mar 03 '16 at 11:57
1

I whipped this up quick, works on a standard Task List.

function ColorCodeDueDate(ctx) {
  var fieldname=ctx.CurrentFieldSchema.RealFieldName,
      datevalue=new Date(ctx.CurrentItem[fieldname]),
      days=GetDaysAfterToday(datevalue);
  if (!days) return 'No '+fieldname;
  if (days<5) style="background-color:red;color:white";
  if (days<-30) style="background-color:green;color:white";
  return String.format("<div style='{0}'>{1} days from {2:dd/MM/yyyy}</div>",style,days,datevalue);
}

Notes:

  • Use the Chrome Extension Cisar (by Andrei Markeev) to code CSR files.
  • Should be easy to spot how to change it to your 180 days
  • Do not hardcode internal fieldnames; this function can be applied to any DateTime field
  • Try to code with only one exit point (return) in your functions; makes it a lot easier to read
  • GetDaysAfterToday() comes from SharePoints own sp.datetimeutil.js library, but you might have to load it as extra JSLink first; because SP loads it after your CSR code executes
    (OR copy that function code into your own CSR file)
  • String.format() comes from SharePoints msajaxbundle.js; which is loaded before your CSR code executes

With the https://iCSR.github.io Library you do not have to write a function at all

If you declare the View as:

'MaxEndDate':{
  View : iCSR.DueDate({
                        ranges:'[msRed],0,#FFF1AD,90,[msGreen]'
                        ,rowcolor:true
                        })
}

You get this: (screenshot from another post)

  • msRed and msGreen are iCSR tokens matching the colors from the new Microsoft Planner
  • iCSR adjusts the textcolor contrast for you (on each cell, you can't set a textcolor on a TR row)
  • and more

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79