Needing help with IF or IFS formula...

%3CLINGO-SUB%20id%3D%22lingo-sub-265945%22%20slang%3D%22en-US%22%3ENeeding%20help%20with%20IF%20or%20IFS%20formula...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-265945%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20JS%20file%20that%20accesses%20a%20termset%20in%20the%20taxonomy%20store.%26nbsp%3B%20Right%20now%2C%20I%20can%20get%20it%20to%20drill%20down%20to%20the%20first%20set%20of%20terms%20and%20the%20children%20but%20I%20have%20the%20root%20(called%20Global%20Nav)%2C%20a%20termset%20(Corporate)%2C%20a%20child%20termset%20(ITS)%2C%20a%20further%20child%20(grandchild%3F%20called%20EPIC)%2C%20and%20a%20set%20of%26nbsp%3Bterms%20under%20that%20that%20do%20not%20contain%20children.%26nbsp%3B%20What%20I%20want%20to%20do%20is%20to%20iterate%20through%20each%20level%20and%20for%20each%20level%20then%20iterate%20through%20the%20next%20and%20the%20next%20and%20so%20on%20so%20that%20I%20can%20have%20a%20drill%20down%20menu%20of%20sorts.%26nbsp%3B%20The%20Javascript%20is%20below%20and%20I%20have%20attached%20an%20image%20of%20a%26nbsp%3Bdiagram%20of%20the%20drill%20down%20menu%20structure%20I%20am%20trying%20to%20accomplish.%26nbsp%3B%20%26nbsp%3BI%20know%20it%20is%20going%20to%20end%20up%20being%20nested%20loops%20but%20I%20just%20cannot%20get%20to%20the%20next%20level.%26nbsp%3B%20I'm%20sure%20if%20someone%20can%20guide%20to%20add%20a%20third%20level%2C%20I%20can%20add%20the%20rest%20myself.%26nbsp%3B%20Thanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFunction.registerNamespace('ManagedGlobalNavigation')%3B%3C%2FP%3E%3CP%3EManagedGlobalNavigation.MenuItem%20%3D%20function%20(title%2C%20url%2C%20icon%2C%20tooltip%2C%20children)%20%7B%3CBR%20%2F%3Ethis.title%20%3D%20title%3B%3CBR%20%2F%3Ethis.url%20%3D%20url%3B%3CBR%20%2F%3Ethis.icon%20%3D%20icon%3B%3CBR%20%2F%3Ethis.tooltip%20%3D%20tooltip%3B%3CBR%20%2F%3Ethis.children%20%3D%20children%3B%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3EManagedGlobalNavigation.viewModel%20%3D%20%7B%3CBR%20%2F%3EglobalMenuItems%3A%20new%20Array()%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3EManagedGlobalNavigation.init%20%3D%20function%20(tree)%20%7B%3CBR%20%2F%3Efor%20(i%20%3D%200%3B%20i%20%26lt%3B%20tree.children.length%3B%20i%2B%2B)%20%7B%3CBR%20%2F%3EManagedGlobalNavigation.viewModel.globalMenuItems.push(new%20ManagedGlobalNavigation.MenuItem(tree.children%5Bi%5D.title%2C%20tree.children%5Bi%5D.url%2C%20tree.children%5Bi%5D.icon%2C%20tree.children%5Bi%5D.tooltip%2C%20tree.children%5Bi%5D.children))%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Eko.applyBindings(ManagedGlobalNavigation.viewModel)%3B%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%2F*!%3CBR%20%2F%3E*%20Termset%20utilities%3CBR%20%2F%3E*%2F%3C%2FP%3E%3CP%3E%2F%2Fvar%20Custom%20%3D%20Custom%20%7C%7C%20%7B%7D%3B%3CBR%20%2F%3E%2F%2FCustom.MMN%20%3D%20Custom.MMN%20%7C%7C%20%7B%7D%3B%3CBR%20%2F%3E%2F%2FCustom.MMN.Termset%20%3D%20Custom.MMN.Termset%20%7C%7C%20%7B%7D%3B%3C%2FP%3E%3CP%3Evar%20MMN%20%3D%20MMN%20%7C%7C%20%7B%7D%3B%3CBR%20%2F%3EMMN.Termset%20%3D%20MMN.Termset%20%7C%7C%20%7B%7D%3B%3C%2FP%3E%3CP%3E(function%20(module)%20%7B%3CBR%20%2F%3E%2F**%3CBR%20%2F%3E*%20Returns%20a%20termset%2C%20based%20on%20ID%3CBR%20%2F%3E*%3CBR%20%2F%3E*%20%40param%20%7Bstring%7D%20id%20-%20Termset%20ID%3CBR%20%2F%3E*%20%40param%20%7Bobject%7D%20callback%20-%20Callback%20function%20to%20call%20upon%20completion%20and%20pass%20termset%20into%3CBR%20%2F%3E*%2F%3C%2FP%3E%3CP%3Emodule.getTermSet%20%3D%20function%20(id%2C%20callback)%20%7B%3CBR%20%2F%3ESP.SOD.loadMultiple(%5B'sp.js'%5D%2C%20function%20()%20%7B%3CBR%20%2F%3E%2F%2F%20Make%20sure%20taxonomy%20library%20is%20registered%3CBR%20%2F%3ESP.SOD.registerSod('sp.taxonomy.js'%2C%20SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'))%3B%3CBR%20%2F%3ESP.SOD.loadMultiple(%5B'sp.taxonomy.js'%5D%2C%20function%20()%20%7B%3CBR%20%2F%3Evar%20ctx%20%3D%20SP.ClientContext.get_current()%2C%3CBR%20%2F%3EtaxonomySession%20%3D%20SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx)%2C%3CBR%20%2F%3EtermStore%20%3D%20taxonomySession.get_termStores().getByName('Managed%20Metadata%20Web%20Service%20Proxy')%2C%3CBR%20%2F%3EtermSet%20%3D%20termStore.getTermSet('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')%2C%3CBR%20%2F%3Eterms%20%3D%20termSet.getTerm('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')%3B%3CBR%20%2F%3Ectx.load(terms)%3B%3CBR%20%2F%3Ectx.executeQueryAsync(Function.createDelegate(this%2C%20function%20(sender%2C%20args)%20%7B%3CBR%20%2F%3Ecallback(terms)%3B%3CBR%20%2F%3E%7D)%2C%3CBR%20%2F%3EFunction.createDelegate(this%2C%20function%20(sender%2C%20args)%20%7B%20%7D))%3B%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3E%2F**%3CBR%20%2F%3E*%20Returns%20an%20array%20object%20of%20terms%20as%20a%20tree%3CBR%20%2F%3E*%3CBR%20%2F%3E*%20%40param%20%7Bstring%7D%20id%20-%20Termset%20ID%3CBR%20%2F%3E*%20%40param%20%7Bobject%7D%20callback%20-%20Callback%20function%20to%20call%20upon%20completion%20and%20pass%20termset%20into%3CBR%20%2F%3E*%2F%3CBR%20%2F%3Emodule.getTermSetAsTree%20%3D%20function%20(id%2C%20callback)%20%7B%3CBR%20%2F%3Emodule.getTermSet(id%2C%20function%20(terms)%20%7B%3CBR%20%2F%3Evar%20termsEnumerator%20%3D%20terms.getEnumerator()%2C%3CBR%20%2F%3Etree%20%3D%20%7B%3CBR%20%2F%3Eterm%3A%20terms%2C%3CBR%20%2F%3Echildren%3A%20%5B%5D%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3E%2F%2F%20Loop%20through%20each%20term%3CBR%20%2F%3Ewhile%20(termsEnumerator.moveNext())%20%7B%3CBR%20%2F%3Evar%20currentTerm%20%3D%20termsEnumerator.get_current()%3B%3CBR%20%2F%3Evar%20currentTermPath%20%3D%20currentTerm.get_pathOfTerm().split('%3B')%3B%3CBR%20%2F%3Evar%20children%20%3D%20tree.children%3B%3C%2FP%3E%3CP%3E%2F%2F%20Loop%20through%20each%20part%20of%20the%20path%3CBR%20%2F%3Efor%20(var%20i%20%3D%200%3B%20i%20%26lt%3B%20currentTermPath.length%3B%20i%2B%2B)%20%7B%3CBR%20%2F%3Evar%20foundNode%20%3D%20false%3B%3CBR%20%2F%3Efor%20(var%20j%20%3D%200%3B%20j%20%26lt%3B%20children.length%3B%20j%2B%2B)%20%7B%3CBR%20%2F%3Eif%20(children%5Bj%5D.name%20%3D%3D%3D%20currentTermPath%5Bi%5D)%20%7B%3CBR%20%2F%3EfoundNode%20%3D%20true%3B%3CBR%20%2F%3Ebreak%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%2F%2F%20Select%20the%20node%2C%20otherwise%20create%20a%20new%20one%3CBR%20%2F%3Evar%20term%20%3D%20foundNode%20%3F%20children%5Bj%5D%20%3A%20%7B%20name%3A%20currentTermPath%5Bi%5D%2C%20children%3A%20%5B%5D%20%7D%3B%3CBR%20%2F%3E%2F%2F%20If%20we're%20a%20child%20element%2C%20add%20the%20term%20properties%3CBR%20%2F%3Eif%20(i%20%3D%3D%3D%20currentTermPath.length%20-%201)%20%7B%3CBR%20%2F%3Eterm.term%20%3D%20currentTerm%3B%3CBR%20%2F%3Eterm.title%20%3D%20currentTerm.get_name()%3B%3CBR%20%2F%3Eterm.guid%20%3D%20currentTerm.get_id().toString()%3B%3CBR%20%2F%3Eterm.url%20%3D%20currentTerm.get_localCustomProperties()%5B'_Sys_Nav_SimpleLinkUrl'%5D%3B%3CBR%20%2F%3Eterm.icon%20%3D%20currentTerm.get_localCustomProperties()%5B'NavIcon'%5D%3B%3CBR%20%2F%3Eterm.tooltip%20%3D%20currentTerm.get_localCustomProperties()%5B'NavToolTip'%5D%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%2F%2F%20If%20the%20node%20did%20exist%2C%20let's%20look%20there%20next%20iteration%3CBR%20%2F%3Eif%20(foundNode)%20%7B%3CBR%20%2F%3Echildren%20%3D%20term.children%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%2F%2F%20If%20the%20segment%20of%20path%20does%20not%20exist%2C%20create%20it%3CBR%20%2F%3Eelse%20%7B%3CBR%20%2F%3Echildren.push(term)%3B%3CBR%20%2F%3E%2F%2F%20Reset%20the%20children%20pointer%20to%20add%20there%20next%20iteration%3CBR%20%2F%3Eif%20(i%20!%3D%3D%20currentTermPath.length%20-%201)%20%7B%3CBR%20%2F%3Echildren%20%3D%20term.children%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Etree%20%3D%20module.sortTermsFromTree(tree)%3B%3CBR%20%2F%3Ecallback(tree)%3B%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3E%2F**%3CBR%20%2F%3E*%20Sort%20children%20array%20of%20a%20term%20tree%20by%20a%20sort%20order%3CBR%20%2F%3E*%3CBR%20%2F%3E*%20%40param%20%7Bobj%7D%20tree%20The%20term%20tree%3CBR%20%2F%3E*%20%40return%20%7Bobj%7D%20A%20sorted%20term%20tree%3CBR%20%2F%3E*%2F%3C%2FP%3E%3CP%3Emodule.sortTermsFromTree%20%3D%20function%20(tree)%20%7B%3CBR%20%2F%3E%2F%2F%20Check%20to%20see%20if%20the%20get_customSortOrder%20function%20is%20defined.%20If%20the%20term%20is%20actually%20a%20term%20collection%2C%3CBR%20%2F%3E%2F%2F%20there%20is%20nothing%20to%20sort.%3CBR%20%2F%3Eif%20(tree.children.length%20%26amp%3B%26amp%3B%20tree.term.get_customSortOrder)%20%7B%3CBR%20%2F%3Evar%20sortOrder%20%3D%20null%3B%3CBR%20%2F%3Eif%20(tree.term.get_customSortOrder())%20%7B%3CBR%20%2F%3EsortOrder%20%3D%20tree.term.get_customSortOrder()%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%2F%2F%20If%20not%20null%2C%20the%20custom%20sort%20order%20is%20a%20string%20of%20GUIDs%2C%20delimited%20by%20a%20%3A%3CBR%20%2F%3Eif%20(sortOrder)%20%7B%3CBR%20%2F%3EsortOrder%20%3D%20sortOrder.split('%3A')%3B%3CBR%20%2F%3Etree.children.sort(function%20(a%2C%20b)%20%7B%3CBR%20%2F%3Evar%20indexA%20%3D%20sortOrder.indexOf(a.guid)%3B%3CBR%20%2F%3Evar%20indexB%20%3D%20sortOrder.indexOf(b.guid)%3B%3CBR%20%2F%3Eif%20(indexA%20%26gt%3B%20indexB)%20%7B%3CBR%20%2F%3Ereturn%201%3B%3CBR%20%2F%3E%7D%20else%20if%20(indexA%20%26lt%3B%20indexB)%20%7B%3CBR%20%2F%3Ereturn%20-1%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ereturn%200%3B%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%2F%2F%20If%20null%2C%20terms%20are%20just%20sorted%20alphabetically%3CBR%20%2F%3Eelse%20%7B%3CBR%20%2F%3Etree.children.sort(function%20(a%2C%20b)%20%7B%3CBR%20%2F%3Eif%20(a.title%20%26gt%3B%20b.title)%20%7B%3CBR%20%2F%3Ereturn%201%3B%3CBR%20%2F%3E%7D%20else%20if%20(a.title%20%26lt%3B%20b.title)%20%7B%3CBR%20%2F%3Ereturn%20-1%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ereturn%200%3B%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Efor%20(var%20i%20%3D%200%3B%20i%20%26lt%3B%20tree.children.length%3B%20i%2B%2B)%20%7B%3CBR%20%2F%3Etree.children%5Bi%5D%20%3D%20module.sortTermsFromTree(tree.children%5Bi%5D)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ereturn%20tree%3B%3CBR%20%2F%3E%7D%3B%3CBR%20%2F%3E%7D)(MMN.Termset)%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I have a JS file that accesses a termset in the taxonomy store.  Right now, I can get it to drill down to the first set of terms and the children but I have the root (called Global Nav), a termset (Corporate), a child termset (ITS), a further child (grandchild? called EPIC), and a set of terms under that that do not contain children.  What I want to do is to iterate through each level and for each level then iterate through the next and the next and so on so that I can have a drill down menu of sorts.  The Javascript is below and I have attached an image of a diagram of the drill down menu structure I am trying to accomplish.   I know it is going to end up being nested loops but I just cannot get to the next level.  I'm sure if someone can guide to add a third level, I can add the rest myself.  Thanks.

 

 

Function.registerNamespace('ManagedGlobalNavigation');

ManagedGlobalNavigation.MenuItem = function (title, url, icon, tooltip, children) {
this.title = title;
this.url = url;
this.icon = icon;
this.tooltip = tooltip;
this.children = children;
};

ManagedGlobalNavigation.viewModel = {
globalMenuItems: new Array()
};

ManagedGlobalNavigation.init = function (tree) {
for (i = 0; i < tree.children.length; i++) {
ManagedGlobalNavigation.viewModel.globalMenuItems.push(new ManagedGlobalNavigation.MenuItem(tree.children[i].title, tree.children[i].url, tree.children[i].icon, tree.children[i].tooltip, tree.children[i].children));
}

ko.applyBindings(ManagedGlobalNavigation.viewModel);
};


/*!
* Termset utilities
*/

//var Custom = Custom || {};
//Custom.MMN = Custom.MMN || {};
//Custom.MMN.Termset = Custom.MMN.Termset || {};

var MMN = MMN || {};
MMN.Termset = MMN.Termset || {};

(function (module) {
/**
* Returns a termset, based on ID
*
* @param {string} id - Termset ID
* @param {object} callback - Callback function to call upon completion and pass termset into
*/

module.getTermSet = function (id, callback) {
SP.SOD.loadMultiple(['sp.js'], function () {
// Make sure taxonomy library is registered
SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
SP.SOD.loadMultiple(['sp.taxonomy.js'], function () {
var ctx = SP.ClientContext.get_current(),
taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx),
termStore = taxonomySession.get_termStores().getByName('Managed Metadata Web Service Proxy'),
termSet = termStore.getTermSet('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'),
terms = termSet.getTerm('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX');
ctx.load(terms);
ctx.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
callback(terms);
}),
Function.createDelegate(this, function (sender, args) { }));
});
});
};

/**
* Returns an array object of terms as a tree
*
* @param {string} id - Termset ID
* @param {object} callback - Callback function to call upon completion and pass termset into
*/
module.getTermSetAsTree = function (id, callback) {
module.getTermSet(id, function (terms) {
var termsEnumerator = terms.getEnumerator(),
tree = {
term: terms,
children: []
};

// Loop through each term
while (termsEnumerator.moveNext()) {
var currentTerm = termsEnumerator.get_current();
var currentTermPath = currentTerm.get_pathOfTerm().split(';');
var children = tree.children;

// Loop through each part of the path
for (var i = 0; i < currentTermPath.length; i++) {
var foundNode = false;
for (var j = 0; j < children.length; j++) {
if (children[j].name === currentTermPath[i]) {
foundNode = true;
break;
}
}
// Select the node, otherwise create a new one
var term = foundNode ? children[j] : { name: currentTermPath[i], children: [] };
// If we're a child element, add the term properties
if (i === currentTermPath.length - 1) {
term.term = currentTerm;
term.title = currentTerm.get_name();
term.guid = currentTerm.get_id().toString();
term.url = currentTerm.get_localCustomProperties()['_Sys_Nav_SimpleLinkUrl'];
term.icon = currentTerm.get_localCustomProperties()['NavIcon'];
term.tooltip = currentTerm.get_localCustomProperties()['NavToolTip'];
}

// If the node did exist, let's look there next iteration
if (foundNode) {
children = term.children;
}
// If the segment of path does not exist, create it
else {
children.push(term);
// Reset the children pointer to add there next iteration
if (i !== currentTermPath.length - 1) {
children = term.children;
}
}
}
}
tree = module.sortTermsFromTree(tree);
callback(tree);
});
};

/**
* Sort children array of a term tree by a sort order
*
* @param {obj} tree The term tree
* @return {obj} A sorted term tree
*/

module.sortTermsFromTree = function (tree) {
// Check to see if the get_customSortOrder function is defined. If the term is actually a term collection,
// there is nothing to sort.
if (tree.children.length && tree.term.get_customSortOrder) {
var sortOrder = null;
if (tree.term.get_customSortOrder()) {
sortOrder = tree.term.get_customSortOrder();
}
// If not null, the custom sort order is a string of GUIDs, delimited by a :
if (sortOrder) {
sortOrder = sortOrder.split(':');
tree.children.sort(function (a, b) {
var indexA = sortOrder.indexOf(a.guid);
var indexB = sortOrder.indexOf(b.guid);
if (indexA > indexB) {
return 1;
} else if (indexA < indexB) {
return -1;
}
return 0;
});
}
// If null, terms are just sorted alphabetically
else {
tree.children.sort(function (a, b) {
if (a.title > b.title) {
return 1;
} else if (a.title < b.title) {
return -1;
}
return 0;
});
}
}

for (var i = 0; i < tree.children.length; i++) {
tree.children[i] = module.sortTermsFromTree(tree.children[i]);
}
return tree;
};
})(MMN.Termset);

0 Replies