3
I have a text showing how many items are being viewed per page based on the total number of items in the database. See an image:
I created an "algorithm" (which I don’t think is cool) to define in a label this text showing this data.
The function receives:
currentPage
: Current page considering that 0 is the first page.limitItensPerPage
: Limit amount per page, which the database returns at a time.totalItens
: Total amount of items the database has.
I would like to improve this "algorithm" that I did well running. See below the function:
generatePageText(1, 10, 55);
function generatePageText(currentPage, limitItensPerPage, totalItens) {
var rows, lastItem;
var firstItem = (currentPage + 1);
// Esta condição foi criada porque o limitItensPerPage
// é atribuido através de um select option, e em alguns casos
// pode haver um valor maior do que a quantidade de itens
if (limitItensPerPage > totalItens) {
rows = totalItens;
lastItem = totalItens;
} else {
rows = limitItensPerPage;
lastItem = ((limitItensPerPage * firstItem) > totalItens) ? totalItens :
limitItensPerPage * firstItem;
}
$('#show-qnd-itens-info').text("Mostrando itens do " +
((firstItem * rows) - rows + 1) + " ao " + lastItem + " de " + totalItens + ".");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="show-qnd-itens-info"></div>
Knowing that it is possible to elaborate this in various ways, how could I improve this function?
Good Sergio! Great solution! = D
– viana