Please find the example below on how pagination works in lightning
Step 1 : Create Apex Class Controller.
Apex class [samplePagerCtrl.apxc]
public with sharing class samplePagerCtrl {
@AuraEnabled
public static AccountPagerWrapper fetchAccount(Decimal pageNumber ,Integer recordToDisply) {
Integer pageSize = recordToDisply;
Integer offset = ((Integer)pageNumber - 1) * pageSize;
// create a instance of wrapper class.
AccountPagerWrapper obj = new AccountPagerWrapper();
// set the pageSize,Page(Number), total records and accounts List(using OFFSET)
obj.pageSize = pageSize;
obj.page = (Integer) pageNumber;
obj.total = [SELECT count() FROM account];
obj.accounts = [SELECT Id, Name,Phone FROM Account ORDER BY Name LIMIT :recordToDisply OFFSET :offset];
// return the wrapper class instance .
return obj;
}
// create a wrapper class with @AuraEnabled Properties
public class AccountPagerWrapper {
@AuraEnabled public Integer pageSize {get;set;}
@AuraEnabled public Integer page {get;set;}
@AuraEnabled public Integer total {get;set;}
@AuraEnabled public List<Account> accounts {get;set;}
}
}
In the above apex class we have a @AuraEnabled method which is takes 2 parameters.
Next we are create a wrapper/inner class in our controller class with 4 @AuraEnabled properties
And in the fetchAccount method we are create a instance of wrapper class and set the @AuraEnabled properties .
And return the wrapper class instance.
In above apex class i’m using OFFSET for display the results in multiple pages.
Sorry its a typo error,i was supposed to post pagereference(once i click on submitbutton record should insert into my sobjet and its detail page should open,same like when u insert a record into sobject,it gets saved into sobject and that records detail will get open in configuration)i want to achive it using lightning component.in vf it is achived through pagereference(which we write in controller),how to achive this in lightning component.
({ saveme : function(component, event, helper) { var studdtls = component.get("v.studentrec");//from component to client controller. var studentdetails = component.get("c.insertfun");//apex function is called from server-side controller. studentdetails.setParams({stud : studdtls}); // Create a callback that is executed after // the server-side action returns studentdetails.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { // Alert the user with the value returned // from the server alert("From server: " + response.getReturnValue()); var newstudent = {'sobjecttype':'Student__c','Name':'','Last_Name__c':'','Dob__c':'','Course__c':'',}; component.set("v.studentrec",newstudent);
// You would typically fire a event here to trigger // client-side notification that the server-side // action is complete } else if (state === "INCOMPLETE") { alert("I am in incomplete"); // do something } else if (state === "ERROR") { var errors = response.getError(); if (errors) { if (errors[0] && errors[0].message) { console.log("Error message: " + errors[0].message); } } else { console.log("Unknown error"); } } }); $A.enqueueAction(studentdetails);
} })
Server-Side-Controller:
public with sharing class SaveRecord { @AuraEnabled public static id insertfun(Student__c stud){ insert stud; return stud.id; }
}
insertion is happening here but how does it lead to detailpage after insrtion?
Please find the example below on how pagination works in lightning
Step 1 : Create Apex Class Controller.
Apex class [samplePagerCtrl.apxc] In the above apex class we have a @AuraEnabled method which is takes 2 parameters.
Next we are create a wrapper/inner class in our controller class with 4 @AuraEnabled properties
And in the fetchAccount method we are create a instance of wrapper class and set the @AuraEnabled properties .
And return the wrapper class instance.
In above apex class i’m using OFFSET for display the results in multiple pages.
See code comments
Step 2 : Create Lightning Component.
Component [SamplePager.cmp]
Hope this helps.
For more information please check below link.
- http://piyushparmar01.blogspot.in/2017/02/pagination-on-lightning-component.html
Kindly mark this as solved if the information was helpful.Thanks,
Nagendra
Sorry its a typo error,i was supposed to post pagereference(once i click on submitbutton record should insert into my sobjet and its detail page should open,same like when u insert a record into sobject,it gets saved into sobject and that records detail will get open in configuration)i want to achive it using lightning component.in vf it is achived through pagereference(which we write in controller),how to achive this in lightning component.
Component:
<aura:component controller="SaveRecord">
<aura:attribute name="course" type="List" default="['C','C++','C#']"/>
<aura:attribute name="studentrec" type="Student__c" default="{'Sobjecttype':'Student__c'}"/>
<lightning:input label="Enter Student Name" name="stuname" value="{!v.studentrec.Name}" />
<lightning:input label="Enter Last Name" name="lstname" value="{!v.studentrec.Last_Name__c}" required="true"/>
<lightning:input label="Date Of Birth" name="dob" value="{!v.studentrec.Dob__c}"/>
<lightning:select label="Enter Course Details" name="lcourse" value="{!v.studentrec.Course__c}">
<aura:iteration items="{!v.course}" var="sme">
<option value="{!sme}" text="{!sme}"/>
</aura:iteration>
</lightning:select>
<lightning:button label="Submit" onclick="{!c.saveme}"/>
</aura:component>
Client-Side-Controller:
({
saveme : function(component, event, helper) {
var studdtls = component.get("v.studentrec");//from component to client controller.
var studentdetails = component.get("c.insertfun");//apex function is called from server-side controller.
studentdetails.setParams({stud : studdtls});
// Create a callback that is executed after
// the server-side action returns
studentdetails.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
// Alert the user with the value returned
// from the server
alert("From server: " + response.getReturnValue());
var newstudent = {'sobjecttype':'Student__c','Name':'','Last_Name__c':'','Dob__c':'','Course__c':'',};
component.set("v.studentrec",newstudent);
// You would typically fire a event here to trigger
// client-side notification that the server-side
// action is complete
}
else if (state === "INCOMPLETE") {
alert("I am in incomplete");
// do something
}
else if (state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log("Error message: " +
errors[0].message);
}
} else {
console.log("Unknown error");
}
}
});
$A.enqueueAction(studentdetails);
}
})
Server-Side-Controller:
public with sharing class SaveRecord {
@AuraEnabled
public static id insertfun(Student__c stud){
insert stud;
return stud.id;
}
}
insertion is happening here but how does it lead to detailpage after insrtion?