I have text that shows how many items are being viewed per page based on total items in the bank. Look at an image:
Icreatedan"algorithm" (which I am not thinking legal) to define in a label this text showing that data.
The function receives:
-
currentPage
: Current page whereas 0 is the first page. -
limitItensPerPage
: Limit amount per page, which the bank 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><divid="show-qnd-itens-info"></div>
Knowing that it is possible to elaborate this in several ways, how could I improve this function?