0

I am new to node js and javascript, I am trying to get 1 tree view as a result, this is my structure of my table in sql server

SQL query table AUT_MENU

SELECT * FROM AUT_MENU

  IDMENU NIVELPADRE     NIVEL       PADRE           OPCION
1   NULL            01      SISTEMA     Modulos
2   01          01.01       Modulos     Almacen
3   01          01.02       Modulos     Mostrador
4   01          01.03       Modulos     Taller
5   01          01.04       Modulos     Venta Vehiculos
6   01          01.05       Modulos     Tunning
7   01          01.06       Modulos     Conexos
8   01          01.07       Modulos     Contabilidad
9   01          01.08       Modulos     Finanzas
10  01          01.09       Modulos     Costos
11  01          01.10       Modulos     GDH
12  01          01.99       Modulos     Soporte
13  NULL            98      SISTEMA     Utilitarios
14  98          98.01       Utilitarios Seleccionar empresa
15  98          98.02       Utilitarios Elegir periodo
16  98          98.03       Utilitarios Calculadora
17  98          98.98       Utilitarios Tablas
18  98.98           98.98.01    Tablas          Tipo de Cambio
19  98.98           98.98.02    Tablas      Persona
20  98.98           98.98.03    Tablas      Producto
21  98.98           98.98.04    Tablas      Vehiculo
22  98          98.99       Utilitarios Salir del sistema
23  NULL            99      SISTEMA     Perfil
24  99          99.01       Perfil      Seguridad
25  99.01           99.01.01    Seguridad   Cambio de Clave

When consuming this table by node js it shows as follows.

export const Menus = async (req, res) => {
try {
    var idusuario = req.params.id;


    const pool = await getConnection();
    const result = await pool.request()
        .input("tipo", sql.Int, 1)
        .input("IDUSUARIO", idusuario)
        .execute(queries.obtenermenu)
    console.log(idusuario)
    // res.json('reportes')

    res.json({
        status: 'success',
        data: result.recordset
    })


} catch (error) {
    res.status(500);
    res.send(error.message);
}

}

request output :

{
"status": "success",
"data": [
    {
        "IDMENU": 1,
        "NIVELPADRE": null,
        "NIVEL": "01",
        "PADRE": "SISTEMA",
        "OPCION": "Modulos"
    },
    {
        "IDMENU": 2,
        "NIVELPADRE": "01",
        "NIVEL": "01.01",
        "PADRE": "Modulos",
        "OPCION": "Almacen"
    },
    {
        "IDMENU": 4,
        "NIVELPADRE": "01",
        "NIVEL": "01.03",
        "PADRE": "Modulos",
        "OPCION": "Taller"
    },
    {
        "IDMENU": 13,
        "NIVELPADRE": null,
        "NIVEL": "98",
        "PADRE": "SISTEMA",
        "OPCION": "Utilitarios"
    },
    {
        "IDMENU": 14,
        "NIVELPADRE": "98",
        "NIVEL": "98.01",
        "PADRE": "Utilitarios",
        "OPCION": "Seleccionar empresa"
    },
    {
        "IDMENU": 15,
        "NIVELPADRE": "98",
        "NIVEL": "98.02",
        "PADRE": "Utilitarios",
        "OPCION": "Elegir periodo"
    },
    {
        "IDMENU": 16,
        "NIVELPADRE": "98",
        "NIVEL": "98.03",
        "PADRE": "Utilitarios",
        "OPCION": "Calculadora"
    },
    {
        "IDMENU": 17,
        "NIVELPADRE": "98",
        "NIVEL": "98.98",
        "PADRE": "Utilitarios",
        "OPCION": "Tablas"
    },
    {
        "IDMENU": 18,
        "NIVELPADRE": "98.98",
        "NIVEL": "98.98.01",
        "PADRE": "Tablas",
        "OPCION": "Tipo de Cambio"
    },
    {
        "IDMENU": 19,
        "NIVELPADRE": "98.98",
        "NIVEL": "98.98.02",
        "PADRE": "Tablas",
        "OPCION": "Persona"
    },
    {
        "IDMENU": 20,
        "NIVELPADRE": "98.98",
        "NIVEL": "98.98.03",
        "PADRE": "Tablas",
        "OPCION": "Producto"
    },
    {
        "IDMENU": 21,
        "NIVELPADRE": "98.98",
        "NIVEL": "98.98.04",
        "PADRE": "Tablas",
        "OPCION": "Vehiculo"
    },
    {
        "IDMENU": 22,
        "NIVELPADRE": "98",
        "NIVEL": "98.99",
        "PADRE": "Utilitarios",
        "OPCION": "Salir del sistema"
    }

] }

But I still can't show the output of the request in tree type, which is desired in the following way, is there a method or a suggestion on how it could work, thank you very much.

What you want to get from the request to create this type of tree through an object in node js

enter image description here

kenny
  • 173
  • 9
  • Please don't post input/desired output as an image. Type the text. – trincot Apr 21 '23 at 17:28
  • 1
    we can't help you if you don't post the query part of your code. Depending on how you're selecting and combining rows, we can help you to process the result returned from database and then process the data e returning a tree like object so you can create your menu with submenus. – Jone Polvora Apr 21 '23 at 17:32
  • The code that brings up the AUT_MENU table is just a select * from Menu – kenny Apr 21 '23 at 17:49

0 Answers0