Skip to content

Commit c7cb274

Browse files
authored
Merge pull request #2 from edunzer/feature-AddPagination
Feature add pagination
2 parents 29229bb + 2ccc0ad commit c7cb274

File tree

8 files changed

+162
-33
lines changed

8 files changed

+162
-33
lines changed

force-app/main/default/classes/workCardController.cls

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ public with sharing class workCardController {
55
return [
66
SELECT Id, Name, Action_Type__c, Status__c, Due_Date__c, Description__c, User__c, Work__c
77
FROM Work_Item_Action__c
8-
WHERE Work__c = :workId
8+
WHERE Work__c = :workId
99
AND User__c = :currentUserId
10+
ORDER BY Name DESC
1011
];
1112
}
1213
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
public with sharing class workListViewController {
22
@AuraEnabled(cacheable=true)
3-
public static List<agf__ADM_Work__c> getWorkRecords() {
3+
public static Map<String, Object> getWorkRecords(Integer pageSize, Integer pageNumber) {
44
// Get the current user's Id
55
Id currentUserId = UserInfo.getUserId();
6-
System.debug('Current User ID: ' + currentUserId);
6+
Integer offset = (pageNumber - 1) * pageSize;
77

88
// Query for Work Item Team records related to the current user
99
List<agf__ADM_Work__c> workRecords = [
@@ -14,13 +14,17 @@ public with sharing class workListViewController {
1414
FROM Work_Item_Team__c
1515
WHERE User__c = :currentUserId
1616
)
17+
LIMIT :pageSize OFFSET :offset
1718
];
1819

19-
System.debug('Work Records Found: ' + workRecords.size());
20-
for (agf__ADM_Work__c workRecord : workRecords) {
21-
System.debug('Work Record: ' + workRecord);
22-
}
23-
24-
return workRecords;
20+
// Query for the total number of records for pagination
21+
Integer totalRecords = [SELECT COUNT() FROM agf__ADM_Work__c WHERE Id IN (SELECT Work__c FROM Work_Item_Team__c WHERE User__c = :currentUserId)];
22+
23+
// Prepare the result map
24+
Map<String, Object> result = new Map<String, Object>();
25+
result.put('workRecords', workRecords);
26+
result.put('totalRecords', totalRecords);
27+
28+
return result;
2529
}
2630
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
.slds-p-horizontal_small {
22
font-size: 0.9rem; /* Adjust text size if needed */
33
}
4+
5+
/* Add padding between the card and the pagination buttons */
6+
.pagination-container {
7+
margin-top: 20px;
8+
}

force-app/main/default/lwc/workCardComponent/workCardComponent.html

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<template>
2-
<!-- Header for the component -->
32
<lightning-card title="Work Action Items">
43
<div class="slds-m-around_medium">
5-
<!-- Display message or work items based on conditions -->
64
<template if:true={message}>
75
<div class="slds-text-align_center slds-m-around_medium slds-box">
86
{message}
97
</div>
108
</template>
11-
<template if:true={workItemActions}>
12-
<template for:each={workItemActions} for:item="action">
9+
<template if:true={paginatedWorkItemActions}>
10+
<template for:each={paginatedWorkItemActions} for:item="action">
1311
<lightning-card
1412
key={action.Id}
1513
icon-name="standard:action_list_component"
@@ -35,6 +33,34 @@
3533
</lightning-card>
3634
</template>
3735
</template>
36+
<template if:true={isPaginationVisible}>
37+
<div class="pagination-container">
38+
<lightning-layout horizontal-align="space">
39+
<lightning-layout-item flexibility="auto">
40+
<lightning-button
41+
label="Previous"
42+
icon-name="utility:chevronleft"
43+
onclick={handlePrevious}
44+
disabled={isPreviousDisabled}
45+
>
46+
</lightning-button>
47+
</lightning-layout-item>
48+
<lightning-layout-item flexibility="auto">
49+
Page {page} of {totalPage}
50+
</lightning-layout-item>
51+
<lightning-layout-item flexibility="auto">
52+
<lightning-button
53+
label="Next"
54+
icon-name="utility:chevronright"
55+
icon-position="right"
56+
onclick={handleNext}
57+
disabled={isNextDisabled}
58+
>
59+
</lightning-button>
60+
</lightning-layout-item>
61+
</lightning-layout>
62+
</div>
63+
</template>
3864
<template if:true={error}>
3965
<div class="slds-text-color_error slds-text-align_center slds-m-around_medium slds-box">
4066
{error}
@@ -43,6 +69,5 @@
4369
</div>
4470
</lightning-card>
4571

46-
<!-- Include the workPopupComponent to ensure it's available in the DOM -->
4772
<c-work-popup-component></c-work-popup-component>
4873
</template>

force-app/main/default/lwc/workCardComponent/workCardComponent.js

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ import WORK_ITEM_MESSAGE_CHANNEL from '@salesforce/messageChannel/WorkItemMessag
77
import { refreshApex } from '@salesforce/apex';
88

99
export default class WorkCardComponent extends NavigationMixin(LightningElement) {
10-
@track workItemActions = []; // Track this property to ensure reactivity
10+
@track workItemActions = [];
1111
error;
12-
workRecordId; // Stores the Work record ID
13-
selectedActionId; // Stores the Work Item Action record ID
12+
workRecordId;
13+
selectedActionId;
1414
isModalOpen = false;
15-
message = 'Select work item to see action items'; // Default message
16-
15+
message = 'Select work item to see action items';
1716
wiredResult;
1817

18+
@track page = 1;
19+
@track pageSize = 3;
20+
@track totalPage = 1;
21+
1922
@wire(MessageContext)
2023
messageContext;
2124

@@ -42,27 +45,28 @@ export default class WorkCardComponent extends NavigationMixin(LightningElement)
4245

4346
handleWorkMessage(message) {
4447
console.log('Message received in workCardComponent from WorkMessageChannel:', message);
45-
this.workRecordId = message.recordId; // This is the Work record ID
46-
this.message = undefined; // Clear any existing messages
47-
this.refreshData(); // Fetch the latest work item actions
48+
this.workRecordId = message.recordId;
49+
this.page = 1; // Reset to first page
50+
this.message = undefined;
51+
this.refreshData();
4852
}
4953

5054
handleWorkItemMessage(message) {
5155
console.log('Message received in workCardComponent from WorkItemMessageChannel:', message);
52-
// Ensure the correct Work record data is refreshed
5356
this.refreshData();
5457
}
5558

5659
@wire(getWorkItemActions, { workId: '$workRecordId' })
5760
wiredWorkItemActions(result) {
58-
this.wiredResult = result; // Store the wired result for refreshing
61+
this.wiredResult = result;
5962
const { data, error } = result;
6063
if (data) {
6164
if (data.length > 0) {
6265
this.workItemActions = data.map(action => ({
6366
...action,
6467
actionUrl: `/ideaexchange/s/work-item-action/${action.Id}/view`
6568
}));
69+
this.totalPage = Math.ceil(this.workItemActions.length / this.pageSize);
6670
this.message = undefined;
6771
} else {
6872
this.workItemActions = [];
@@ -78,16 +82,13 @@ export default class WorkCardComponent extends NavigationMixin(LightningElement)
7882
}
7983

8084
refreshData() {
81-
// Clear the existing data to force a re-render
8285
this.workItemActions = [];
8386
this.error = undefined;
8487

85-
// Explicitly refresh the wired data
8688
if (this.wiredResult) {
8789
refreshApex(this.wiredResult);
8890
console.log('Data refreshed via refreshApex.');
8991
} else {
90-
// Fallback in case refreshApex is not applicable
9192
this.fetchWorkItemActions();
9293
}
9394
}
@@ -101,6 +102,7 @@ export default class WorkCardComponent extends NavigationMixin(LightningElement)
101102
...action,
102103
actionUrl: `/ideaexchange/s/work-item-action/${action.Id}/view`
103104
}));
105+
this.totalPage = Math.ceil(this.workItemActions.length / this.pageSize);
104106
this.message = undefined;
105107
} else {
106108
this.workItemActions = [];
@@ -117,6 +119,36 @@ export default class WorkCardComponent extends NavigationMixin(LightningElement)
117119
});
118120
}
119121

122+
get paginatedWorkItemActions() {
123+
const start = (this.page - 1) * this.pageSize;
124+
const end = start + this.pageSize;
125+
return this.workItemActions.slice(start, end);
126+
}
127+
128+
get isPaginationVisible() {
129+
return this.workItemActions.length > 4;
130+
}
131+
132+
get isPreviousDisabled() {
133+
return this.page === 1;
134+
}
135+
136+
get isNextDisabled() {
137+
return this.page === this.totalPage || this.totalPage === 0;
138+
}
139+
140+
handlePrevious() {
141+
if (this.page > 1) {
142+
this.page -= 1;
143+
}
144+
}
145+
146+
handleNext() {
147+
if (this.page < this.totalPage) {
148+
this.page += 1;
149+
}
150+
}
151+
120152
handleEditClick(event) {
121153
const actionRecordId = event.currentTarget.dataset.id;
122154
console.log('View button clicked for recordId:', actionRecordId);

force-app/main/default/lwc/workListViewComponent/workListViewComponent.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
padding: 0; /* Adjust padding */
1414
}
1515

16-
/* Optionally, you can add some margin or padding to the datatable if needed */
16+
/* Add some margin or padding to the datatable */
1717
.lightning-datatable {
1818
margin-top: 10px; /* Example of adding some margin above the table */
1919
}
20+
21+
/* Add padding between the datatable and the pagination buttons */
22+
.pagination-container {
23+
margin-top: 20px;
24+
}

