自定义adminj中的外键相关表值的显示值
我对 adminjs ,我正在面临以下挑战: 我有一个具有ID
和名称
列的表。 Adminjs自动显示了name
值的列,这些列是用外键引用的,该列的id> id
的。 (很棒的功能!)
我与父表有另一个外键关系,该列表标有promer_name
而不是name
。我很想使用显示的promer_name
列值重新创建该表显示行为,而不仅仅是显示父表exourth forefer 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' });
I’m newish to Adminjs and I’m having the following challenge:
I have one table with both id
and name
columns. Adminjs automatically shows the name
value for columns that are referenced with a foreign key to the id
of the parent table. (Great feature!)
I have another foreign key relationship to a parent table that has a column labeled proper_name
instead of just name
. I would love to recreate this table display behavior with the proper_name
column values being displayed instead of just displaying the parent table foreign key id
values when referencing the foreign key related table.
Is there a way to customize/override this display behavior so the dropdown shows the value of another column from the parent table other than the id
column?
parent table model:
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',
});
child 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',
});
foreign key relationships are defined after the fact in the index.js
file:
child_table.belongsTo(parent_1_table, { foreignKey: 'parent_1_id' });
child_table.belongsTo(parent_2, { foreignKey: 'parent_2_id' });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
社区不和谐中的项目贡献者之一阐明了上述
istitle:true
解决方案需要在选项区域的列定义之后:One of the project contributors in the community Discord clarified the above
isTitle: true
solution needs to be AFTER the column definitions in the options area: