【问题标题】:Keystonejs admin ui : View field instead of id for relationshipsKeystonejs admin ui:查看字段而不是关系的 id
【发布时间】:2021-07-30 06:28:49
【问题描述】:

这是一个非常简单的问题:

当我在列表中有一个name 字段并设置了关系时,该关系会正确显示链接文档的名称。例如,在该列表中,我有一个名为 business 的字段,它与具有 name 字段的 Business 列表有关系。

但是当我的关系是一个没有 name 字段的列表时,只有 id 显示在管理 UI 中,如下所示:

这显然是非常不切实际的。

如何配置 keystone 以在管理 ui 中显示特定字段来表示文档?

【问题讨论】:

    标签: keystonejs


    【解决方案1】:

    Keystone 仅将 name 字段显示为文档标题或关系摘要。

    但是有一个简单的解决方案可以将自定义字段用作name 字段:map 选项。创建模型时,将要显示的字段映射到name 字段。

    例如我有本地化的文本字段,我想默认显示英文版本:

    const LocalizedText = new keystone.List('LocalizedText', {
      map: { name: 'en' } // <=== This is the solution
    });
    
    LocalizedText.add({
      fr: { type: String },
      en: { type: String }
    });
    
    LocalizedText.register();
    

    Here is the documentation :

    地图

    将字段映射到特殊列表路径的对象。如果添加了具有该键的字段,则每个路径都默认为其键。可映射路径包括

    name - 包含项目名称的字段,用于在管理 UI 中显示。

    【讨论】:

      【解决方案2】:

      我知道这是一个老问题,但遇到了同样的问题,我在这里留下了如何在 Keystone 5 中解决它,希望它可以帮助其他人。

      您只需要在创建列表时添加一个 labelField,使用要显示的字段名称而不是 id。

      keystone.createList('User', {
        fields: {
          name: { type: Text },
          email: { type: Text },
        },
        labelField: 'name',
      });
      

      文档here

      【讨论】:

      • 您也可以使用'labelResolve'自定义标签:
      【解决方案3】:

      自定义标签示例:

      const Post = {  
      fields: { 
          title: { type: Text },
          // other fields
        },
        labelResolver: item => item.title,
      };  
      

      使用更多逻辑:

        const Post = {
        fields: {
          title: { type: Text },
          status: {
            type: Select,
            defaultValue: 'draft',
            options: [
              { label: 'Draft', value: 'draft' },
              { label: 'Published', value: 'published' },
            ],
          },
          // and the rest of the fields too
        },
        labelResolver: item => `${item.status === 'draft' ? 'DRAFT - ' : ''}` + item.title,
      };
      

      使用查询结果:

        const Post = {
        fields: {
          author: {
            type: AuthedRelationship,
            ref: 'User',
            isRequired: true,
          },
          // and the rest of the fields too
        },
        labelResolver: async item => {
          const { data } = await keystone.executeGraphQL({
            query: `query {
                User(where: {id: "${item.author}" }) {
                  name
                }
              }`,
          });
      
          return `${item.status === 'draft' ? 'DRAFT ' : ''}` + item.title + ` (${data.User.name})`;
        },
      };
      

      详情来自keystoneJs的博客:
      Using customized label

      【讨论】:

        猜你喜欢
        • 2018-03-16
        • 2018-05-20
        • 2014-10-27
        • 2016-08-22
        • 2021-08-16
        • 1970-01-01
        • 2015-06-19
        • 2020-08-19
        • 2017-01-17
        相关资源
        最近更新 更多