<html>
<head>
<meta name="description" content="Pagination and sorting example for emberjs website" />
<meta charset=utf-8 />
<title>JS Bin</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v2.0.0.js"></script>
<script src="http://builds.emberjs.com/ember-latest.js"></script>
</head>
<body>
<script type="text/x-handlebars" data-template-name="application">
<h2>Query Params: client-side sorting</h2>
<p>
In this example, we're not bothering querying
the server for data but just sorting and
paginating what we
have already loaded on the client-side.
</p>
<p>
<a href="http://jsbin.com/ucanam/2942">See here</a> for an example of
how to opt into a full on transition
that can re-query the server to manage
pagination/sorting on the server side.
</p>
<p>
Page:
{{#each n in pages}}
{{linkTo n (query-params page=n)}}
{{/each}}
of {{pages.length}}
</p>
<p>
{{#if previousPage}}
{{link-to 'Previous' (query-params page=previousPage)}}
{{else}}
Previous
{{/if}}
{{#if nextPage}}
{{link-to 'Next' (query-params page=nextPage)}}
{{else}}
Next
{{/if}}
</p>
<h3>Sorting by {{sortBy}}</h3>
<form {{action 'updatePageSize' on='submit'}}>
Page size:
{{input value=newPageSize type="number"}}
{{input type="submit" value="Change"}}
</form>
<table>
<thead>
<tr>
<th>
{{linkTo "First Name" (query-params sortBy="firstName")}}
</th>
<th>
{{linkTo "Last Name" (query-params sortBy="lastName")}}
</th>
<th>
{{linkTo "Location" (query-params sortBy="location")}}
</th>
</tr>
</thead>
<tbody>
{{#each paged}}
<tr>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
<td>{{location}}</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
</body>
</html>
.active {
font-weight: bold;
}
table {
width: 100%;
}
form {
margin: 1em 0;
}
th {
font-weight: normal;
}
App = Ember.Application.create();
App.ApplicationRoute = Ember.Route.extend({
model: function() {
var FIRST_NAMES = ["Alex", "Kris", "Luke"];
var LAST_NAMES = ["Matchneer", "Selden", "Melia"];
var people = [];
for (var i = 0; i < 400; i++) {
people.push(
{
firstName: FIRST_NAMES[Math.floor(Math.random() * 3)],
lastName: LAST_NAMES[Math.floor(Math.random() * 3)],
location: FIRST_NAMES[Math.floor(Math.random() * 3)] + "ville"
});
}
return people;
}
});
// Helper computed property used by nextPage
// and previousPage.
var incrementPage = function(amt) {
return Ember.computed('page', 'numPages', function() {
var newPage = parseInt(this.get('page'), 10) + amt;
if (newPage <= parseInt(this.get('numPages'), 10) &&
newPage >= 1) {
return newPage;
}
});
};
App.ApplicationController = Ember.ArrayController.extend({
queryParams: ['sortBy', 'page', 'pageSize'],
page: 1,
pageSize: 25,
sortBy: 'firstName',
sortProperties: function() {
return [this.get('sortBy')];
}.property('sortBy'),
pages: function() {
var pageSize = this.get('pageSize'),
l = this.get('model.length'),
pages = Math.ceil(l / pageSize),
pagesArray = [];
for(var i = 0; i < pages; i ++) {
pagesArray.push(i + 1);
}
return pagesArray;
}.property('pageSize', 'model.length'),
numPages: function() {
var pageSize = this.get('pageSize'),
l = this.get('model.length');
return Math.ceil(l / pageSize);
}.property('pageSize'),
paged: function() {
var page = this.get('page') - 1,
pageSize = this.get('pageSize'),
start = page * pageSize,
end = start + pageSize;
return this.get('arrangedContent').slice(start, end);
}.property('page', 'arrangedContent', 'pageSize'),
previousPage: incrementPage(-1),
nextPage: incrementPage(1),
// We don't want updates to the newPageSize
// input field to immediately update `pageSize`
// (and therefore the URL), so we make this
// binding read-only (and later explicitly
// write `pageSize` inside the `updatePageSize`)
newPageSize: Ember.computed.oneWay('pageSize'),
actions: {
updatePageSize: function() {
this.set('pageSize', parseInt(this.get('newPageSize'), 10));
}
}
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |