Código de JavaScript - Clase para controlar la paginación

Imágen de perfil
Val: 2.288
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Clase para controlar la paginacióngráfica de visualizaciones


JavaScript

Publicado el 1 de Diciembre del 2019 por Katas (200 códigos)
2.284 visualizaciones desde el 1 de Diciembre del 2019
Crear la clase PaginationHelper, que es la clase utilizada para consultar información de paginación relacionada con una matriz.
La clase está diseñada para incorporar una matriz de valores y un número entero que indica cuántos elementos se mostrarán por cada página. Los tipos de valores contenidos en la colección / matriz no son relevantes.

1
2
3
4
5
6
7
8
9
10
11
12
var helper = new PaginationHelper(['a','b','c','d','e','f'], 4);
helper.pageCount(); // 2
helper.itemCount(); // 6
helper.pageItemCount(0); // 4
helper.pageItemCount(1); // last page 2
helper.pageItemCount(2); // -1 since the page is invalid
 
// pageIndex takes an item index and returns the page that it belongs on
helper.pageIndex(5); // 1 (zero based index)
helper.pageIndex(2); // 0
helper.pageIndex(20); // -1
helper.pageIndex(-10); // -1

Utilizando class

Publicado el 1 de Diciembre del 2019gráfica de visualizaciones de la versión: Utilizando class
225 visualizaciones desde el 1 de Diciembre del 2019

Utilizando prototype
estrellaestrellaestrellaestrellaestrella(1)

Publicado el 1 de Diciembre del 2019gráfica de visualizaciones de la versión: Utilizando prototype
2.060 visualizaciones desde el 1 de Diciembre del 2019
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// The constructor takes in an array of items and a integer indicating how many
// items fit within a single page
function PaginationHelper(collection, itemsPerPage){
    this.collection=collection;
    this.itemsPerPage=itemsPerPage;
}
 
// returns the number of items within the entire collection
PaginationHelper.prototype.itemCount = function() {
    return this.collection.length;
}
 
// returns the number of pages
PaginationHelper.prototype.pageCount = function() {
    return Math.ceil(this.collection.length/this.itemsPerPage);
}
 
// returns the number of items on the current page. page_index is zero based.
// this method should return -1 for pageIndex values that are out of range
PaginationHelper.prototype.pageItemCount = function(pageIndex) {
    if (pageIndex<this.pageCount()) {
        return Math.min(this.itemsPerPage, this.itemCount()-(pageIndex*this.itemsPerPage));
    }
    return -1;
}
 
// determines what page an item is on. Zero based indexes
// this method should return -1 for itemIndex values that are out of range
PaginationHelper.prototype.pageIndex = function(itemIndex) {
    if (itemIndex<this.itemCount() && itemIndex>=0) {
        return Math.floor(itemIndex/this.itemsPerPage);
    }
    return -1;
}



Comentarios sobre la versión: Utilizando prototype (1)

Imágen de perfil
11 de Diciembre del 2019
estrellaestrellaestrellaestrellaestrella
Muy bueno!!!
Responder

Comentar la versión: Utilizando prototype

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s5696