function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
abu saleh khan 8abu saleh khan 8 

my data got saved in sobject but it is not leading to detail page,can any one help me how pagenation works in lightning?

NagendraNagendra (Salesforce Developers) 
Hi Khan,

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.

See code comments 

Step 2 : Create Lightning Component.

Component [SamplePager.cmp]
<aura:component controller="samplePagerCtrl">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <aura:attribute name="Accounts" type="Account[]"/>
    <aura:attribute name="page" type="integer" description="using for store page Number"/>
    <aura:attribute name="pages" type="integer" description="using for store All Pages page Number"/>
    <aura:attribute name="total" type="integer" description="total records count store "/>
    
    <div class="slds-m-around_small">
        <div class="slds-page-header" role="banner">
            <div class="slds-align_absolute-center">            
                <lightning:button disabled="{!v.page == 1}" variant="brand" label="Previous Page" onclick="{! c.navigate }" />            
                <lightning:button disabled="{!v.page == v.pages}" aura:id="previousPage" variant="brand" label="Next Page" onclick="{! c.navigate }" />
            </div>
            <p class="slds-page-header__title slds-truncate">{!v.total} Accounts • page {!v.page} / {!v.pages}</p>
            <ui:inputSelect aura:id="recordSize" label="Display Record Per Page: " change="{!c.onSelectChange}">
                <ui:inputSelectOption text="10" label="10" value="true"/>
                <ui:inputSelectOption text="15" label="15"/>
                <ui:inputSelectOption text="20" label="20"/>
            </ui:inputSelect>
        </div>
        
        <table class="slds-table slds-table_bordered slds-table_cell-buffer">
            <thead>
                <tr class="slds-text-title_caps">
                    <th scope="col">
                        <div class="slds-truncate" title="Name">Account Name</div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration items="{!v.Accounts}" var="account"> 
                    <tr>
                        <th scope="row" data-label="Opportunity Name">
                            <div class="slds-truncate" title="{!account.Name}">{!account.Name}</div>
                        </th>
                    </tr>
                </aura:iteration>	
            </tbody>
        </table>
    </div>
</aura:component>
User-added image
Hope this helps.

For more information please check below link. Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra

 
abu saleh khan 8abu saleh khan 8
Hi 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?