Answer the question
In order to leave comments, you need to log in
SequelizeJs how to implement tree view?
Good hour and straight to the point.
Let's say there is a list of combinations in the database:
combs -> [id:1, name:'noname', weight:100500]
combs -> [id:2, name:'noname', weight:100500]
combs -> [id:3, name:'noname', weight:100500]
conf -> [id:1, name:'noname', combs:[1, 2, 3]]
conf -> [id:2, name:'noname', combs:[3, 4, 5]]
conf -> [id:3, name:'noname', combs:[3, 1, 4]]
confModel.findAll( ).then(function(items:any):any {
var tree = items.map(function(item:any):any {
return item.get({plain: true});
});
console.log(tree);
// [{
// id:1,
// name:'noname',
// combs:[
// {id:1, weight:100500},
// {id:2, weight:100500},
// {id:3, weight:100500}
// },{
// id:2,
// name:'noname',
// combs:[
// {id:3, weight:100500},
// {id:4, weight:100500},
// {id:5, weight:100500}
// }];
});
Answer the question
In order to leave comments, you need to log in
In php, everything is simpler, because it uses the database synchronously.
This is where connections need to be made.
Methods: hasMany is not suitable because the brainchild of JOIN.
And here it is necessary to make a repeated request for each pass, but to the "combs" table.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question