开发者

Can someone please share a extremely simple version of JQuery Ajax with pagination?

开发者 https://www.devze.com 2022-12-26 23:45 出处:网络
Can someone please share a extremely simple version of JQuery Ajax w开发者_StackOverflow社区ith pagination??I would suggest using the server to take care of pagination, as in it should respond with th

Can someone please share a extremely simple version of JQuery Ajax w开发者_StackOverflow社区ith pagination??


I would suggest using the server to take care of pagination, as in it should respond with the correct output based on the received pageNo parameter:

$(document).ready(function() {
    $("a").click(function() {
        $("#results").load( "foo.php", { pageNo: $(this).text()}, sortBy: $("#sortBy").val() );
        return false;
    });
});

<select id="sortBy">
    <option value="date">Added</option>
    <option value="price">Price</option>
</select>

<div id="results"></div>

<a href="foo.php?p=1">1</a>
<a href="foo.php?p=2">2</a>

Note: When an object is passed as the second argument to $.load, a POST request will be made. See http://api.jquery.com/load/

EDIT: The same as above, but fetching JSON from the server:

$(document).ready(function() {
    $("a").click(function() {
        $.getJSON( "foo.php", { pageNo: $(this).text(), sortBy: $("#sortBy").val() }, function(json) {
            // read json here, possibly using $.each
        });
        return false;
    });
});

EDIT(again): I would recommend using the server to render in the initial content. If you insist on requesting the first page of results with ajax, one way is to simulate the clicking of the link for page 1:

$("a:first").click();

better you give your pagination links a class, say .pageNo, to eliminate any confusion:

$("a.pageNo:first").click();


http://plugins.jquery.com/project/pagination

0

精彩评论

暂无评论...
验证码 换一张
取 消