From 0dbbaed1faf4403bd97115c2c4041ab8423fab6e Mon Sep 17 00:00:00 2001 From: Ethan Dunzer Date: Thu, 22 Aug 2024 14:57:40 -0700 Subject: [PATCH 1/3] Add Pagination --- .../IdeaListViewComponentController.cls | 66 +++++---- .../ideaListViewComponent.css | 11 +- .../ideaListViewComponent.html | 45 ++++-- .../ideaListViewComponent.js | 136 +++++++++++++----- 4 files changed, 174 insertions(+), 84 deletions(-) diff --git a/force-app/main/default/classes/IdeaListViewComponentController.cls b/force-app/main/default/classes/IdeaListViewComponentController.cls index 0d602f2..bc7b42e 100644 --- a/force-app/main/default/classes/IdeaListViewComponentController.cls +++ b/force-app/main/default/classes/IdeaListViewComponentController.cls @@ -1,55 +1,53 @@ public with sharing class IdeaListViewComponentController { @AuraEnabled(cacheable=true) - public static List getIdeasWithVotes(String sourceType, String sortField, String sortOrder, String statusFilter, Id recordId) { + public static Map getIdeasWithVotes(String sourceType, String sortField, String sortOrder, String statusFilter, Id recordId, Integer pageSize, Integer pageNumber) { Id currentUserId = UserInfo.getUserId(); - + Integer offset = (pageNumber - 1) * pageSize; + // Base query for ideas String baseQuery = 'SELECT Id, Name, Product_Tag__c, Product_Tag__r.Name, Status__c, Subject__c, Submitted_By__c, Submitted_By__r.Name, Up__c, Down__c FROM Idea__c'; + String countQuery = 'SELECT COUNT() FROM Idea__c'; List ideas; + Integer totalRecords; try { + String whereClause = ''; if (recordId != null) { - // If recordId is provided, fetch that specific Idea - baseQuery += ' WHERE Id = :recordId'; - ideas = [SELECT Id, Name, Product_Tag__c, Product_Tag__r.Name, Status__c, Subject__c, Submitted_By__c, Submitted_By__r.Name, Up__c, Down__c FROM Idea__c WHERE Id = :recordId]; + whereClause = ' WHERE Id = :recordId'; } else if (sourceType == 'CurrentUser') { - baseQuery += ' WHERE Submitted_By__c = :currentUserId'; - ideas = Database.query(baseQuery); + whereClause = ' WHERE Submitted_By__c = :currentUserId'; } else { - baseQuery += ' WHERE Id != NULL'; // Ensures WHERE clause is always valid - ideas = Database.query(baseQuery); + whereClause = ' WHERE Id != NULL'; // Ensures WHERE clause is always valid } // Apply status filter if provided and valid if (statusFilter != null && !String.isBlank(statusFilter)) { - try { - List statusList = statusFilter.split(','); - if (!statusList.isEmpty()) { - baseQuery += ' AND Status__c IN :statusList'; - } - } catch (Exception e) { - System.debug('Error processing statusFilter: ' + e.getMessage()); + List statusList = statusFilter.split(','); + if (!statusList.isEmpty()) { + whereClause += ' AND Status__c IN :statusList'; } } - // Apply sorting based on sortField and sortOrder - baseQuery += ' ORDER BY ' + sortField + ' ' + sortOrder + ' LIMIT 100'; - - // Debugging output to verify final query - System.debug('Final Query: ' + baseQuery); - - // Execute the query - if (recordId == null) { - ideas = Database.query(baseQuery); - } - + // Append the whereClause to both queries + baseQuery += whereClause; + countQuery += whereClause; + + // Apply sorting and pagination + baseQuery += ' ORDER BY ' + sortField + ' ' + sortOrder + ' LIMIT :pageSize OFFSET :offset'; + + // Execute the queries + ideas = Database.query(baseQuery); + + // Execute the count query to get the total number of records + totalRecords = Database.countQuery(countQuery); // Changed from SOQL to countQuery + // Collect idea IDs for further processing Set ideaIds = new Set(); for (Idea__c idea : ideas) { ideaIds.add(idea.Id); } - + // Map to hold votes related to the ideas Map ideaVotesMap = new Map(); if (!ideaIds.isEmpty()) { @@ -60,21 +58,27 @@ public with sharing class IdeaListViewComponentController { ideaVotesMap.put(vote.Idea__c, vote); } } - + // Wrap the Ideas and associate the votes List ideaList = new List(); for (Idea__c idea : ideas) { Idea_Vote__c userVote = ideaVotesMap.get(idea.Id); ideaList.add(new IdeaWrapper(idea, userVote)); } - - return ideaList; + + // Prepare the return map + Map resultMap = new Map(); + resultMap.put('ideas', ideaList); + resultMap.put('totalRecords', totalRecords); + + return resultMap; } catch (Exception e) { // Log the error and throw a handled exception to the client System.debug('Error in getIdeasWithVotes: ' + e.getMessage()); throw new AuraHandledException('Error in getIdeasWithVotes: ' + e.getMessage()); } } + // Method to handle upvote diff --git a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css index 58ffc0c..a5f066b 100644 --- a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css +++ b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css @@ -1,12 +1,14 @@ -/* ideaListViewComponent.css */ +/* Add padding or margin to the container around the datatable */ +.datatable-container { + margin-bottom: 20px; /* Add margin below the datatable */ +} -/* Custom styles for the lightning-card */ +/* Optional: Keep the existing styles */ .lightning-card { max-width: 100%; /* Ensure the card does not exceed its container */ overflow-x: auto; /* Allow horizontal scrolling if content overflows */ } -/* Style the title of the lightning-card */ .lightning-card__header { background-color: transparent; /* Remove background color */ border-bottom: none; /* Remove border under the title */ @@ -15,7 +17,6 @@ padding: 0; /* Adjust padding */ } -/* Optionally, you can add some margin or padding to the datatable if needed */ .lightning-datatable { - margin-top: 10px; /* Example of adding some margin above the table */ + margin-top: 10px; /* Margin above the table */ } diff --git a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html index 22909b1..67ef79f 100644 --- a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html +++ b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html @@ -1,15 +1,42 @@