I was trying to put a pagination on my table, after a help I partially solved the problem, but it did not stay the way I expected.
I can already get the currentPage
with each click on the < pagination>
the problem is on how to load the data when page mute. The old form was using setFirstResult
and setMaxResults
of Hibernate
but when I clicked on the next page it would always repeat the last line of the previous page on the new page.
What would be the best way to get the data according to the clicked page, what do I pass as a parameter for this query?
Front-end
Controller:
angular.module("oraculo").controller("colaboradorController", function($scope, $routeParams, $location, colaboradorAPI, colaboradores){
$scope.tamanhoMaximo = 6;
$scope.currentPage = 1;
$scope.totalItems = 60;
//Onde fica armazenada toda a consulta
$scope.colaboradores = colaboradores.data;
...
Page:
...
</table>
<pagination total-items="totalItems" ng-model="currentPage" max-size="tamanhoMaximo" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages" ng-click="loading(currentPage)"></pagination>
Back-end
DAO:
@SuppressWarnings("unchecked")
public List<Colaborador> listarColaboradores() {
List<Colaborador> lista = new ArrayList<>();
Session sessao = HibernateUtil.getSessionFactory().openSession();
Transaction transacao = null;
try {
transacao = sessao.beginTransaction();
Query consulta = sessao.getNamedQuery("Colaborador.listar");
lista = consulta.list();
transacao.commit();
} catch (RuntimeException ex) {
ex.printStackTrace();
throw ex;
} finally {
sessao.close();
}
return lista;
}
I was doing this way earlier:
DAO:
@SuppressWarnings("unchecked")
public List<Colaborador> listarColaboradores(Integer paginaInicio, Integer count) {
List<Colaborador> lista = new ArrayList<>();
Session sessao = HibernateUtil.getSessionFactory().openSession();
Transaction transacao = null;
try {
transacao = sessao.beginTransaction();
Query consulta = sessao.getNamedQuery("Colaborador.listar");
consulta.setFirstResult(paginaInicio);
consulta.setMaxResults(count);
lista = consulta.list();
transacao.commit();
} catch (RuntimeException ex) {
ex.printStackTrace();
throw ex;
} finally {
sessao.close();
}
return lista;
}
Controller:
@Get
@Path("/colaboradores/{paginaInicio}")
public void listarTodos(Integer paginaInicio) {
result.use(Results.json())
.withoutRoot()
.from(colaboradorDAO.listarColaboradores(paginaInicio, 4))
.serialize();
}
No Front-end :
Controller:
$scope.loading = function(currentPage){
console.log("currentPage: "+currentPage);
colaboradorAPI.getColaboradores(currentPage).success(function(data){
$scope.colaboradores = data;
});
}
Service:
var _getColaboradores = function(paginaInicio){
return $http.get(config.baseURL + "/Oraculo/colaborador/colaboradores/" + paginaInicio);
};
In this way I call the function loading
in ng-click
of pagination
, and pass currentPage
. Then I pass currentPage
as a parameter to back-end
.
My Project: link