PaginationSearch.cmp ......... component
<aura:component controller="pagiSearchController" implements="flexipage:availableForAllPageTypes,force:appHostable,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName"
access =“ global”>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="AccountList" type="Account[]"/>
<aura:attribute name="PageNumber" type="integer" default="1"/>
<aura:attribute name="TotalPages" type="integer" default="0"/>
<aura:attribute name="TotalRecords" type="integer" default="0"/>
<aura:attribute name="RecordStart" type="integer" default="0"/>
<aura:attribute name="RecordEnd" type="integer" default="0"/>
<aura:attribute name="selectedValue" type="integer" default="5"/>
<aura:registerEvent name="showToast" type="force:showToast"/>
<aura:attribute name="searchKey" type="String" description="use for store user search input"/>
<aura:attribute name="defaultShow" type="boolean" default="true"/>
<aura:attribute name="showButton" type="boolean" default="false"/>
<aura:attribute name="currentId" type="String"/>
<aura:attribute name="spinner" type="boolean" default="false"/>
<aura:handler event="aura:waiting" action="{!c.showSpinner}"/>
<aura:handler event="aura:doneWaiting" action="{!c.hideSpinner}"/>
<lightning:card variant="Basic" title="Account" iconName="standard:account">
<aura:set attribute="actions">
<lightning:layout class="slds-float_right">
<lightning:layoutItem size="7" padding="around-small">
<lightning:input value="{!v.searchKey}"
placeholder="Search Accounts.."
aura:id="searchField"
onchange="{!c.searchKeyChange}"/>
</lightning:layoutItem>
<lightning:layoutItem size="5" padding="slds-p-top_medium" class="buttonPosition">
<lightning:button label="New Account" onclick="{!c.createRecord}"/>
</lightning:layoutItem>
</lightning:layout>
</aura:set>
</lightning:card>
<div class="slds-page-header" role="banner">
<aura:if isTrue="{!v.showButton}">
<lightning:buttonIcon iconName="utility:home" class="slds-p-around_small" size="large" variant="bare"
onclick =“ {!c.doInit}” AlternativeText =“ HomePage” />
<aura:if isTrue="{!v.spinner}">
<div class="exampleHolder">
<lightning:spinner alternativeText="Loading" size="medium" />
</div>
</aura:if>
<table aria-multiselectable="true" class="slds-table slds-table_bordered slds-table_resizable-cols" role="grid">
<thead>
<tr class="slds-text-title_caps">
<th scope="col">
<div class="slds-truncate" title="S.no">S.no</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Account Name">Account Name</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Type">Type</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Industry">Industry</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Phone">Phone</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Fax">Fax</div>
</th>
<th scope="col">
<div class="slds-truncate" title="Fax"></div>
</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.AccountList}" var="acc" indexVar="count">
<tr>
<td> {!acc.Id}</td>
<td> {!acc.Name}</td>
<td> {!acc.Type}</td>
<td> {!acc.Industry}</td>
<td> {!acc.Phone}</td>
<td> {!acc.Fax}</td>
<td>
<lightning:buttonMenu iconSize="x-small" aura:id="menu" value="{!acc.Id}" onselect="{!
c.handleSelect}“ AlternativeText =”显示菜单“>
<aura:if isTrue="{!v.defaultShow}">
<lightning:select aura:Id ="pageSize" class="marginText slds-size_2-of-12" name="selectItem"
value =“ {!v.selectedValue}” label =“每页显示记录:”
onchange =“ {!c.onSelectChange}”>
<aura:if isTrue="{!v.defaultShow}">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_9-of-12 ">
<lightning:layoutItem>
Page No.<lightning:input name="input1" class="widthPageNoBox" value="{!v.PageNumber}"
onchange =“ {!c.setPageNum}” />总页数:{!v.TotalPages}来自 总记录:{!v.TotalRecords}
<div class="slds-col slds-size_3-of-12 pageNoDisplay">
<lightning:layout>
<lightning:layoutItem>
<lightning:button disabled="{!v.PageNumber == 1}" variant="brand" label="First" onclick="{!c.handleFirst}" />
<lightning:button disabled="{!v.PageNumber == 1}" variant="brand" aura:id="prevPage" label="Prev" onclick="{!c.handlePrev}" />
<lightning:button disabled="{!v.PageNumber == v.TotalPages}" aura:id="nextPage" variant="brand" label="Next" onclick="{!c.handleNext}"/>
<lightning:button disabled="{!v.PageNumber == v.TotalPages}" variant="brand" label="Last" onclick="{!c.handleLast}"/>
</lightning:layoutItem>
</lightning:layout>
</div>
</div>
</aura:if>
</div>
</aura:component>
PaginationSearchController..................controller
({
doInit: function(component, event, helper) {
component.set("v.defaultShow",true);
component.set("v.showButton",false);
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
helper.getAccountList(component, pageNumber, pageSize);
},
handleFirst: function(component, event, helper) {
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
helper.getAccountList(component, 1 , pageSize);
},
handleNext: function(component, event, helper) {
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
pageNumber++;
helper.getAccountList(component, pageNumber, pageSize);
},
handlePrev: function(component, event, helper) {
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
pageNumber--;
helper.getAccountList(component, pageNumber, pageSize);
},
handleLast: function(component, event, helper) {
var pageSize = component.find("pageSize").get("v.value");
var TotalRecords =component.get("v.TotalRecords");
var last = Math.ceil(TotalRecords/pageSize);
helper.getAccountList(component, last, pageSize);
},
setPageNum :function(component, event, helper){
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
var totalPages = component.get("v.TotalPages");
if(pageNumber > totalPages){
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
title : '',
message: 'Invalid page number!!,The page number you entered is greater then the total number of existing pages.',
duration:' 8000',
key: 'info_alt',
type: 'error',
mode: 'pester'
});
toastEvent.fire();
helper.getAccountList(component,1, pageSize);
}else{
helper.getAccountList(component, pageNumber, pageSize);
}
},
onSelectChange: function(component, event, helper){
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
helper.getAccountList(component, pageNumber, pageSize);
},
createRecord : function (component, event, helper) {
var createRecordEvent = $A.get("e.force:createRecord");
createRecordEvent.setParams({
"entityApiName": "Account"
});
createRecordEvent.fire();
},
searchKeyChange: function(component, event,helper){
component.set("v.defaultShow",false);
component.set("v.showButton",true);
var searchKey = component.find("searchField").get("v.value");
if($A.util.isEmpty(searchKey)){
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
title : '',
message: 'Please enter the string you want to search.',
duration:' 8000',
key: 'info_alt',
type: 'error',
mode: 'pester'
});
toastEvent.fire();
helper.getAccountList(component, 1,5);
}else{
helper.getAccount(component);
}
},
handleSelect: function (component, event) {
console.log("in han");
var selected = event.getParam("label");
console.log("selected"+selected);
var selectedMenuItemValue = event.getParam("value");
console.log("selectedMenuItemValue"+selectedMenuItemValue);
var action = component.get("c.deleteAccount");
action.setParams({accId:selectedMenuItemValue});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
$A.get('e.force:refreshView').fire();
}
});
$A.enqueueAction(action);
document.location.reload(true);
switch (selectedMenuItemValue) {
case 'menuSec':
var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
"url": "/"+selectedMenuItemValue
});
urlEvent.fire();
break;
}
},
showSpinner: function(component, event) {
component.set("v.spinner", true);
},
hideSpinner : function(component,event){
component.set("v.spinner", false);
}
})
Paginationsearchelper..................helper
({
getAccountList: function(component, pageNumber, pageSize) {
var action = component.get("c.getAccountData");
action.setParams({
"pageNumber": pageNumber,
"pageSize": pageSize
});
action.setCallback(this, function(result) {
var state = result.getState();
if (state === "SUCCESS"){
var resultData = result.getReturnValue();
console.log(JSON.stringify(resultData.accountList));
component.set("v.AccountList", resultData.accountList);
component.set("v.PageNumber", resultData.pageNumber);
component.set("v.TotalRecords", resultData.totalRecords);
component.set("v.RecordStart", resultData.recordStart);
component.set("v.TotalPages", Math.ceil(resultData.totalRecords / pageSize));
}
});
$A.enqueueAction(action);
},
getAccount: function(component){
var searchKey = component.find("searchField").get("v.value");
var action = component.get("c.fetchAccount");
action.setParams({
"searchKeyWord": searchKey
});
action.setCallback(this, function(response) {
var state = response.getState();
if (component.isValid() && state === "SUCCESS"){
var resultData = response.getReturnValue();
component.set("v.TotalRecords", component.get("v.AccountList").length);
component.set('v.AccountList', resultData);
}
});
$A.enqueueAction(action);
},
})
pagiSearchController.apxc
public class pagiSearchController {
@AuraEnabled
public static List < account > fetchAccount(String searchKeyWord){
String searchKey = searchKeyWord + '%';
List < Account > returnList = new List < Account > ();
List < Account > lstOfAccount = [select id, Name, Type, Industry, Phone, Fax from account
where Name LIKE: searchKey];
for (Account acc: lstOfAccount) {
returnList.add(acc);
}
return returnList;
}
@AuraEnabled
public static void deleteAccount(String accId){
delete [select id, Name, Type, Industry, Phone, Fax from account where id=:accId] ;
}
@AuraEnabled
public static AccountDataTableWrapper getAccountData(Decimal pageNumber, Decimal pageSize){
Integer pSize = (Integer)pageSize;
Integer pNumber = (Integer)pageNumber;
Integer offset = (pNumber - 1) * pSize;
Integer recordEnd = pSize * pNumber;
//System.debug('offset'+offset);
Integer totalRecords = [SELECT COUNT() FROM Account];
AccountDataTableWrapper objDT = new AccountDataTableWrapper();
objDT.pageSize = pSize;
objDT.pageNumber = pNumber;
objDT.recordStart = offset + 1;
objDT.recordEnd = totalRecords >= recordEnd ? recordEnd : totalRecords;
objDT.totalRecords = totalRecords;
objDT.accountList = [select Id, Name, Type, Industry, Phone,Fax, CreatedDate from account ORDER BY Name LIMIT :pSize
OFFSET:offset]; 返回objDT; }
public class AccountDataTableWrapper {
@AuraEnabled
public Integer pageSize {get;set;}
@AuraEnabled
public Integer pageNumber {get;set;}
@AuraEnabled
public Integer totalRecords {get;set;}
@AuraEnabled
public Integer recordStart {get;set;}
@AuraEnabled
public Integer recordEnd {get;set;}
@AuraEnabled
public List<Account> accountList {get;set;}
}
}
答案 0 :(得分:0)
对于记录OFFSET:offset仅适用于前2000条记录,您需要更改代码中的方式
您需要查询所有记录,然后可以在控制器中根据需要进行操作 您可以例如创建地图>,然后将结果划分在列表中,每个键代表页码
如果评论对您有帮助,请标记为您的答案,否则您可以问我任何需要进一步澄清的问题
习惯上