<li> <a href=""

时间:2016-10-11 10:11:59

标签: javascript pagination

="" I'm trying to make a simple (only next previous buttons) javascript pagination for my <li> links. this is my link list :

<div class="panel-body">
    <div id="listingTable">
        <ul id = "Mylinks" class="paging">
            <li style="display: none;"><a href="http://site1.com" rel="nofollow">Description1</a></li>
            <li style="display: none;"><a href="http://site2.com" rel="nofollow">Description2</a></li>
            <li style="display: none;"><a href="http://site3.com" rel="nofollow">Description3</a></li>
            <li style="display: none;"><a href="http://site4.com" rel="nofollow">Description4</a></li>
            <li style="display: none;"><a href="http://site5.com" rel="nofollow">Description5</a></li>
            <li style="display: none;"><a href="http://site6.com" rel="nofollow">Description6</a></li>
            <li style="display: none;"><a href="http://site7.com" rel="nofollow">Description7</a></li>
            <li style="display: none;"><a href="http://site100.com" rel="nofollow">Description100</a></li>
            //Number of links is unlimited.
        </ul>
    </div>
    <ul class="pager">
      <li><a href="javascript:prevPage()" id="btn_prev">previous</a></li>
      <li><a href="javascript:nextPage()" id="btn_next">next</a></li>
    </ul>
</div>

I'm Using bootstrap css. I don't want to use jquery. my code is nearly working.

 var current_page = 1;
    var records_per_page = 3;

    function prevPage()
    {
        if (current_page > 1) {
            current_page--;
            changePage(current_page);
        }
    }

    function nextPage()
    {
        if (current_page < numPages()) {
            current_page++;
            changePage(current_page);
        }
    }

    function changePage(page)
    {
        var btn_next = document.getElementById("btn_next");
        var btn_prev = document.getElementById("btn_prev");
        var listing_table = document.getElementById("listingTable");
        var page_span = document.getElementById("page");

        // Validate page
        if (page < 1) page = 1;
        if (page > numPages()) page = numPages();

var list = document.getElementById('linkbox'),items = list.childNodes;
    for (var i = (page-1) * records_per_page; i < (page * records_per_page) && i < childNodes.length; i++) {
        items[i].style.display = "block";
    }
        }
        page_span.innerHTML = page + "/" + numPages();

        if (page == 1) {
            btn_prev.style.visibility = "hidden";
        } else {
            btn_prev.style.visibility = "visible";
        }

        if (page == numPages()) {
            btn_next.style.visibility = "hidden";
        } else {
            btn_next.style.visibility = "visible";
        }
    }

    function numPages()
    {
        return Math.ceil(objJson.length / records_per_page);
    }

    window.onload = function() {
        changePage(1);
    };

But the problem is var objJson = [] I want to use style="display: block;" for page1 and put style="display: none;" for rest of links. when page changes style changes too.

Updated:

I put my links Directly inside <ul> I think it is not good Idea to put them in java script and show only parts of them, Difference is : google consider all links inside <ul></ul> but when I put them in javascript only page1 is in <ul></ul>. I should get all links by :

var list = document.getElementById('Mylinks'),
    items = list.childNodes;

for (var i = 0, length = childNodes.length; i < length; i++)
{
    if (items[i].nodeType != 1) {
       continue;
    }
    items[i].style.display = "block"; 
}

and update styles for page 1 to style="display: block;"

1 个答案:

答案 0 :(得分:0)

如果我理解了你想要的东西,那么说IDisposable代表一页会更容易。

根据这个你可以做这样的事情:

<ul>
var current_page = 1;
var records_per_page = 3;

var objJson = [
    { adName: "AdName 1"},
    { adName: "AdName 2"},
    { adName: "AdName 3"},
    { adName: "AdName 4"},
    { adName: "AdName 5"},
    { adName: "AdName 6"},
    { adName: "AdName 7"},
    { adName: "AdName 8"},
    { adName: "AdName 9"},
    { adName: "AdName 10"}
]; // Can be obtained from another source, such as your objJson variable

function prevPage()
{
    if (current_page > 1) {
        current_page--;
        changePage(current_page);
    }
}

function nextPage()
{
    if (current_page < numPages()) {
        current_page++;
        changePage(current_page);
    }
}

function changePage(page)
{
    var btn_next = document.getElementById("btn_next");
    var btn_prev = document.getElementById("btn_prev");
    var page_span = document.getElementById("page");

    // Validate page
    if (page < 1) page = 1;
    if (page > numPages()) page = numPages();

    if(document.querySelector('.current')) document.querySelector('.current').classList.remove('current');
    document.getElementById('page'+page).classList.add('current')
    
    page_span.innerHTML = page + "/" + numPages();

    if (page == 1) {
        btn_prev.style.visibility = "hidden";
    } else {
        btn_prev.style.visibility = "visible";
    }

    if (page == numPages()) {
        btn_next.style.visibility = "hidden";
    } else {
        btn_next.style.visibility = "visible";
    }
}

function numPages()
{
    return Math.ceil(objJson.length / records_per_page);
}

function initPages() {
  var ul;
  var container = document.getElementById("listingTable");
  for(var i = 0; i<objJson.length; i++) {
    if(i%records_per_page === 0){
      ul = document.createElement('ul');
      ul.className = "paging";
      ul.id = "page"+ ((i/records_per_page)+1);
      container.appendChild(ul);
    }
    
    var li = document.createElement('li');
    var a = document.createElement('a');
    a.href = '#';
    a.innerHTML = objJson[i].adName;
    li.appendChild(a);
    ul.appendChild(li);
  }
  changePage(1);
}

window.onload = function() {
    initPages();
    
};
.paging {
  display: none; 
}

.paging.current{
  display: block;
}