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;
}
}
George
  • 1,512
  • 2
  • 24
  • 60

2 Answers2

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.

kpfeuffer
  • 3
  • 2
0

This worked for me:

var Period = ctx.CurrentItem["Title"]; 
var slicedPeriod = Period.slice(-2); 
switch (slicedPeriod)
George
  • 1,512
  • 2
  • 24
  • 60