SPTreeView has the functions ExpandAll() and CollapseAll(). Usually one would insert image buttons with onclick event handlers that calls the 2 functions above. The downside of this is that the image buttons will cause a postback, and the entire page will reload. This is not an ideal situation, especially if the tree view is a web part within the SharePoint page.
Here's how we can achieve the same objectives without a postback (using Javascript):
JS Functions to handle Expand/Collapse of nodes
function TreeView_ExpandCollapseAll(data, id, lineType, flag) { if (!data) { return; } $("[id='" + id + "'] a[id^='" + id + "n']").each(function() { nodeID = $(this).attr('id'); }); } if (!data) { return; } var img = node.childNodes[0]; try { if (flag == 1) { children.style.display = "block"; if ((typeof (img) != "undefined") && (img != null)) { if (lineType == "l") { img.src = data.images[15]; } img.alt = data.collapseToolTip.replace(/\{0\}/, TreeView_GetNodeText(node)); } } children.style.display = "none"; if (lineType == "l") { img.src = data.images[14]; } } } } catch (e) { } data.expandState.value = data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1); } |
The function TreeView_ToggleNode exists in a default JS file that is auto-embedded when you use the SPTreeView class (WebResource.axd?...). We are modifying it to take in an additional flag parameter to control whether to expand or collapse the nodes.
Include reference to JS file from WebPart code
Literal ltlJs = new |
Call Javascript function from WebPart controls
HtmlImage TreeExpandAll = new
HtmlImage TreeCollapseAll = new
|
Note that the object treeViewSPW must be added to this.Controls before treeViewSPW.ClientID can be used.
No comments:
Post a Comment