2010-07-05 162 views
1

我創建了一個谷歌地圖存儲定位器和即時通訊使用jquery分頁,每頁只顯示5個結果。有用。唯一的問題是,每次我搜索它重複分頁。jQuery分頁結果頁面

Screen shot 2010-07-05 at 11.54.13 AM.png

代碼:

function paginate(){ 
var page = 1; 
var itemsPerPage = 4; 
var prev = "Previous Results"; 
var next = "More Results"; 

var $entries = $("#entries"); 
var $list = $entries.children("ul:first"); 
$list.children().eq(itemsPerPage).nextAll().andSelf().hide(); 

var $pagination = $("<ul class='pagination'></ul>").each(function() { 
    var itemsTotal = $list.children().size(); 
    var pages = Math.ceil(itemsTotal/itemsPerPage); 
$(this).append("<span class='endline'></span>"); 
    $(this).append("<li class='prev'>" + prev + "</li>"); 
    $(this).append("<li class='more'>" + next + "</li>"); 
}).appendTo($entries); 
$pagination.children("li:first").hide(); 
$pagination.children().click(function() { 

    if ($(this).text() == prev) 
     page = page - 1; 
    else if ($(this).text() == next) 
     page = page + 1; 

    var firstToShow = (page - 1) * itemsPerPage; 
    var lastToShow = page * itemsPerPage; 
    $list.children().hide().slice(firstToShow, lastToShow).slideDown('slow'); 

    if (page == 1) 
     $(this).parent().find("li:first").hide(); 
    else 
     $(this).parent().find("li:first").show(); 
    if (page == Math.ceil($list.children().size()/itemsPerPage)) 
     $(this).parent().find("li:last").hide(); 
    else 
     $(this).parent().find("li:last").show(); 
}); 
} 

然後在谷歌地圖的JavaScript我有(代碼從Creating a Store Locator with PHP, MySQL & Google Maps取):

function searchLocationsNear(center) { 
var radius = document.getElementById('radiusSelect').value; 
var searchUrl = 'genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius; 
GDownloadUrl(searchUrl, function(data) { 
var xml = GXml.parse(data); 
var markers = xml.documentElement.getElementsByTagName('marker'); 
map.clearOverlays(); 

var sidebar = document.getElementById('sidebar'); 
sidebar.innerHTML = ''; 
if (markers.length == 0) { 
    sidebar.innerHTML = '<p class="caption">No results found.</p>'; 
    map.setCenter(new GLatLng(40, -100), 4); 
    return; 
} 

var bounds = new GLatLngBounds(); 
for (var i = 0; i < markers.length; i++) { 
    var name = markers[i].getAttribute('name'); 
    var address = markers[i].getAttribute('address'); 
    var type = markers[i].getAttribute('type'); 
    var distance = parseFloat(markers[i].getAttribute('distance')); 
    var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')), 
          parseFloat(markers[i].getAttribute('lng'))); 

    var marker = createMarker(point, name, address, type); 
    map.addOverlay(marker); 
    var sidebarEntry = createSidebarEntry(marker, name, address, distance, type); 
    sidebar.appendChild(sidebarEntry); 
    bounds.extend(point); 
} 
paginate(); 
results(); 
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
}); 
    } 

HTML:

<div id="entries"> 
<ul id="sidebar"> 
    <li>all the entries</li> 
    <li>all the entries</li> 
    ...etc 
</ul> 
and then the pagination 
<ul class="pagination"></ul> 
</div> 

,按鈕上有:

<input type="button" onclick="searchLocations()" class="submit" value="Search"/> 

和功能是:

function searchLocations() { 
var address = document.getElementById('addressInput').value; 
geocoder.getLatLng(address, function(latlng) { 
if (!latlng) { 
sidebar.innerHTML = '<p class="caption"><b>No results found.</b><br><br>Please try your search again. Make sure to enter a single city, state, or zip code.</p>'; 
} else { 
    searchLocationsNear(latlng); 
    filter(); 
} 
}); 
} 
+0

您的標記是什麼樣子的,或者您使用id =「entries」設置了哪個元素? – Bermo 2010-07-06 00:47:53

+0

哎呦我知道我忘了東西><

and then the pagination
    Robert 2010-07-06 01:38:51

    +0

    把它加到剩下的對不起:X – Robert 2010-07-06 01:40:56

    回答

    2

    添加以下行,以消除任何現有的分頁中paginate()功能是重新創建之前:

    $(".pagination").remove(); 
    

    所以你最終獲得:

    $(".pagination").remove(); 
    var $pagination = $("<ul class='pagination'></ul>").each(function() { 
        ... 
    }).appendTo($entries); 
    
    +0

    我擺脫了整個功能嗎? – Robert 2010-07-06 01:52:21

    +0

    哦,不,不,它實際上並不在標記中,我只是在頁面呈現後顯示它顯示的位置。 – Robert 2010-07-06 02:02:37

    +0

    對不起,我沒有意識到這是呈現的標記 - 我已經修改了,它應該可以解決您的問題。 – Bermo 2010-07-06 02:31:21