force-app/main/default/lwc/workListViewComponent/workListViewComponent.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,32 @@
99
max-row-selection="1"
1010
>
1111
</lightning-datatable>
12+
13+
<div class="pagination-container">
14+
<lightning-layout horizontal-align="space">
15+
<lightning-layout-item flexibility="auto">
16+
<lightning-button
17+
label="Previous"
18+
icon-name="utility:chevronleft"
19+
onclick={handlePrevious}
20+
disabled={isPreviousDisabled}
21+
>
22+
</lightning-button>
23+
</lightning-layout-item>
24+
<lightning-layout-item flexibility="auto">
25+
Page {page} of {totalPage}
26+
</lightning-layout-item>
27+
<lightning-layout-item flexibility="auto">
28+
<lightning-button
29+
label="Next"
30+
icon-name="utility:chevronright"
31+
icon-position="right"
32+
onclick={handleNext}
33+
disabled={isNextDisabled}
34+
>
35+
</lightning-button>
36+
</lightning-layout-item>
37+
</lightning-layout>
38+
</div>
1239
</lightning-card>
1340
</template>

force-app/main/default/lwc/workListViewComponent/workListViewComponent.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LightningElement, wire } from 'lwc';
1+
import { LightningElement, wire, track } from 'lwc';
22
import getWorkRecords from '@salesforce/apex/WorkListViewController.getWorkRecords';
33
import { publish, MessageContext } from 'lightning/messageService';
44
import WORK_MESSAGE_CHANNEL from '@salesforce/messageChannel/WorkMessageChannel__c';
@@ -14,16 +14,22 @@ const columns = [
1414
];
1515

1616
export default class WorkListViewComponent extends LightningElement {
17-
columns = columns;
18-
workRecords;
17+
@track columns = columns;
18+
@track workRecords;
19+
@track page = 1;
20+
@track pageSize = 10; // Number of records per page
21+
@track totalRecords;
22+
@track totalPage;
1923

2024
@wire(MessageContext)
2125
messageContext;
2226

23-
@wire(getWorkRecords)
27+
@wire(getWorkRecords, { pageSize: '$pageSize', pageNumber: '$page' })
2428
wiredWorkRecords({ error, data }) {
2529
if (data) {
26-
this.workRecords = data.map(record => {
30+
this.totalRecords = data.totalRecords;
31+
this.updateTotalPages();
32+
this.workRecords = data.workRecords.map(record => {
2733
return {
2834
...record,
2935
workUrl: `/ideaexchange/s/adm-work/${record.Id}`,
@@ -39,6 +45,30 @@ export default class WorkListViewComponent extends LightningElement {
3945
}
4046
}
4147

48+
updateTotalPages() {
49+
this.totalPage = Math.ceil(this.totalRecords / this.pageSize);
50+
}
51+
52+
get isPreviousDisabled() {
53+
return this.page <= 1;
54+
}
55+
56+
get isNextDisabled() {
57+
return this.page >= this.totalPage;
58+
}
59+
60+
handlePrevious() {
61+
if (this.page > 1) {
62+
this.page -= 1;
63+
}
64+
}
65+
66+
handleNext() {
67+
if (this.page < this.totalPage) {
68+
this.page += 1;
69+
}
70+
}
71+
4272
handleRowSelection(event) {
4373
const selectedRows = event.detail.selectedRows;
4474
if (selectedRows.length > 0) {

0 commit comments

Comments
 (0)