js功能函数:数据分页、翻页

    xiaoxiao2022-07-04  104

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <p onclick="paginationFn(arrList,10,2)">123</p> <script> let arrList = [1, 4, 5, 6, 34, 34, 67, 895, 2456, 87, 9, 5, 23, 884, 56, 463]; window.onlick = arrayGroup(arrList, 5); function paginationFn(arrayList, perPage, current) { // arrayList :需要分页的数组 // perPage:每页显示的数据量 // current:当前页所显示的内容,可以通过修改current来做到翻页 let showArray = [] let startRow = (current - 1) * perPage + 1 let endRow = current * perPage > arrayList.length ? arrayList.length : current * perPage arrayList.filter((e, i) => { if (i + 1 >= startRow && i + 1 <= endRow) { showArray.push(e) } return showArray }) console.log(showArray) } function arrayGroup(list, num) { // list:需要分页的所有的数据 // num:每页需要显示的数据量 var result = new Array(Math.ceil(list.length / num)) for (let i = 0; i < result.length; i++) { result[i] = new Array(); for (var j = 0; j < num; j++) { result[i][j] ? result[i][j] : ''; } } list.forEach((e, i) => { result[parseInt(i / num)][i % num] = list[i] return result }) console.log(result) } </script> </body> </html>
    最新回复(0)