0 votes
3.2k views
in Coach Views by (120 points)
Hi,

I want to create a view with a tree, which can facilitate my access to data by level and sublevel.

I need your help please, if there is a piece of code or a toolkit and thank you in advance.

Best regards

1 Answer

0 votes
by (30.6k points)

There is no OOB tree control in the UI Toolkit, so a tree is a small custom coach view. Two quick routes:

1. Custom coach view with a recursive template - bind a list of nodes { label, children[] } and render nested <ul>; a click toggles the child list. About 40 lines, no library:

// coach view "Tree": binding = list of Node {label, id, children (list of Node)}
// load handler
var root = this.context.element, me = this;
function render(nodes, parent) {
  var ul = document.createElement("ul"); parent.appendChild(ul);
  nodes.forEach(function (n) {
    var li = document.createElement("li"), span = document.createElement("span");
    span.textContent = (n.children && n.children.length ? "▸ " : "• ") + n.label; li.appendChild(span);
    span.onclick = function () { me.context.trigger(function(){}, { id: n.id }); var c = li.querySelector("ul"); if (c) c.style.display = c.style.display == "none" ? "" : "none"; };
    if (n.children && n.children.length) render(n.children, li);
    ul.appendChild(li);
  });
}
render(this.context.binding.get("value").items || this.context.binding.get("value"), root);

2. Library-based: add bootstrap-treeview (Bootstrap 3 is already on the coach page) or jstree as managed web assets and feed them the same node list; you get search, checkboxes and lazy loading for free. Fetch levels on demand with an Ajax service (a service flow called from the view) when the data comes from a database.

On the data side, flat SQL results (parent id / child id) are turned into the nested list in a server script before the coach:

var byId = {}; tw.local.rows.forEach(function (r) { byId[r.id] = { id: r.id, label: r.name, children: [] }; });
var roots = []; tw.local.rows.forEach(function (r) { (r.parentId && byId[r.parentId] ? byId[r.parentId].children : roots).push(byId[r.id]); });
tw.local.tree = roots;

References

Related questions

0 votes
1 answer 4.0k views
0 votes
1 answer 1.7k views
0 votes
1 answer 3.4k views
0 votes
1 answer 1.2k views
0 votes
1 answer 3.1k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...