2
I was trying to put a pagination in my table, after a help I managed to partially solve the problem, but it was not the way I expected.
I can get the currentPage
every click on <pagination>
the problem is how to load the data when I change page. The old way was using setFirstResult
and setMaxResults
of Hibernate
but when I clicked on the next page it always repeated the last line of the previous page on the new page.
What would be the best way to get the data according to the page clicked, what I pass as parameter to 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 it 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();
}
In the 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);
};
That’s how I call function loading
in the ng-click
of pagination
, and step the currentPage
. Then I pass the currentPage
as a parameter for the back-end
.
My Project: https://github.com/sinkz/Angular
You can post how you are sending the values of setFirstResult and setMaxResults in the first three pages?
– Emir Marques