Skip to content

Commit

Permalink
added parents method for the tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
Atanas Atanasov committed Apr 30, 2018
1 parent 0bb475a commit f4c4d78
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 2 deletions.
41 changes: 41 additions & 0 deletions dist/combined/js/gijgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10534,6 +10534,23 @@ gj.tree.methods = {
$tree.removeClass().empty();
}
return $tree;
},

pathFinder: function (data, list, id, parents) {
var i, result = false;

for (i = 0; i < list.length; i++) {
if (list[i].id == id) {
result = true;
break;
} else if (gj.tree.methods.pathFinder(data, list[i].children, id, parents)) {
parents.push(list[i].data[data.textField]);
result = true;
break;
}
}

return result;
}
}
/**
Expand Down Expand Up @@ -11131,6 +11148,30 @@ gj.tree.widget = function ($element, jsConfig) {
return methods.getChildren(this, $node, cascade);
};

/**
* Return an array with the names of all parents.
* @method
* @param {String} id - The id of the target node
* @return array
* @example sample <!-- tree -->
* Location: <div id="location" style="display: inline-block;"></div>
* <div id="tree"></div>
* <script>
* var tree = $('#tree').tree({
* dataSource: '/Locations/Get',
* select: function (e, node, id) {
* var parents = tree.parents(id);
* $('#location').text(parents.join(' / ') + ' / ' + tree.getDataById(id).text);
* }
* });
* </script>
*/
self.parents = function (id) {
var parents = [], data = this.data();
methods.pathFinder(data, data.records, id, parents);
return parents.reverse();
};

/**
* Enable node from the tree.
* @method
Expand Down
2 changes: 1 addition & 1 deletion dist/combined/js/gijgo.min.js

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions dist/modular/js/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,23 @@ gj.tree.methods = {
$tree.removeClass().empty();
}
return $tree;
},

pathFinder: function (data, list, id, parents) {
var i, result = false;

for (i = 0; i < list.length; i++) {
if (list[i].id == id) {
result = true;
break;
} else if (gj.tree.methods.pathFinder(data, list[i].children, id, parents)) {
parents.push(list[i].data[data.textField]);
result = true;
break;
}
}

return result;
}
}
/** */gj.tree.widget = function ($element, jsConfig) {
Expand Down Expand Up @@ -796,6 +813,13 @@ gj.tree.methods = {
return methods.getChildren(this, $node, cascade);
};

/**
* Return an array with the names of all parents. */ self.parents = function (id) {
var parents = [], data = this.data();
methods.pathFinder(data, data.records, id, parents);
return parents.reverse();
};

/**
* Enable node from the tree. */ self.enable = function ($node, cascade) {
return methods.enableNode(this, $node, cascade);
Expand Down
2 changes: 1 addition & 1 deletion dist/modular/js/tree.min.js

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions examples/tree/Tree.Base.parents.sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example</title>
<script src="../../dist/libraries/jquery/jquery.js"></script>
<script src="../../dist/modular/js/core.js" type="text/javascript"></script>
<link href="../../dist/modular/css/core.css" rel="stylesheet" type="text/css">
<link href="../../dist/modular/css/tree.css" rel="stylesheet" type="text/css">
<script src="../../dist/modular/js/tree.js"></script>
</head>
<body style="padding: 8px;">
Location: <div id="location" style="display: inline-block;"></div>
<div id="tree"></div>
<script>
var tree = $('#tree').tree({
dataSource: '/Locations/Get',
select: function (e, node, id) {
var parents = tree.parents(id);
$('#location').text(parents.join(' / ') + ' / ' + tree.getDataById(id).text);
}
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions examples/tree/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<li><a href="Tree.Base.getSelections.Sample.html">Tree.Base.getSelections.Sample</a></li>
<li><a href="Tree.Base.getChildren.Cascade.True.html">Tree.Base.getChildren.Cascade.True</a></li>
<li><a href="Tree.Base.getChildren.Cascade.False.html">Tree.Base.getChildren.Cascade.False</a></li>
<li><a href="Tree.Base.parents.sample.html">Tree.Base.parents.sample</a></li>
<li><a href="Tree.Base.enable.Material.Design.html">Tree.Base.enable.Material.Design</a></li>
<li><a href="Tree.Base.enable.Bootstrap.html">Tree.Base.enable.Bootstrap</a></li>
<li><a href="Tree.Base.enable.Bootstrap.4.html">Tree.Base.enable.Bootstrap.4</a></li>
Expand Down
17 changes: 17 additions & 0 deletions src/tree/js/tree.base.methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,5 +507,22 @@ gj.tree.methods = {
$tree.removeClass().empty();
}
return $tree;
},

pathFinder: function (data, list, id, parents) {
var i, result = false;

for (i = 0; i < list.length; i++) {
if (list[i].id == id) {
result = true;
break;
} else if (gj.tree.methods.pathFinder(data, list[i].children, id, parents)) {
parents.push(list[i].data[data.textField]);
result = true;
break;
}
}

return result;
}
}
24 changes: 24 additions & 0 deletions src/tree/js/tree.base.widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,30 @@ gj.tree.widget = function ($element, jsConfig) {
return methods.getChildren(this, $node, cascade);
};

/**
* Return an array with the names of all parents.
* @method
* @param {String} id - The id of the target node
* @return array
* @example sample <!-- tree -->
* Location: <div id="location" style="display: inline-block;"></div>
* <div id="tree"></div>
* <script>
* var tree = $('#tree').tree({
* dataSource: '/Locations/Get',
* select: function (e, node, id) {
* var parents = tree.parents(id);
* $('#location').text(parents.join(' / ') + ' / ' + tree.getDataById(id).text);
* }
* });
* </script>
*/
self.parents = function (id) {
var parents = [], data = this.data();
methods.pathFinder(data, data.records, id, parents);
return parents.reverse();
};

/**
* Enable node from the tree.
* @method
Expand Down

0 comments on commit f4c4d78

Please sign in to comment.