Last active
October 10, 2015 09:21
-
-
Save thysultan/1f91ba6a97a3402902b1 to your computer and use it in GitHub Desktop.
$.ajaxPagination
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
$.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); | |
}); | |
}; |
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
<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"> |
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
<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