Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Array Pagination in JS

const dataList = [
    'a',
    'b',
    'c',
    'd',
    'e',
    '1',
    '2',
    '3',
    '4',
    '5'
]

function paginateArray(arr , itemPerPage , pageIndex) {
    const lastIndex = itemPerPage * pageIndex;
    const firstIndex = lastIndex - itemPerPage;
    return arr.slice(firstIndex , lastIndex);
}

console.log(paginateArray(dataList , 5 , 2));
Comment

Paginate array in JavaScript

var host = 'https://' + document.location.hostname
var pages = [host + '/index.html', host + '/index2.html', host + '/index3.html']

var behind = document.getElementById('behind')
var forward = document.getElementById('forward')

function prev(current, pages) {
  var index = pages.indexOf(current)
  if (index === 0) {
    return pages[pages.length - 1]
  }
  return pages[index - 1]
}

function next(current, pages) {
  var index = pages.indexOf(current)
  if (index === pages.length - 1) {
    return pages[0]
  }
  return pages[index + 1]
}

behind.addEventListener('click', function () {
  var newUrl = prev(host + window.location.pathname, pages)
  window.location.href = newUrl
})

forward.addEventListener('click', function () {
  var newUrl = next(host + window.location.pathname, pages)
  window.location.href = newUrl
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: Warning: Prop `className` did not match. Client and server rendered different classes . 
Javascript :: remove an element from an array javascript 
Javascript :: convert long date to short date javascript 
Javascript :: javascript is int in array 
Javascript :: find Array of value in JSON 
Javascript :: import applymiddleware 
Javascript :: how to remove duplicate array object in javascript 
Javascript :: javascript round to 1 decimal 
Javascript :: is java and javascript the same 
Javascript :: window onscroll position fixed position in jquery 
Javascript :: npm http status codes 
Javascript :: using index of on array of objects 
Javascript :: getitem localstorage 
Javascript :: contains whitespace js function 
Javascript :: react not getting img by src 
Javascript :: how to include a css file in jsp 
Javascript :: nested loops javascript 
Javascript :: anonymous function jquery 
Javascript :: set min date field to current date 
Javascript :: javascript add 1 day to new date 
Javascript :: javascript operator double pipes 
Javascript :: update many mongoose 
Javascript :: toggle class javascript 
Javascript :: generate random date 
Javascript :: js calculate date difference 
Javascript :: get public url as laravel asset() in jquery 
Javascript :: libraries like html-react-parser 
Javascript :: react material ui classname 
Javascript :: two sum javascript 
Javascript :: stylelint init 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =