2010-11-28 93 views
4

我想创建一个表格,当超过10行时,我想创建一个超链接,告诉用户去下一页。这个概念被称为分页,但我怎样才能实现它jQuery/JavaScriptjQuery分页HTML表格

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
     <title>Table</title> 
     <style type="text/css"> 
      th { 
       background-color: #ddd; 
      } 
      th td { 
       border: 1px solid black; 
      } 
     </style> 
    </head> 
    <body> 
     <table> 
      <th>Heading1</th> 
      <th>Heading2</th> 
      <tbody> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr><td>This is td</td><td>This is td</td></tr> 
       <tr></tr> 
      </tbody> 
     </table> 
    </body> 
</html> 
+0

您使用的是服务器端语言吗? – karim79 2010-11-28 00:54:59

回答

16

或者给插件,如果你想看到简化的代码,所以你可以教育自己,以分页是如何工作的,看看这个小提琴我敲了你。

http://jsfiddle.net/29W9Q/

的代码只是结合两个按钮,一个和下一个改变您指定的表行的可见性。每次点击一个按钮时,步骤是:查看您是否可以向后或向前移动,隐藏当前行,找到应该可见的行,向上或向下10行,然后使其可见。其余的代码是为了说明这个例子。

真正的jQuery的工作正在由less-thangreater-than选择做::lt():gt(),选择隐藏/显示行。

var maxRows = 10; 
$('.paginated-table').each(function() { 
    var cTable = $(this); 
    var cRows = cTable.find('tr:gt(0)'); 
    var cRowCount = cRows.size(); 

    if (cRowCount < maxRows) { 
     return; 
    } 

    /* add numbers to the rows for visuals on the demo */ 
    cRows.each(function(i) { 
     $(this).find('td:first').text(function(j, val) { 
      return (i + 1) + " - " + val; 
     }); 
    }); 

    /* hide all rows above the max initially */ 
    cRows.filter(':gt(' + (maxRows - 1) + ')').hide(); 

    var cPrev = cTable.siblings('.prev'); 
    var cNext = cTable.siblings('.next'); 

    /* start with previous disabled */ 
    cPrev.addClass('disabled'); 

    cPrev.click(function() { 
     var cFirstVisible = cRows.index(cRows.filter(':visible')); 

     if (cPrev.hasClass('disabled')) { 
      return false; 
     } 

     cRows.hide(); 
     if (cFirstVisible - maxRows - 1 > 0) { 
      cRows.filter(':lt(' + cFirstVisible + '):gt(' + (cFirstVisible - maxRows - 1) + ')').show(); 
     } else { 
      cRows.filter(':lt(' + cFirstVisible + ')').show(); 
     } 

     if (cFirstVisible - maxRows <= 0) { 
      cPrev.addClass('disabled'); 
     } 

     cNext.removeClass('disabled'); 

     return false; 
    }); 

    cNext.click(function() { 
     var cFirstVisible = cRows.index(cRows.filter(':visible')); 

     if (cNext.hasClass('disabled')) { 
      return false; 
     } 

     cRows.hide(); 
     cRows.filter(':lt(' + (cFirstVisible +2 * maxRows) + '):gt(' + (cFirstVisible + maxRows - 1) + ')').show(); 

     if (cFirstVisible + 2 * maxRows >= cRows.size()) { 
      cNext.addClass('disabled'); 
     } 

     cPrev.removeClass('disabled'); 

     return false; 
    }); 

}); 
+3

+1这非常有帮助。我讨厌使用插件,除非绝对必要,因为我喜欢充分定制代码的能力,而不必使用他们制作的代码。 – chromedude 2010-11-28 03:21:17