4 MINDS

4MINDS Gestão de Conhecimento

Scala - Algoritimo de paginação





val totalPages = math.ceil(totalCount.toDouble / totalPerPage).toInt
val maxPages = math.min(5, totalPages)
val middle = maxPages / 2
val baseMin = math.max(currPage - middle, 0)
val baseMax = math.min(baseMin + maxPages, totalPages)
val rest = maxPages - (baseMax - baseMin)
val min = math.min(baseMin - rest, baseMin)
val max = math.min(baseMax + rest, totalPages)
val (nextIdx, prevIdx) = (currPage + 1, currPage - 1)
val (hasPrev, hasNext) = (prevIdx >= 0, nextIdx < totalPages)
val pages = (min to max).toSeq