Angular and mysql paging logic

Asked

Viewed 300 times

1

I would like to know how to implement a database paging logic using mysql and angular on the front end with ui-bootstrap or dirPagination. For example, taking 10 out of 10 in the bank using LIMIT and OFFSET. My doubt is in the implementation of logic at the front end. It can be a simple explanation and some example.

Since it’s an API, I’m not sure about endpoints either. Would they look like the examples below? I’d like an example as well.

GET /recursos?offset=10&limit=10
GET /recursos/limit/:limit/offset/:offset

1 answer

2


Usually I use these parameters as query params so they are optional and I always return with a default value if no parameter is passed, type 1st page with 10 items.

GET /recursos?pagina=1&total=10

Receiving these parameters on the back, do:

int offset = (pagina < 1 ? 0 : pagina-1) * total;
int limit = offset + total

Remember to return the total amount of items in the bank by making a COUNT no limit and offset for your front knowing how much it can still order from the back.

A good component that already solves this at the front is the ngTable, where you only need to inform which current page, the total of items that can be returned and the quantities and items per page.

  • It worked, @Giovane, thank you. It’s just bringing a little more record, but it must be some error in my logic, I did using dirPagination because I couldn’t use ng-table. How do I add the total of items in the json answer? I’m using Node.js and the return is like this: return res.status(200).json(result);

  • 1

    Query with a Count after doing it paged. So you will have the total number of records, then in return send an object with the total and the paged results. Ex: return res.status(200).json({ total: countResult, conteudo: 'result' });

  • 1

    Output result as string pq my cel auto corrected. result is your result object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.