如何通过JSON列格式化模拟Lookup列链接的默认样式?
问题描述
我正在尝试使用列格式化功能,在其他列中模拟Lookup列的链接样式。
原生Lookup列的链接在列单元格内具备特定的颜色规则与定位样式,我希望自定义的My Column列内的链接,外观与Lookup Column列的原生链接完全一致,效果可参考示例截图:
通过浏览器开发者工具排查,我发现两类列生成的HTML结构存在差异:
- 原生
Lookup列链接HTML结构:
<div role="gridcell" aria-readonly="true" aria-colindex="7" class="ms-DetailsRow-cell cell-223 cellUnpadded-111" data-automationid="DetailsRowCell" data-automation-key="LookupColumn" style="width: 190px;"> <div> <button type="button" class="ms-Link od-FieldRender od-FieldRender-lookup root-211" aria-label="My Lookup Value Here" role="link" tabindex="-1"> My Lookup Value Here </button> </div> </div>
- 自定义
My Column列链接HTML结构:
<div role="gridcell" aria-readonly="true" aria-colindex="8" class="ms-DetailsRow-cell cell-113 cellUnpadded-111" data-automationid="DetailsRowCell" data-automation-key="MyColumn" style="width: 299px;"> <div class="customField_ab5ae447"> <div class="sp-field-customFormatter"> <a style="margin-left:30px;" target="_blank" href="https://some-link-here.com" rel="noopener noreferrer " data-interception="off" tabindex="-1"> click here to view something </a> </div> </div> </div>
当前我用于在My Column中生成链接的JSON格式化代码如下:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "children": [ { "style": { "margin-left": "30px" }, "elmType": "a", "txtContent": "click here to view something", "attributes": { "target": "_blank", "href": "https://some-link-here.com" } } ] }
需要修改上述JSON配置,让自定义链接的样式与原生Lookup列链接完全一致。
补充说明:我已参考过同类问题的解决方案,但其中提供的方法最终呈现的样式仍无法与原生
Lookup链接列的样式匹配。
解决方法
不需要手动硬编码字体、颜色、边距等样式属性,直接复用原生Lookup列使用的内置UI类名,即可1:1匹配样式,还能自动适配站点主题变更,修改后的JSON配置如下:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "children": [ { "elmType": "a", "txtContent": "click here to view something", "attributes": { "target": "_blank", "href": "https://some-link-here.com", "class": "ms-Link od-FieldRender od-FieldRender-lookup" } } ] }
调整点说明:
- 删除了自定义写死的
margin-left: 30px样式,原生Lookup列的内边距、对齐规则由内置类自动控制,会适配列表单元格的通用布局规则,不会出现偏移 - 给
a标签添加三个原生Lookup元素使用的公共类:ms-Link:继承Fluent UI全局链接的字体、颜色、hover下划线、交互态样式od-FieldRender:匹配SharePoint列表字段渲染的通用行高、定位规则od-FieldRender-lookup:匹配查找字段专属的字重、选中态、单元格内对齐样式
- 不需要额外添加任何自定义style属性,最终渲染的链接样式会和原生Lookup列完全一致,站点切换主题时也会自动同步样式,不会出现色差。
内容的提问来源于stack exchange,提问作者user1063287
相关产品推荐
相关产品推荐

