Skip to content

Instantly share code, notes, and snippets.

@vegarringdal
Last active October 28, 2016 19:46
Show Gist options
  • Select an option

  • Save vegarringdal/7c6d32f75b81cd724ab207d3cd377d02 to your computer and use it in GitHub Desktop.

Select an option

Save vegarringdal/7c6d32f75b81cd724ab207d3cd377d02 to your computer and use it in GitHub Desktop.
new grid grouping
export class ArrayGrouping {
constructor() {
this.gID = 0;
}
//@params grouping : ["attribute", "attribute2" etc etc ])
group(arrayToGroup, grouping) {
//temp holder for groups as we create them
let groups = [];
grouping.forEach((groupBy, groupNo) => {
if (groupNo === 0) {
//create main group and add to groups array
let mainGroup = this.groupMain(arrayToGroup, groupBy, groupNo)
groups.push(mainGroup);
} else {
//get last group created, and group children
let childGroupArray = groups[groups.length - 1]
let newSubGroup = this.groupChildren(childGroupArray, groupBy, groupNo)
groups.push(newSubGroup)
}
});
this.groups = groups;
this.grouping = grouping;
//return main group
return groups[0];
}
groupMain(array, groupBy, groupNo) {
let tempGroupArray = [];
let curGroup = {};
let tempValue = null;
//first level, here we use array
array.forEach((element, i) => {
this.gID++;
if (element[groupBy] !== tempValue) {
curGroup = {
__groupName: element[groupBy],
__group: true,
__groupID: this.gID,
__groupLvl: groupNo,
__groupChildren: [element],
__groupExpanded: false
};
tempValue = element[groupBy];
tempGroupArray.push(curGroup);
} else {
element.__groupLvl = groupNo;
curGroup.__groupChildren.push(element)
}
})
return tempGroupArray;
}
groupChildren(childGroupArray, groupBy, groupNo) {
let tempGroupArray = []
let curGroup = {};
//loop groups
childGroupArray.forEach((element, i) => {
let tempValue = null;
//loop children
let rebuiltChildrenArray = []
element.__groupChildren.forEach((child) => {
this.gID++;
if (child[groupBy] !== tempValue) {
curGroup = {
__groupName: child[groupBy],
__groupID: this.gID,
__group: true,
__groupLvl: groupNo,
__groupChildren: [child],
__groupExpanded: false
};
child.__groupLvl = groupNo + 1;
tempValue = child[groupBy];
rebuiltChildrenArray.push(curGroup);
tempGroupArray.push(curGroup)
} else {
child.__groupLvl = groupNo + 1;
curGroup.__groupChildren.push(child)
}
})
//replace children with new groups
element.__groupChildren = rebuiltChildrenArray;
})
return tempGroupArray;
}
getGrouping() {
return this.grouping;
}
expand(id) {
let all = id ? false : true; //if no id, then all
let subGroup
let collection = [];
let mainGroups = this.groups[0];
//lopp children
subGroup = (g) => {
g.__groupChildren.forEach((sg) => {
collection.push(sg)
switch (true) {
case all:
case sg.__groupID === id:
case sg.__groupID !== id && sg.__groupExpanded:
if (sg.__groupChildren) {
sg.__groupExpanded = true;
subGroup(sg)
}
break;
}
})
}
//loop main groups
mainGroups.forEach((g) => {
collection.push(g)
switch (true) {
case all:
case g.__groupID === id:
case g.__groupID !== id && g.__groupExpanded:
g.__groupExpanded = true;
if (g.__groupChildren) {
subGroup(g)
}
break;
}
})
return collection;
}
collapse(id) {
let all = id ? false : true; //if no id, then all
id = id === undefined ? null : id;
let subGroup
let collection = [];
let mainGroups = this.groups[0];
//lopp children
subGroup = (g) => {
g.__groupChildren.forEach((sg) => {
switch (true) {
case all:
if (sg.__groupChildren) {
sg.__groupExpanded = false;
subGroup(sg)
}
break;
case sg.__groupID === id:
collection.push(sg)
sg.__groupExpanded = false;
break;
default:
collection.push(sg)
if (sg.__groupChildren && sg.__groupExpanded) {
subGroup(sg)
}
break;
}
})
}
//loop main groups
mainGroups.forEach((g) => {
collection.push(g)
switch (true) {
case all:
g.__groupExpanded = false;
if (g.__groupChildren) {
subGroup(g)
}
break;
case g.__groupID === id:
g.__groupExpanded = false;
break;
default:
if (g.__groupChildren && g.__groupExpanded) {
subGroup(g)
}
break;
}
})
return collection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment