更改列格式的当前代码以包括图标

时间:2019-09-24 04:07:55

标签: javascript json sharepoint-online

我希望有人能提供帮助-我对JSON或编码一无所知。

我有一个共享点在线列表,该列表已格式化为根据每个项目中显示的文本显示不同的颜色。 我现在也希望在列中添加图标。例如,如果状态为“新建”,则我希望它是我在代码中选择的颜色,但还希望从Office UI Fabric中添加一个+图标。

下面是我使用的两个代码,它们分别工作,我想将它们结合在一起。

    {
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "span",
      "style": {
        "display": "inline-block",
        "padding": "0 4px"
      },
      "attributes": {
        "iconName": "=if(@currentField == 'Completed', 'CheckMark', if(@currentField == 'Draft phase', 'Forward', if(@currentField == 'Approval phase', 'Error', if(@currentField == 'On hold', 'Warning', 'ErrorBadge'))))"
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ]
}

   {
  "elmType": "div",
  "style": {
    "padding": "0 4px"
  },
  "attributes": {
    "class": {
      "operator": ":",
      "operands": [
        {
          "operator": "==",
          "operands": [
            {
              "operator": "toLowerCase",
              "operands": [
                "@currentField"
              ]
            },
            {
              "operator": "toLowerCase",
              "operands": [
                "New"
              ]
            }
          ]
        },
        "sp-css-backgroundColor-blueBackground07",
        {
          "operator": ":",
          "operands": [
            {
              "operator": "==",
              "operands": [
                {
                  "operator": "toLowerCase",
                  "operands": [
                    "@currentField"
                  ]
                },
                {
                  "operator": "toLowerCase",
                  "operands": [
                    "Draft phase"
                  ]
                }
              ]
            },
            "sp-css-backgroundColor-warningBackground",
            {
              "operator": ":",
              "operands": [
                {
                  "operator": "==",
                  "operands": [
                    {
                      "operator": "toLowerCase",
                      "operands": [
                        "@currentField"
                      ]
                    },
                    {
                      "operator": "toLowerCase",
                      "operands": [
                        "Approval phase"
                      ]
                    }
                  ]
                },
                "sp-css-backgroundColor-successBackground",
                {
                  "operator": ":",
                  "operands": [
                    {
                      "operator": "==",
                      "operands": [
                        {
                          "operator": "toLowerCase",
                          "operands": [
                            "@currentField"
                          ]
                        },
                        {
                          "operator": "toLowerCase",
                          "operands": [
                            "Ready for upload"
                          ]
                        }
                      ]
                    },
                    "sp-css-backgroundColor-blockingBackground",
                    {
                      "operator": ":",
                      "operands": [
                        {
                          "operator": "==",
                          "operands": [
                            {
                              "operator": "toLowerCase",
                              "operands": [
                                "@currentField"
                              ]
                            },
                            {
                              "operator": "toLowerCase",
                              "operands": [
                                "Completed"
                              ]
                            }
                          ]
                        },
                        "sp-css-backgroundColor-neutralBackground",
                        {
                          "operator": ":",
                          "operands": [
                            {
                              "operator": "==",
                              "operands": [
                                {
                                  "operator": "toLowerCase",
                                  "operands": [
                                    "@currentField"
                                  ]
                                },
                                {
                                  "operator": "toLowerCase",
                                  "operands": [
                                    "On hold"
                                  ]
                                }
                              ]
                            },
                            "sp-css-backgroundColor-errorBackground",
                            ""
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  },
  "txtContent": "@currentField"
}

1 个答案:

答案 0 :(得分:0)

尝试一下。

enter image description here

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "padding": "0 4px"
  },
  "attributes": {
    "class": {
      "operator": ":",
      "operands": [
        {
          "operator": "==",
          "operands": [
            {
              "operator": "toLowerCase",
              "operands": [
                "@currentField"
              ]
            },
            {
              "operator": "toLowerCase",
              "operands": [
                "New"
              ]
            }
          ]
        },
        "sp-css-backgroundColor-blueBackground07",
        {
          "operator": ":",
          "operands": [
            {
              "operator": "==",
              "operands": [
                {
                  "operator": "toLowerCase",
                  "operands": [
                    "@currentField"
                  ]
                },
                {
                  "operator": "toLowerCase",
                  "operands": [
                    "Draft phase"
                  ]
                }
              ]
            },
            "sp-css-backgroundColor-warningBackground",
            {
              "operator": ":",
              "operands": [
                {
                  "operator": "==",
                  "operands": [
                    {
                      "operator": "toLowerCase",
                      "operands": [
                        "@currentField"
                      ]
                    },
                    {
                      "operator": "toLowerCase",
                      "operands": [
                        "Approval phase"
                      ]
                    }
                  ]
                },
                "sp-css-backgroundColor-successBackground",
                {
                  "operator": ":",
                  "operands": [
                    {
                      "operator": "==",
                      "operands": [
                        {
                          "operator": "toLowerCase",
                          "operands": [
                            "@currentField"
                          ]
                        },
                        {
                          "operator": "toLowerCase",
                          "operands": [
                            "Ready for upload"
                          ]
                        }
                      ]
                    },
                    "sp-css-backgroundColor-blockingBackground",
                    {
                      "operator": ":",
                      "operands": [
                        {
                          "operator": "==",
                          "operands": [
                            {
                              "operator": "toLowerCase",
                              "operands": [
                                "@currentField"
                              ]
                            },
                            {
                              "operator": "toLowerCase",
                              "operands": [
                                "Completed"
                              ]
                            }
                          ]
                        },
                        "sp-css-backgroundColor-neutralBackground",
                        {
                          "operator": ":",
                          "operands": [
                            {
                              "operator": "==",
                              "operands": [
                                {
                                  "operator": "toLowerCase",
                                  "operands": [
                                    "@currentField"
                                  ]
                                },
                                {
                                  "operator": "toLowerCase",
                                  "operands": [
                                    "On hold"
                                  ]
                                }
                              ]
                            },
                            "sp-css-backgroundColor-errorBackground",
                            ""
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  },
  "children": [
    {
      "elmType": "span",
      "style": {
        "display": "inline-block",
        "padding": "0 4px"
      },
      "attributes": {
        "iconName": "=if(@currentField == 'Completed', 'CheckMark', if(@currentField == 'Draft phase', 'Forward', if(@currentField == 'Approval phase', 'Error', if(@currentField == 'On hold', 'Warning', 'ErrorBadge'))))"
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ]
}