Dynamic Paging with Angular Table

0

I have the following scenario:

IretrievethisdatafromaRestAPIwithVraptor.Iwouldliketodisplayonlytwoitemsperpage,butIdonotknowwheretostart.Iimplementedthispagingusingthe ui-bootstrap .

How do I make a dynamic query and display only two items per page?

Back-end:

Controller:

@Get
@Path("/colaboradores")
public void listarTodos() {
        result.use(Results.json())
        .withoutRoot()
        .from(colaboradorDAO.listarColaboradores())
        .serialize();
}

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;
    }

Front-end:

Controller:

angular.module("oraculo").controller("colaboradorController", function($scope, $routeParams, $location, colaboradorAPI){

 $scope.tamanhoMaximo = 6;
 $scope.totalItems = 175;
 $scope.currentPage = 1;

 ...//Código Omitido

Page:

...//Trecho omitido
</table>
<pagination total-items="totalItems" ng-model="currentPage" max-size="tamanhoMaximo" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></pagination>
<pre>Page: {{currentPage}} / {{numPages}}</pre>
    
asked by anonymous 23.09.2015 / 16:43

1 answer

2

Do this:

public List<Colaborador> listarColaboradores(Integer paginaInicio, Integer paginaFim) {
    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(paginaFim);

        lista = consulta.list();
        transacao.commit();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
        throw ex;
    } finally {
        sessao.close();
    }
    return lista;
}

In your service you can pass two parameters defining the beginning and end of the page. In your case, for example, it would be in the first query: 0 and 2, in the second query: 2 and 4 and so on.

    
23.09.2015 / 17:09