pnp / List-Formatting

List Formatting Samples for use in SharePoint and Microsoft Lists
https://pnp.github.io/List-Formatting/
MIT License
1.71k stars 824 forks source link

Column Formatting: forEach won't iterate over arrays #642

Closed benjblack closed 1 year ago

benjblack commented 1 year ago

Sample

https://github.com/pnp/List-Formatting/tree/master/column-samples/multi-choice-foreach

What Should Happen

According to the documentation, forEach iterates over a multi-choice field or an array. ex., from the documentation: "iteratorName in Expression-Returning-An-Array"

So in multi-choice-foreach.json, I should be able to replace @currentField with:

split('hello world yo', ' ')

That way, I could have a comma-delimited string in a text field, split it, and write display the different values, like so:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "forEach": "choiceIterator in split('hello world yo', ' ')",
      "elmType": "div",
      "txtContent": "[$choiceIterator]",
      "attributes": {
        "class": "ms-bgColor-themePrimary ms-fontColor-white"
      },
      "style": {
        "width": "16px",
        "height": "16px",
        "text-align": "center",
        "margin": "1px"
      }
    }
  ]
}

What Actually Happens

Nothing is rendered in the cell. Tested in SharePoint online in a modern site / modern list experience.

Fedes365 commented 1 year ago

Hello @benjblack

Can you tell me what's the internal name of the column you used to test this sample?

Fedes365 commented 1 year ago

I did a quick test and it didn't work when split operator is used to create an array.

thechriskent commented 1 year ago

This is a weird bug where spaces are the problem. To see it in action, switch this line:

"forEach": "choiceIterator in split('hello world yo', ' ')",

To this one (no spaces at all in either the value or the expression after the in

 "forEach": "choiceIterator in split('hello|world|yo','|')",

And you'll see things work. It isn't really supposed to work and the use of split in a forEach isn't technically the intended scenario. But it absolutely should.

I recommend opening this as an issue on the official issues list so that they can work on it there (this one is just for samples)

Fedes365 commented 1 year ago

@thechriskent thanks for your precious support and clarification!