You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Sanity Studio中显示对象数组的多个属性?

解决Sanity Studio对象数组预览多属性的问题

完全可以实现,核心是在对象类型的Schema中配置preview属性,自定义数组项的预览显示内容,这种方法也适用于其他需要多属性预览的字段。

直接修改你的translation对象Schema即可:

// sanity\schemas\objects\translation.ts

import { defineField, defineType } from "sanity"

export default defineType({
    title: 'Translation',
    name: 'translation',
    type: 'object',
    fields: [
        defineField({ title: 'Language', name: 'language', type: 'string' }),
        defineField({ title: 'Article', name: 'article', type: 'reference', to: [{ type: 'article' }] }),
    ],
    // 新增预览配置,自定义显示格式
    preview: {
        select: {
            language: 'language',
            articleTitle: 'article.title' // 提取引用文章的title字段
        },
        prepare(selection) {
            const { language, articleTitle } = selection;
            // 处理空值,避免异常显示
            const displayText = articleTitle 
                ? `${language} - ${articleTitle}` 
                : language || '未配置翻译';
            return {
                title: displayText
            };
        }
    }
});

关键说明:

  • select:指定需要提取的字段,支持引用类型的嵌套属性(比如这里的article.title)。
  • prepare:把提取到的字段拼接成你需要的格式,同时处理空值场景(比如未选择文章、语言未填写的情况),保证界面显示友好。

这种方式是Sanity原生支持的自定义预览方案,完全不需要依赖国际化插件,其他对象类型要实现类似效果,只需要在对应Schema里添加相同逻辑的preview配置即可。

内容的提问来源于stack exchange,提问作者Aislinn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 21:50:33