2017-01-27 76 views
0

我想要一個表分頁,並在表頭中同時搜索框。因此,使用此代碼jsfiddle pagination過濾表與分頁

我想輸出是這樣的: enter image description here

這裏是我的過濾代碼:

function searchCabinet() { 
    var input, filter, table, tr, td, i; 
    input = document.getElementById("searchCabinet"); 
    filter = input.value.toUpperCase(); 

    table = document.getElementById("myTable"); 
    tr = table.getElementsByTagName("tr"); 
     for (i = 0; i < tr.length; i++) { 
     td = tr[i].getElementsByTagName("td")[0]; 
     if (td) { 
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { 
      tr[i].style.display = ""; 
      } else { 
      tr[i].style.display = "none"; 
      } 
     }  
    } 
} 

回答

0

每個分頁過濾後的行這樣的總數:

$table.find('tbody tr').hide(); 
$filteredRows = $table.find('tbody tr').filter(function(i, tr) { 
    return $('#search').val() != "" ? $(tr).find("td").get().map(function(td) { 
    return $(td).text(); 
    }).filter(function(td){ 
    return td.indexOf($('#search').val()) != -1; 
    }).length > 0 : true; 
}); 

然後顯示當前頁的行:

$filteredRows.slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show(); 

,然後更改頁數:

var numRows = $filteredRows.length; 
var numPages = Math.ceil(numRows/numPerPage); 

檢查工作fiddle

+0

搜索框不能正常工作 – Kyrie

+0

你的意思不工作是什麼......這表明該行如果輸入的是什麼?在至少1列中找到 – Vanojx1