Developer forum

Forum » Templates » Remove commas from last iteration of loop

Remove commas from last iteration of loop

Jens Mouritzen
Jens Mouritzen
Reply

Hi guys

Any idea how to remove specific characters from the last iteration of a loop? We have a demo solution, where we want to remove the comma the from the lasty iteration of a checkbox list.

Itempublisher template attached.
Screenshot of output on frontend attached

frontend.png

Replies

 
Nicolai Pedersen
Reply

Hi Jens

Your caseItem.GetLoop("ItemPublisher:Item.SolutionType.Options") is a collection/enumeration, so you can do something like this:

var options = caseItem.GetLoop("ItemPublisher:Item.SolutionType.Options");
var totalOptionsCount = options.Count;
int optionsRendered = 0;
@foreach(var i in options)
{
    if(i.GetBoolean("ItemPublisher:Item.SolutionType.Option.IsSelected"))
    {
        var solutionType = i.GetString("ItemPublisher:Item.SolutionType.Option.Label");
        if(!(++optionsRendered == totalOptionsCount)){
            solutionType += ", ";
        }
        @solutionType
        }
}

 
Jens Mouritzen
Jens Mouritzen
Reply

Hi Nicolai, thank you for your answer.
It only works, when all the choices in the checkbox list is selected (6 total). Can i make it, so it will apply to only the ones selected (eg. 4 where that last does not have a comma)?

It cant figure it out  :|

 
Nicolai Pedersen
Reply

Some LINQ should do that:

var totalOptionsCount = options.Count(option => option.GetBoolean("ItemPublisher:Item.SolutionType.Option.IsSelected") == true);

 
Jens Mouritzen
Jens Mouritzen
Reply

Thanks Nicolai, that did it!

 

You must be logged in to post in the forum