0

I have come across an issue where I cannot figure out what would be approach to make a dynamic drop down menu with sub dropdowns that comes from database. I made api that pulls product categories and subcategories from database and it looks like this when I put it in model.

db_table

export class Category {
    id : any;
    parent_id : any;
    name : any;
    created : any;
    modified : any;
    status : any;
}

Example

  |id | parent_id  |   name      |        
  | 1 |      0     |   cars      |        
  | 2 |      1     |   ford      |          
  | 3 |      2     |   focus     |      
  | 4 |      0     |   maincat2  |   
  | 5 |      4     |   subcat    |   
  | 6 |      1     |   fiesta    |  

records with id 0 are are main in list records that have parent_id other than 0 are submenus etc

Can anyone give me an insight in what my approach can be, or to point me to some example or answer here that I may have overlooked because I have been researching for two days now, and I haven't found anything.

This is somehow close to what I am looking for to populate example menu

Stankela
  • 1
  • 4
  • Check this post out https://stackoverflow.com/questions/18017869/build-tree-array-from-flat-array-in-javascript to make a "tree array", once you make this tree array it will be easier to create your dropdown component – bjdose Feb 25 '21 at 22:42
  • Thanks, that is what i was looking for – Stankela Feb 26 '21 at 15:10

0 Answers0