express-admin icon indicating copy to clipboard operation
express-admin copied to clipboard

OneToMany mapping with two tables

Open aniruddhasm opened this issue 9 years ago • 4 comments
trafficstars

Hello team,

I've a Category masters table with column (name, parent) Topic table with columns (name, category_id) SubTopic table with columns (name, topic_id)

The way I want to show topic_id in Listing UI is name-category(Actual category name) Below is my code

 "name": "topic_id",
                "verbose": "Topic",
                "control": {
                    "select": true
                },
                "oneToMany": {
                    "table": "topic",
                    "pk": "id",
                    "columns": [
                        "name",
                        "category_id"
                    ]
                },
                "type": "int(100)",
                "allowNull": false,
                "defaultValue": null,
                "listview": {
                    "show": true
                },
                "editview": {
                    "show": true
                }

The above code produces output as

(name)cartoon 1(category_id)

where cartoon is name 1 is the category_id Here instead of 1 I want the actual category name.

Please help.

aniruddhasm avatar Mar 16 '16 12:03 aniruddhasm

Currently it's not possible to configure such relationship through the configuration.

simov avatar Mar 16 '16 12:03 simov

ok Thank You

aniruddhasm avatar Mar 17 '16 17:03 aniruddhasm

Hello,

I'm trying a work around

I've created a field helpertext in topic table that is hidden. So when I save the topic table I have written a preSave event to save the helpertext as (name)cartoon 1(category_id)


exports.preSave = function (req, res, args, next) {
    if (args.debug) console.log('preSave');
    debugger;
    if (args.name == 'topic') {
        record = args.data.view.topic.records[0].columns;
        record.helpertext = record.name + "-" + record.category_id;
        console.log(JSON.stringify(record));
    }
    next();
}

but instead of this I want category name. So I did this

exports.preSave = function (req, res, args, next) {
    if (args.debug) console.log('preSave');
    debugger;
    if (args.name == 'topic') {
        record = args.data.view.topic.records[0].columns;
                var inline = args.data.manyToOne.topic;

        console.log(JSON.stringify(args.data));
        record.helpertext = record.category_id + "-" + record.name;
        console.log(JSON.stringify(record));
    }
    next();
}

I am getting error at var inline = args.data.manyToOne.topic;

Please help

aniruddhasm avatar Mar 22 '16 14:03 aniruddhasm

+1. Would love to see this feature

ikrasnykh avatar Jul 03 '17 05:07 ikrasnykh