0

I have the below function,which colors Title column in SP Online. I want to extract just the last 2 characters from "Period" and use it in the Switch case.

I tried Slice,Substr function but both didn't work. Can someone help me?

function PeriodView(ctx) {
var Period = ctx.CurrentItem["Title"];
switch (Period)
{
case "2017-09":
return '<b><u>' + Period + '</u></b>';
break;
case "2017-10":
return '<b><i>' + Period + '</i></b>';
break;
default:
return '<center>' + Period + '</center>';
break;
}
}
3
  • var Period = ctx.CurrentItem["Title"]; var slicedPeriod = Period.slice(-2); switch (slicedPeriod) Commented Feb 5, 2018 at 11:18
  • @Christoffer This worked.
    – George
    Commented Feb 5, 2018 at 12:29
  • String operations on Dates will (eventually) cause headaches ... SharePoint has extra functions to deal with Dates: see: sharepoint.stackexchange.com/questions/160806/… Commented Feb 5, 2018 at 16:16

2 Answers 2

0

As Christoffer says, you can handle slicing like this:

var slicedPeriod = Period.length > 2? Period.substr(-2): Period

And then just continue with it in your switch.

0

This worked for me:

var Period = ctx.CurrentItem["Title"]; 
var slicedPeriod = Period.slice(-2); 
switch (slicedPeriod)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.