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;
}
}
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, privacy policy and cookie policy

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