Skip to content

Instantly share code, notes, and snippets.

@jrweth
Created October 29, 2014 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrweth/f5a1168d0ddd76470a65 to your computer and use it in GitHub Desktop.
Save jrweth/f5a1168d0ddd76470a65 to your computer and use it in GitHub Desktop.
Script to save the state of an Oracle APEX tree
/*!
* apex-tree-state saver. The way to spruce up a Checkbox Item List into a Tree format in Oracle APEX.
*
* Copyright (c) 2014 J. Reuben Wetherbee
* http://jrweth.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*
* Launch : October 2014
* Version : 1.0.0
* Released: 29th October, 2014
*
* Description:
* This javascript library is used in conjunction with Oracle Aplication Express (Apex 4.2)
* to save the state of a apex tree region to a hidden item
*
* Usage:
* - Include this code in your page by editing the page and including it in the
* "Javascript Function and Global Variable Declaration" or in the "Javascript File URLs
* - Create a hidden item (e.g. P100_TREE_STATE)
* - Create a new "Tree" region
* - Assign a static_id in the Tree Region "Attributes" section (e.g. MY_TREE)
* - Link the Tree and Item by adding the following to the page javascript "Execute when Page Loads"
* or a dynamic action set to run when the page loads:
* apexTreeStateSaver.init('[TREE_STATIC_ID]','[ITEM_ID]')
* (e.g.) apexTreeStateSaver.init('MY_TREE', 'P100_TREE_STATE');
*/
(function (apexTreeStateSaver, $) {
"use strict";
apexTreeStateSaver.init = function (treeDomId, itemDomId) {
//set up variables for the $Tree Div Container
var $tree = $("#" + treeDomId + " div.tree"),
//set up variable for the $item to save state
$item = $("#" + itemDomId),
//get the Branch Ids that are saved in Item
openBranchIds = $item.val().split(','),
//function to set value of Item to open branches
treeChangedFunc = function () {
var branchIds = [];
$tree.find('li.open').each(function () {
branchIds.push($(this).attr('id'));
});
$item.val(branchIds.join());
};
//initialize the tree branches based upon the open branch ids saved in Item
$tree.find('li').each(function () {
var $this = $(this);
if (openBranchIds.indexOf($this.attr('id')) >= 0) {
$.tree.reference($tree).open_branch($this);
} else {
$.tree.reference($tree).close_branch($this);
}
});
//set up trigger so that new
$.tree.reference($tree).settings.callback.onopen = treeChangedFunc;
$.tree.reference($tree).settings.callback.onclose = treeChangedFunc;
};
})(this.apexTreeStateSaver = this.apexTreeStateSaver || {}, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment