Note: Please check this blog post for more details on these functions.
Sort a SQL query with id
and parentid
so that
the rows have the correct order of the tree.
Parameters:
q
(Array): A query result (see example below)id
(String): The name of the id column (Default: "id")parentid
(String): The name of the ParentItemID column (Default: "parentid")
Example:
SQL: SELECT * FROM Animals ORDER BY name
_queryTreeSort({q: [
{"id": 456, "parentid": 123, "name": "Dogs"},
{"id": 214, "parentid": 456, "name": "Labradors"},
{"id": 123, "parentid": 0, "name": "Mammals"},
{"id": 810, "parentid": 456, "name": "Pugs"},
{"id": 919, "parentid": 456, "name": "Terriers"}
]});
Result:
[
{"id": 123, "parentid": 0, "name": "Mammals"},
{"id": 456, "parentid": 123, "name": "Dogs"},
{"id": 214, "parentid": 456, "name": "Labradors"},
{"id": 810, "parentid": 456, "name": "Pugs"},
{"id": 919, "parentid": 456, "name": "Terriers"}
]
Use the result to loop over the query or create a tree with _makeTree()
Transform a correctly sorted SQL query with id and parentid
into a tree object. Use _queryTreeSort()
to sort you SQL query.
Parameters:
q
(Array): A query result (see example below)id
(String): The name of the id column (Default: "id")parentid
(String): The name of the ParentItemID column (Default: "parentid")children
(String): The name of the "children" array to be created in rows that have children (Default: "children")
Example:
_makeTree({ q:
[
{"id": 123, "parentid": 0, "name": "Mammals"},
{"id": 456, "parentid": 123, "name": "Dogs"},
{"id": 214, "parentid": 456, "name": "Labradors"},
{"id": 810, "parentid": 456, "name": "Pugs"},
{"id": 919, "parentid": 456, "name": "Terriers"}
]
});
Result:
[
{
"id": 123,
"parentid": 0,
"name": "Mammals",
"children": [
{
"id": 456,
"parentid": 123,
"name": "Dogs",
"children": [
{
"id": 214,
"parentid": 456,
"name": "Labradors"
},
{
"id": 810,
"parentid": 456,
"name": "Pugs"
},
{
"id": 919,
"parentid": 456,
"name": "Terriers"
}
]
}
]
}
]
Use the result to display your tree with a recursive function like _renderTree()
To Speed up the raw Make Tree it's also possible to do the
makeTree
without the presorting.Just use this version of
_makeTree
: