Skip to content

Instantly share code, notes, and snippets.

@cmcculloh-kr
Created June 23, 2017 12:05
Show Gist options
  • Save cmcculloh-kr/5f4673e513c3bcdcab3da989e1318fba to your computer and use it in GitHub Desktop.
Save cmcculloh-kr/5f4673e513c3bcdcab3da989e1318fba to your computer and use it in GitHub Desktop.
static data consuming tree
var findChildData = function findChildData (targetParent, rootData) {
if (_.isEmpty(targetParent)) {
return rootData;
}
if (rootData === undefined) {
return false;
}
for (var i = 0; i < rootData.length; i++) {
var potentialMatch = rootData[i];
if (potentialMatch.attr && targetParent.attr && potentialMatch.attr.id === targetParent.attr.id) {
return potentialMatch.children;
} else if (potentialMatch.children) {
var foundChild = findChildData(targetParent, potentialMatch.children);
if (foundChild) {
return foundChild;
}
}
}
return false;
};
var staticDataSourceConsumer = function staticDataSourceConsumer (openedParentData, callback) {
var childData = findChildData(openedParentData, staticData);
callback({
data: childData
});
};
$('#myTree').tree({
dataSource: staticDataSourceConsumer
});
@cmcculloh-kr
Copy link
Author

This tree datasource will consume static JSON for use in a tree, such as this:

			var staticData = [
				{
					name: 'Empty Folder',
					type: 'folder',
					attr: {
						id: 'emptyFolder'
					},
					children: []
				},
				{
					name: 'Full Folder',
					type: 'folder',
					attr: {
						id: 'fullFolder'
					},
					children: [
						{
							name: 'Full Folder 2',
							type: 'folder',
							attr: {
								id: 'emptyFolder2'
							},
							children: [
								{
									name: 'Folder Sibling 3',
									type: 'item',
									attr: {
										id: 'sibling3',
										'data-icon': 'glyphicon glyphicon-file'
									}
								},
								{
									name: 'Folder Sibling 4',
									type: 'item',
									attr: {
										id: 'sibling4',
										'data-icon': 'glyphicon glyphicon-file'
									}
								}
							]
						},
						{
							name: 'Folder Sibling 2',
							type: 'item',
							attr: {
								id: 'sibling2',
								'data-icon': 'glyphicon glyphicon-file'
							}
						}
					]
				},
				{
					name: 'Folder Sibling',
					type: 'item',
					attr: {
						id: 'sibling1',
						'data-icon': 'glyphicon glyphicon-file'
					}
				}
			];

@futuremint
Copy link

So much indentations! 😲

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment