diff --git a/front/core/components/treeview/index.js b/front/core/components/treeview/index.js index f73bf3e38b..a71a3a342b 100644 --- a/front/core/components/treeview/index.js +++ b/front/core/components/treeview/index.js @@ -66,7 +66,14 @@ export default class Treeview extends Component { }); } - item.childs = newData.sort((a, b) => b.isIncluded - a.isIncluded); + item.childs = newData.sort((a, b) => { + let priority = (b.isIncluded - a.isIncluded) - 1; + + if (b.name > a.name) + priority++; + + return priority; + }); }); } diff --git a/modules/agency/back/methods/zone-geo/getLeaves.js b/modules/agency/back/methods/zone-geo/getLeaves.js index 9f819e83a8..6312864655 100644 --- a/modules/agency/back/methods/zone-geo/getLeaves.js +++ b/modules/agency/back/methods/zone-geo/getLeaves.js @@ -36,8 +36,6 @@ module.exports = Self => { let conn = Self.dataSource.connector; let stmts = []; - let geo = await Self.findById(parentFk); - stmts.push(`DROP TEMPORARY TABLE IF EXISTS tmp.checkedChilds`); stmts.push(new ParameterizedSQL( `CREATE TEMPORARY TABLE tmp.checkedChilds ( @@ -77,7 +75,9 @@ module.exports = Self => { zg.sons, IF(ch.id = zg.id, isIncluded, null) isIncluded FROM zoneGeo zg - JOIN tmp.checkedChilds ch ON zg.lft <= ch.lft AND zg.rgt >= ch.rgt + JOIN tmp.checkedChilds ch + ON zg.lft <= ch.lft AND zg.rgt >= ch.rgt + AND zg.depth > 0 UNION ALL SELECT child.id, @@ -105,12 +105,26 @@ module.exports = Self => { const result = await Self.rawStmt(sql); const nodes = result[resultIndex]; + if (nodes.length == 0) + return nodes; + // Get parent nodes + const minorDepth = nodes.reduce((a, b) => { + return b < a ? b : a; + }).depth; + const parentNodes = nodes.filter(element => { - return element.depth === geo.depth + 1; + return element.depth === minorDepth; }); - const sortedLeaves = parentNodes.sort((a, b) => b.isIncluded - a.isIncluded); + const sortedLeaves = parentNodes.sort((a, b) => { + let priority = (b.isIncluded - a.isIncluded) - 1; + + if (b.name > a.name) + priority++; + + return priority; + }); const leaves = Object.assign([], sortedLeaves); nestLeaves(leaves);