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

发布于 2025-02-10 16:04:34 字数 1678 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2025-02-17 16:04:34

社区不和谐中的项目贡献者之一阐明了上述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',
  });

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:

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',
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文