Skip to content

Instantly share code, notes, and snippets.

@thysultan
Last active October 10, 2015 09:21
Show Gist options
  • Save thysultan/1f91ba6a97a3402902b1 to your computer and use it in GitHub Desktop.
Save thysultan/1f91ba6a97a3402902b1 to your computer and use it in GitHub Desktop.
$.ajaxPagination
$.ajaxPagination = function(el, container, e){
e.preventDefault();
var oldLink, newLink, selectors, page;
el = $(el);
selectors = {
loading: "loading",
pageName: "data-page-name",
pageNo: "data-page-no"
};
page = {
name: el.attr(selectors.pageName),
no: parseInt( el.attr(selectors.pageNo), 10 ) + 1
};
oldLink = page.name+page.no;
newLink = page.name + ( parseInt( page.no, 10 ) + 1 );
if( el.hasClass(selectors.loading) === true ) return false;
el.addClass(selectors.loading);
$.ajax({
url: oldLink
})
.done(function(data) {
var html = $(data).find(container).html();
$(container).append(html);
el.removeClass(selectors.loading);
el.attr(selectors.pageName, page.name);
el.attr(selectors.pageNo, page.no);
});
};
<script>
$(function(){
$("[data-pageNo]").click(function(e) {
//target, container, event
$.ajaxPagination(this, ".list", e);
});
});
</script>
<a href="#" data-page-name="page/" data-page-no="1">
<ul class="list">
<li>next page content...</li>
<li>...</li>
<li>...</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment