首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义AdminJs中外键相关表值的显示值

自定义AdminJs中外键相关表值的显示值
EN

Stack Overflow用户
提问于 2022-06-26 22:13:49
回答 1查看 178关注 0票数 0

我对行政管理很陌生,我面临着以下挑战:我有一个同时包含idname列的表。Adminjs自动显示用外键引用到父表的name的列的id值。(很棒的特征!)

我与一个父表有另一个外键关系,该表有一个标记为proper_name的列,而不仅仅是name。我希望通过显示proper_name列值重新创建此表显示行为,而不是在引用外键相关表时仅显示父表外键id值。

是否有一种自定义/重写此显示行为的方法,以便下拉列表显示父表中除id列之外的另一列的值?

父表模型:

代码语言:javascript
复制
class parent_1_table extends Model {
    static associate(models) {
    }
  }
  parent_table.init({
    id: {
      type: Sequelize.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    proper_name: {
      type: Sequelize.STRING(32),
      isTitle: true,
    },
  }, {
    sequelize,
    modelName: 'parent_table',
    timestamps: false,
    createdAt: false,
    updatedAt: false,
    freezeTableName: true,
    tableName: 'parent_table',
  });

儿童桌:

代码语言:javascript
复制
class child_table extends Model {
    static associate(models) {
    }
  }
  child_table.init({
    parent_1_id: {
      type: Sequelize.BIGINT,
      allowNull: false,
    },
    parent_2_id: {
      type: Sequelize.BIGINT,
      allowNull: false,
    },
  }, {
    sequelize,
    modelName: 'child_table',
    timestamps: false,
    createdAt: false,
    updatedAt: false,
    freezeTableName: true,
    tableName: 'child_table',
  });

外键关系是在index.js文件的事实之后定义的:

代码语言:javascript
复制
child_table.belongsTo(parent_1_table, { foreignKey: 'parent_1_id' });
child_table.belongsTo(parent_2, { foreignKey: 'parent_2_id' });
EN

回答 1

Stack Overflow用户

发布于 2022-06-27 07:37:25

社区中的项目贡献者之一澄清了上述isTitle: true解决方案需要在选项领域的列定义之后:

代码语言:javascript
复制
class parent_1_table extends Model {
    static associate(models) {
    }
  }
  parent_table.init({
    id: {
      type: Sequelize.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    proper_name: {
      type: Sequelize.STRING(32),
      // isTitle: true, * not here
    },
  }, {
    sequelize,
    properties: { // put these display options here!
      proper_name: {
        isTitle: true,
      },
    },
    modelName: 'parent_table',
    timestamps: false,
    createdAt: false,
    updatedAt: false,
    freezeTableName: true,
    tableName: 'parent_table',
  });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72765502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档