You need to sign in to do that
Don't have an account?
Jonathan Wolff 7
Sort datatable by descending date
Hello, I got a set of columns for my datatable, but the problem is they appear without the columns being ordered by date:
I cant order it through my Sql query because i got a wrapper class. Can you show me how to use sorting in datatable with controller.
Component:
Controller:
Apex:
I cant order it through my Sql query because i got a wrapper class. Can you show me how to use sorting in datatable with controller.
Component:
<aura:component controller="ApexActivityWrapper" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global"> <aura:attribute name="taskEventList2" type="object"/> <aura:handler name="init" value="{!this}" action="{!c.doInIt}"/> <aura:attribute name="mycolumns" type="List"/> <div style= "font-size: 14px; font-weight: bold; margin-left:15px; margin-top:10px;"> ActivityList </div> <div> <aura:if isTrue="{!not(empty(v.taskEventList2))}"> <lightning:datatable data="{!v.taskEventList2}" columns="{!v.mycolumns}" keyField="Id" hideCheckboxColumn="true"/> <aura:set attribute="else"> <div Style="text-align : center">Keine Aufgaben</div> </aura:set> </aura:if> </div> </aura:component>
Controller:
({ doInIt : function(component, event, helper) { component.set('v.mycolumns', [ {label: "Datum", fieldName: "DatumOut", type: "date-local", typeAttributes: {month: "2-digit", day: "2-digit" }}, {label: 'Typ', fieldName: 'TypOut', type: 'text'}, {label: 'Thema', fieldName: 'ThemaOut', type: 'string', typeAttributes: {label: { fieldName: 'sub' }, target: '_blank'}}, {label: 'Bezug zu', fieldName: 'BezugOut', type: 'text'}, {label: 'Zugewiesen zu', fieldName: 'ZugewiesenOut', type: 'text'}, ]); var action = component.get("c.ApexActivityWrapper"); var recordId = component.get('v.recordId'); action.setParams({ recordId : recordId }); action.setCallback(this, function(response){ var state = response.getState(); console.log('state==='+state); if(state === "SUCCESS") { var response = response.getReturnValue(); console.log('response==='+JSON.stringify(response)); component.set("v.taskEventList2", response); } else if(state === "INCOMPLETE") { //do something } else if(state === "ERROR") { var error = response.getError(); if(error) { console.log("error"+error); } } }); $A.enqueueAction(action); } })
Apex:
public Class ApexActivityWrapper { @auraEnabled public static List<eEventTTask> ApexActivityWrapper (String recordId) { system.debug('recordId =' + recordId); List<eEventTTask> taskEventList = new List<eEventTTask>(); // Abfrage von Terminen/Events for (Event e: [SELECT Id, Subject, ActivityDate, What.Name, Owner.Name FROM Event WHERE Id IN (select EventId from EventRelation where RelationId = :recordId)]) { taskEventList.add(new eEventTTask(e.Subject, e.ActivityDate, 'E', e.What.Name, e.Owner.Name)); } // Abfrage von Aufgaben/Tasks for (Task t: [SELECT Id, Subject, ActivityDate, What.Name, Owner.Name FROM Task WHERE Id IN (select TaskId from TaskRelation where RelationId = :recordId)]) { taskEventList.add(new eEventTTask(t.Subject, t.ActivityDate, 'T', t.What.Name, t.Owner.Name)); } // Abfrage von Email-Messages // for (EmailMessage emm: [SELECT Id, Subject, MessageDate FROM EmailMessage WHERE Id IN (select EmailMessageId from EmailMessageRelation where RelationId = :recordId)]) { // taskEventList.add(new eEventTTask(emm.Subject, emm.MessageDate, 'EMM')); // } // Abfrage von Notizen/Notes for(Note n: [SELECT Id, Title, CreatedDate, Createdby.Name, Owner.Name, Parent.Name, ParentId FROM Note WHERE ParentId = :recordId]) { taskEventList.add(new eEventTTask(n.Title, date.newInstance(n.CreatedDate.year(), n.CreatedDate.month(), n.CreatedDate.day()), 'N', n.Parent.Name, n.Owner.Name)); } // Abrage von Anlagen/Dateien (alte Version - wird von Salesforce irgendwann abgestellt) for(Attachment a: [SELECT Id, Name, CreatedDate, Createdby.Name, Owner.Name, Parent.Name, ParentId FROM Attachment WHERE ParentId = :recordId]) { taskEventList.add(new eEventTTask(a.Name, date.newInstance(a.CreatedDate.year(), a.CreatedDate.month(), a.CreatedDate.day()), 'A', a.Parent.Name, a.Owner.Name)); } // Abfrage von Notizen (neue Version) for(ContentDocumentLink c: [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.createdDate, ContentDocument.Createdby.Name, ContentDocument.FileExtension, LinkedEntity.Id, LinkedEntity.Name FROM ContentDocumentLink WHERE LinkedEntityId = :recordId]) { taskEventList.add(new eEventTTask(c.ContentDocument.Title, date.newInstance(c.ContentDocument.CreatedDate.year(), c.ContentDocument.CreatedDate.month(), c.ContentDocument.CreatedDate.day()), 'C', c.LinkedEntity.Name, c.ContentDocument.CreatedBy.Name)); } system.debug('taskEventList' + taskEventList); return taskEventList; } public class eEventTTask { @AuraEnabled public String ThemaOut {get; set;} @AuraEnabled public Date DatumOut {get; set;} @AuraEnabled public String TypOut {get; set;} @AuraEnabled public String BezugOut {get; set;} @AuraEnabled public String ZugewiesenOut {get; set;} public eEventTTask(String ThemaIn, Date DatumIn, String TypIn, String BezugIn, String ZugewiesenIn) { ThemaOut = ThemaIn; DatumOut = DatumIn; TypOut = TypIn; BezugOut = BezugIn; ZugewiesenOut = ZugewiesenIn; } } }
Refer the below link have lightning data table with sorting. modify your code accordingly.
https://www.salesforcebolt.com/2020/04/add-sorting-in-lightning-data-table.html
Thanks!!
Please follwo below url for your solution:-
https://www.apexhours.com/lightning-datatable-sorting-in-lightning-web-components/#:~:text=Lightning%20datatable%20provides%20an%20onsort,fieldName%20attribute%20on%20the%20column.
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh