3

RSS パーサーでページング機能を作成しようとしています。それはほとんど働いています。最後のページだけ間違っています。ここでフィドルを参照してください。最後のページには 11-10 が表示されます。何故ですか?現在、フィードには 10 個のアイテムがあるため、この例では 3 ページではなく 2 ページのみにする必要があります。また、ページ番号 2 では、[次へ] ボタンを非表示にする必要があります。

これは何かが間違っているところですか?

if (numEntriesReturned+1-oRssConfig.contempOffset>0 && oRssConfig.contempOffset<100-oRssConfig.maxItems) $('#btnNext').css("display", "block");
    else $('#btnNext').css("display", "none");
4

2 に答える 2

3

何時間もかけて、スクリプトを完全に書き直しました。これは、Zazar の zRSSFeed プラグインを使用して RSS フィードを読み込んでページ付けするために機能するはずです。何が起こっているのかを見て理解できるように、ほとんどの機能についてコメントしました。

特徴:
表示する「ページごとのフィード」を簡単に選択できます。
RSS フィードへのリンクを簡単に入力できます。
次と前のボタンは、必要に応じて表示/非表示になります。
各ページにフィードカウントを自動的に表示します。例: 「1/5」

このFiddleをチェックして、動作を確認してください。
質問は?

JQuery スクリプト

// Editable Values
var feedlink = 'http://feeds.reuters.com/reuters/oddlyEnoughNews'; // Set Feed Location
var fpp = 4; // Choose how many feeds per page to display (fpp = Feeds Per Page)
var feedview = '#RSSview'; // Choose where to diplay the RSS Feed

// Initial Variables ( Do Not Edit )
var feeds = null; // Variable to hold total feed count
var totalpages = null; // Variable to hold total pages
var currentpage = null; // Variable to hold Current Page being Displayed
var lastpagefeeds = null; // Variable to hold Amount of Feeds on the Last Page
var firstof = null; // Variable to hold First Feed Display - Example: 3 of ?
var lastof = null; // Variable to hold Last Feed Display - Example: ? of 10
var feedoffset = 1; // Set Initial Feed Offset

///////////////////
// RSS Functions //
///////////////////

// Calulate Feed Count Display
function displayfeedcount(){
    // Set 'First Of ???'
    firstof = feedoffset;
    // Set '??? of Last'
    if(currentpage == Math.ceil(totalpages)){ lastof = feeds; }else{ lastof = (firstof + fpp) - 1;}
    $('#offsetDispl').html( firstof + ' of ' + lastof); // Display Feed Count ' First of Last'
}

// Load Initial Feeds on Page 1
function initialfeeds(){
    $(feedview).rssfeed( feedlink , { limit: fpp , offset: 0 }); // Load Initial Set of Feeds
    currentpage = 1; // Set Current Page to 1
    displayfeedcount(); // Trigger the Display of Feedcount - Example: '1 of 5'
}

// Calculate Total Pages
function calculatepages(){
    totalpages = feeds / fpp; // Total Page Calculation
    console.log( 'Total Pages: ' + totalpages); // Log - For Testing Purposes Only - Delete if Desired
    initialfeeds(); // Trigger Initial Display of Feeds
}

// Determine if the NextBtn should be shown on load
function showbuttons(){
    if ( feeds > fpp ){ $('#btnNext').show(); } // Evaluate 'Next Button' Visibility
}

// Determine Total Feed Count
function feedcount() {
    feeds = arguments[1]; // Set Feed Count to Variable 'feeds'
    console.log( 'Total Feeds: ' + feeds ); // Log - For Testing Purposes Only - Delete if Desired
    showbuttons(); // Trigger Initial Button Display
    calculatepages(); // Trigger Total Page Calculation
}

// Function to Show Next Page
function nextpage(){
    currentpage = currentpage + 1; // Set New Current Page
    feedoffset = feedoffset + fpp ; // Set New Feed Offset   
    console.log('Current Page is: ' + currentpage);  // Log - For Testing Purposes Only - Delete if Desired
    console.log('Feed Offset is: ' + feedoffset );  // Log - For Testing Purposes Only - Delete if Desired
    $(feedview).rssfeed( feedlink , { limit: fpp , offset: feedoffset }); // Load Next Set of Feeds
    if( currentpage >= totalpages ){ $('#btnNext').hide();} // Evaluate 'Next Button' Visibility
    if( currentpage > 1){ $('#btnPrev').fadeIn('250');} // Evaluate 'Previous Button' Visibility
    displayfeedcount(); // Display Feed Count ' ??? of ??? '   
}

// Function to Show Previous Page
function prevpage(){
    currentpage = currentpage - 1;  // Set New Current Page
    feedoffset  = feedoffset - fpp ; // Set New Feed Offset
    console.log('Current Page is: ' + currentpage);  // Log - For Testing Purposes Only - Delete if Desired
    console.log('Feed Offset is: ' + feedoffset );  // Log - For Testing Purposes Only - Delete if Desired
    $(feedview).rssfeed( feedlink , { limit: fpp , offset: feedoffset });
    if( currentpage <= totalpages ){ $('#btnNext').fadeIn('250');} // Evaluate 'Next Button' Visibility
    if( currentpage <= 1){ $('#btnPrev').hide();} // Evaluate 'Previous Button' Visibility
    displayfeedcount(); // Display Feed Count ' ??? of ??? '
}

// Bind Previous and Next Button Clicks
$('#btnPrev').on('click', prevpage); // Bind the PrevPage Function
$('#btnNext').on('click', nextpage); // Bind the NextPage Function

// Retrieve Total Feeds
$('#hidden').rssfeed( feedlink , {}, feedcount);
// Make sure this divider exists on the page body >>>  <div id="hidden" style='display:none;'></div>   
于 2012-11-09T14:46:21.523 に答える
0

パラメータnumEntriesReturned値は、最初のページでは 5、2 ページ目では 10 です。最後のページがいっぱいのときは役に立たないと思います。

最後のページが空であることを検出してボタンを削除すると、戻ることができるかもしれません。

于 2012-11-09T14:13:47.307 に答える