如何在保留链接的同时将多值Lookup列值显示在一行
多值Lookup列:保留跳转链接+单行显示+无末尾多余逗号
SharePoint里的Lookup列默认会把值做成链接,点击可跳转至对应列表项;要是开启「允许多个值」,每个值就会单独占一行。
之前为了让值单行显示,用了这段列格式代码,虽然实现了逗号分隔,但所有链接都丢失了:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "=join(@currentField.lookupValue, ', ')" }
现在要实现的效果是:每个值保留跳转链接,同时单行排列,最后一个值后无多余逗号。
解决思路
利用forEach循环遍历每个Lookup项,通过判断当前项的索引是否为最后一项,控制逗号的显示:
- 用
loopIndex('lookupItem')获取当前项的索引(从0开始计数) - 用
length(@currentField)获取总项数 - 若当前索引等于「总项数-1」,则隐藏逗号;否则显示「, 」作为分隔
最终可用代码
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "elmType": "div", "children": [ { "forEach": "lookupItem in @currentField", "elmType": "span", "children": [ { "elmType": "a", "txtContent": "=[$lookupItem.lookupValue]", "attributes": { "target": "_blank", "href": "='https://tenant-name.sharepoint.com/sites/site-name/Lists/Some%20List/DispForm.aspx?ID=' + [$lookupItem.lookupId]" } }, { "elmType": "span", "txtContent": ", ", "style": { "display": "=if(loopIndex('lookupItem') == length(@currentField) - 1, 'none', 'inline')" } } ] } ] }
代码说明
- 通过
forEach遍历每个Lookup值,迭代器命名为lookupItem - 每个值拆分为两个子元素:带跳转链接的文本、分隔用的逗号
- 用条件判断控制逗号显示:最后一项的逗号设置
display: none隐藏,其余项正常显示 - 跳转链接通过拼接
lookupId构造,确保能精准跳转到对应列表项的详情页
内容的提问来源于stack exchange,提问作者user1063287
相关产品推荐
相关产品推荐

