I have an array of objects in json like this:
[{
"vehicleid": 3,
"name": "Teste2VDD",
"brand": "Scania",
"model": "6x2",
"class": "class1",
"region": "Curitiba",
"customer": "Cliente A",
"customerid": 1
},
{
"vehicleid": 1,
"name": "Teste1",
"brand": "Volkswagen",
"model": "5x3",
"class": "class1",
"region": "Curitiba",
"customer": "Cliente A",
"customerid": 1
},
{
"vehicleid": 2,
"name": "Teste3VDD",
"brand": "Volkswagen",
"model": "6x2",
"class": "class2",
"region": "Curitiba",
"customer": "Cliente A",
"customerid": 1
},
{
"vehicleid": 5,
"name": "Teste4VDD",
"brand": "Scania",
"model": "6x4",
"class": "class2",
"region": "Curitiba",
"customer": "Novo",
"customerid": 2
},
{
"vehicleid": 6,
"name": "Teste5VDD",
"brand": "Scania",
"model": "5x5",
"class": "class2",
"region": "Curitiba",
"customer": "Novo",
"customerid": 2
},
{
"vehicleid": 7,
"name": "veiculo",
"brand": "Scania",
"model": "5x4",
"class": "class1",
"region": "Porto Alegre",
"customer": "Cliente3",
"customerid": 3
},
{
"vehicleid": 8,
"name": "veiculo65",
"brand": "Volkswagen",
"model": "5x4",
"class": "class3",
"region": "Porto Alegre",
"customer": "Cliente3",
"customerid": 3
},
{
"vehicleid": 11,
"name": "Veiculo de teste h",
"brand": "Scania",
"model": "5x3",
"class": "class3",
"region": "Belo Horizonte",
"customer": "Cliente de teste h",
"customerid": 10
},
{
"vehicleid": 13,
"name": "Nome 3",
"brand": "Volkswagen",
"model": "6x3",
"class": "class3",
"region": "Belo Horizonte",
"customer": "Cliente de teste h",
"customerid": 10
},
{
"vehicleid": 12,
"name": "Nome",
"brand": "Volvo",
"model": "6x3",
"class": "class3",
"region": "Belo Horizonte",
"customer": "Cliente de teste h",
"customerid": 10
}]
And I need to create an tree hierarquia depending on which filters I pass as parameter.
The first level will always be the customer and the last level will always be the vehicle.
For example:
1 - If I select to filter the tree by Customer, the tree will be ordered by Customer, then the children of the Customer will be the Vehicles, like this:
[{
"attr": {
"type": "customer"
},
"text": "Cliente A",
"children": [
{
"id": 1,
"text": "Teste1",
"attr": {
"type": "vehicle",
"title": "Teste1"
}
},
{
"id": 2,
"text": "Teste3VDD",
"attr": {
"type": "vehicle",
"title": "Teste3VDD"
}
},
{
"id": 3,
"text": "Teste2VDD",
"attr": {
"type": "vehicle",
"title": "Teste2VDD"
}
}
]
},
{
"attr": {
"type": "customer"
},
"text": "Novo",
"children": [
{
"id": 5,
"text": "Teste4VDD",
"attr": {
"type": "vehicle",
"title": "Teste4VDD"
}
},
{
"id": 6,
"text": "Teste5VDD",
"attr": {
"type": "vehicle",
"title": "Teste5VDD"
}
}
]
},
{
"attr": {
"type": "customer"
},
"text": "Cliente3",
"children": [
{
"id": 7,
"text": "veiculo",
"attr": {
"type": "vehicle",
"title": "veiculo"
}
},
{
"id": 8,
"text": "veiculo65",
"attr": {
"type": "vehicle",
"title": "veiculo65"
}
}
]
},
{
"attr": {
"type": "customer"
},
"text": "Cliente",
"children": [
{
"id": 11,
"text": "Veiculo de teste h",
"attr": {
"type": "vehicle",
"title": "Veiculo de teste h"
}
},
{
"id": 12,
"text": "Nome",
"attr": {
"type": "vehicle",
"title": "Nome"
}
},
{
"id": 13,
"text": "Nome 3",
"attr": {
"type": "vehicle",
"title": "Nome 3"
}
}
]
}]
2 - If I select to filter the tree by Brand, the tree will be ordered by Customer, then the children of the Customer will be the Brand, then the children of the Brand wil be Vehicles
3 - If I select to filter the tree by Brand and Model, the tree will be ordered by Customer, then the children of the Customer will be the Brand, then the children of the Brand will be Model then the children of the Model will be Vehicles
4 - If I select to filter the tree by Brand, Model and "Region", the tree will be ordered by Customer, then the children of the Customer will be the Brand, then the children of the Brand will be Model then the children of the Model will be Region, then the children of the Region will be Vehicles
And so on...
Anyone knows something to make this?
Thanks in advance!