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



我是新来的Adminjs,我有以下挑战:我有一个包含idname列的表。Adminjs自动显示name值的列与外键引用到父表的id。(伟大的功能!)

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

是否有一种方法来自定义/覆盖此显示行为,使下拉菜单显示父表中id列以外的另一列的值?

父表模型:

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',
});

子表:

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文件中定义的:

child_table.belongsTo(parent_1_table, { foreignKey: 'parent_1_id' });
child_table.belongsTo(parent_2, { foreignKey: 'parent_2_id' });

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

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',
});

最新更新