Last active
March 30, 2017 10:20
-
-
Save peterknolle/3e5dbba5ea1185feff33 to your computer and use it in GitHub Desktop.
Remote Objects Infinite Scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class ContactsController { | |
@RemoteAction | |
public static Map<String, Object> retrieveContacts(String type, List<String> fields, Map<String, Object> criteria) { | |
// Retrieve using the standard retrieve | |
Map<String, Object> result = RemoteObjectController.retrieve(type, fields, criteria); | |
// Add in the total record count for the current user. | |
// This is needed to know when to stop scrolling. | |
Integer totalContacts = [ | |
SELECT Count() | |
FROM Contact | |
WHERE OwnerId =: UserInfo.getUserId() | |
]; | |
// Create a new map since the result map is read-only | |
Map<String, Object> customResult = | |
new Map<String, Object> {'totalContacts'=> totalContacts}; | |
customResult.putAll(result); | |
return customResult; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="ContactsController"> | |
<apex:includeScript value="{!URLFOR($Resource.jquery, 'jquery-1.11.1.min.js')}"/> | |
<!-- css generated from http://tablestyler.com/ --> | |
<apex:stylesheet value="{!URLFOR($Resource.table, 'table.css')}"/> | |
<apex:remoteObjects > | |
<apex:remoteObjectModel jsShortHand="con" name="Contact" fields="Id,Name,Email,Phone,MobilePhone,Image_URL__c" | |
retrieve="{!$RemoteAction.ContactsController.retrieveContacts}"/> | |
</apex:remoteObjects> | |
<script> | |
var $j = jQuery.noConflict(); | |
var offsetVal = 0; | |
var limitVal = 5; | |
var totalContacts = limitVal; | |
var rowCount = 1; | |
var scrollListener = function() { | |
$j(window).one("scroll", function() { | |
if ($j(window).scrollTop() >= $j(document).height() - $j(window).height() - 100) { | |
if (offsetVal < totalContacts) { | |
loadRecords(); | |
} | |
} | |
setTimeout(scrollListener, 200); | |
}); | |
}; | |
$j(document).ready(function() { | |
loadRecords(); | |
scrollListener(); | |
}); | |
function loadRecords() { | |
var criteria = { | |
limit: limitVal, | |
orderby: [ {Name: "ASC"} ], | |
}; | |
// offsetVal must be at least 1 | |
if (offsetVal >= 1) { | |
criteria.offset = offsetVal; | |
} | |
var con = new SObjectModel.con(); | |
con.retrieve(criteria, function(err, records, event) { | |
if (err) { | |
alert(err); | |
} else { | |
offsetVal += records.length; | |
totalContacts = event.result.totalContacts; | |
records.forEach(function(record) { | |
addTableRow(record); | |
}); | |
} | |
}); | |
} | |
function addTableRow(record) { | |
var cssClass = ""; | |
if ( (rowCount % 2) == 0 ) { | |
cssClass = "alt"; | |
} | |
$j("#contacts tbody:last").append( | |
"<tr class='" + cssClass + "'>" + | |
"<td><img src='" + record.get("Image_URL__c") + "' /></td>" + | |
"<td><a href='/" + record.get("Id") + "' target='_blank'>" + record.get("Name") + "</a></td>" + | |
"<td>" + record.get("Email") + "</td>" + | |
"<td>" + record.get("MobilePhone") + "</td>" + | |
"<td>" + record.get("Phone") + "</td>" + | |
"</tr>" | |
); | |
rowCount++; | |
} | |
</script> | |
<div class="datagrid"> | |
<table id="contacts"> | |
<thead> | |
<tr> | |
<th>Image</th> <th>Name</th> <th>Email</th> <th>Home Phone</th> <th>Mobile Phone</th> | |
</tr> | |
</thead> | |
<tbody> </tbody> | |
</table> | |
</div> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* From http://tablestyler.com/ */ | |
.datagrid table { | |
border-collapse: collapse; | |
text-align: left; | |
width: 100%; | |
} | |
.datagrid { | |
font: normal 12px/150% Arial, Helvetica, sans-serif; | |
background: #fff; | |
overflow: hidden; | |
border: 1px solid #006699; | |
-webkit-border-radius: 3px; | |
-moz-border-radius: 3px; | |
border-radius: 3px; | |
} | |
.datagrid table td, .datagrid table th { | |
padding: 3px 10px; | |
} | |
.datagrid table thead th { | |
background: -webkit-gradient( linear, left top, left bottom, color-stop(0.05, #006699), color-stop(1, #00557F) ); | |
background: -moz-linear-gradient( center top, #006699 5%, #00557F 100% ); | |
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#006699', endColorstr='#00557F'); | |
background-color: #006699; | |
color: #FFFFFF; | |
font-size: 15px; | |
font-weight: bold; | |
border-left: 1px solid #0070A8; | |
} | |
.datagrid table thead th:first-child { | |
border: none; | |
} | |
.datagrid table tbody td { | |
color: #00496B; | |
border-left: 1px solid #E1EEF4; | |
font-size: 12px; | |
font-weight: normal; | |
} | |
.datagrid table tbody .alt td { | |
background: #E1EEF4; | |
color: #00496B; | |
} | |
.datagrid table tbody td:first-child { | |
border-left: none; | |
} | |
.datagrid table tbody tr:last-child td { | |
border-bottom: none; | |
} | |
.datagrid table tbody tr td:first-child { | |
text-align: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment