You need to sign in to do that
Don't have an account?
Kenneth Kimbrell
Looking to determine the best way to make a nested table with a wrapper class
My goal is to query a list of Employee Hours. I need to order by both the department the Employee works for and the Work Date. Which is the ending date for the work week. I am trying to count hours for all employees that worked within a department and during the same week period. So In other words I will have a header of each department, and under that header will be a secondary header that seperates each work date. Nested within each secondary header would be each employee that worked during that work date.
I will attach an example of what I have so far and what I am trying to accomplish.
Here is my backend logic. I can figure out the front-end for collapsing the rows. I just want to know what are some best practices people use for grouping and counting rows based on specific groupings. e.g. grouped by department and dates, then getting totals of all hours within that specific grouped date.
Any help would be appreciated thanks.
I will attach an example of what I have so far and what I am trying to accomplish.
Here is my backend logic. I can figure out the front-end for collapsing the rows. I just want to know what are some best practices people use for grouping and counting rows based on specific groupings. e.g. grouped by department and dates, then getting totals of all hours within that specific grouped date.
public class CLS_managerReportOfEmployeeHours { public string sDate {get;set;} public string eDate {get;set;} public string bannerMessage {get;set;} public list<string> primary {get;set;} public list<string> unselected {get;set;} public list<string> selected {get;set;} public list<Hours__c> hlist {get;set;} public list<wrapper> wrap {get;set;} public boolean validError {get;set;} public boolean noResults {get;set;} public class wrapper{ public date startOfWeek {get;set;} public date dateWorked {get;set;} public decimal onShoreHours {get;set;} public decimal offShoreHours {get;set;} public string department {get;set;} public string empName {get;set;} public wrapper(date startOfWeek,date dateWorked,decimal onShoreHours,decimal offShoreHours,string department,string empName){ this.startOfWeek = startOfWeek; this.dateWorked = dateWorked; this.onShoreHours = onShoreHours; this.offShoreHours = offShoreHours; this.department = department; this.empName = empName; } } //constructor public CLS_managerReportOfEmployeeHours() { validError = false; unselected = new list<string>(getEmployeeDepartmentList()); selected = new list<string>(); primary = new list<string>(getEmployeeDepartmentList()); noResults = true; } public pageReference updateFormValues(){ getdata(); return null; } public void getData(){ date sd = date.parse(sDate).toStartOfWeek(); date ed = date.parse(eDate); hList = new list<Hours__c>([SELECT Date_worked__c,Employee__r.Offshore__c,Employee__r.Name,Employee__r.Department__c,Employee_Name__c,hours__c,Minutes__c FROM Hours__c WHERE Date_worked__c >= :sd AND Date_worked__c <= :ed AND Employee__r.Department__c IN :selected ORDER BY Employee__r.Department__c,Date_worked__c]); wrap = new list<wrapper>(); string tempDepartment; date tempWorkDate; date tempStart = sd; decimal offshore = 0; decimal onshore = 0; for(Hours__c h: hList){ //count hours of both offshore and onshore if(Employee__r.Offshore__c){ offshore += offshore; }else{ onshore += onshore; } //initiate a new wrapper for each department as a primary header if( (h.Employee__r.Department__c != null && ((tempDepartment != null && tempDepartment != h.Employee__r.Department__c) || ( tempDepartment == null)) ) ){ tempDepartment = h.Employee__r.Department__c; wrap.add(new wrapper(null,null,null,null,tempDepartment,'')); } //initiate a new wrapper for each new week period, total employee hours for each week period for each department if( (h.Date_worked__c != null && ((tempWorkDate != null && tempWorkDate != h.Date_worked__c) || ( tempWorkDate == null)) ) || (h.Employee__r.Department__c != null && ((tempDepartment != null && tempDepartment != h.Employee__r.Department__c) || ( tempDepartment == null)) ) ){ system.debug('isfiring'); tempWorkDate = h.Date_worked__c; wrap.add(new wrapper(h.Date_worked__c.toStartOfWeek(),h.Date_worked__c,onshore,offshore,'total','')); } //initiate a new wrapper for each record and give the ability to open and collapse all records under a given total week period if(h.Employee__r.Offshore__c){ wrap.add(new wrapper(h.Date_worked__c.toStartOfWeek(),h.Date_worked__c,0,h.hours__c,'collapse',h.Employee__r.Name)); }else{ wrap.add(new wrapper(h.Date_worked__c.toStartOfWeek(),h.Date_worked__c,h.hours__c,0,'collapse',h.Employee__r.Name)); } } if(wrap.size() <= 0){ noResults = true; }else{ noResults = false; } system.debug('wrap: ' + wrap); system.debug('hList: ' + hList); } }
Any help would be appreciated thanks.
All Answers
to me all this would imply is a way to potentially make the first level header which is the employee department name. I have already achieved this by ordering the list of Hours__c by Department. That way the Employee__r.department is already being queried in order. So I then use conditions to see when the department name changes. once it changes I then instantiate a new wrapper that identifies the name of the header and I use front-end logic to hide or show a row based on the value of the department name. The result is having the hours records grouped by department and the ability for the user to see that on the front end. Problem is that is where my logic stops. The ability to keep grouped dates with the grouped departments only works to a small extent. When I add more departments to the filter it no longer works as expected.
Can you let me know why creating an inner range and nesting my wrapper class into another wrapper class will help me achieve my desired functionality?
Thanks for your help.
I got it to work a different way. Here is the result if someone is interested in nesting tables differently.