Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any easy way to output the paginator as json? #262

Closed
zspine opened this issue Nov 21, 2018 · 8 comments
Closed

Any easy way to output the paginator as json? #262

zspine opened this issue Nov 21, 2018 · 8 comments

Comments

@zspine
Copy link

zspine commented Nov 21, 2018

Hi

Is there any helper method to output the paginator properties in array or json format? similar to Zend Paginator toJson method with JsonSerializable support? my current workaround:

    public function toArray(Pagerfanta $pagerfanta)
    {
        $items = (array) $pagerfanta->getCurrentPageResults();

        return [
            'items'             => $items,
            'count_results'     => $pagerfanta->getNbResults(),
            'current_page'      => $pagerfanta->getCurrentPage(),
            'number_of_pages'   => $pagerfanta->getNbPages(),
            'next'              => ($pagerfanta->hasNextPage()) ? $pagerfanta->getNextPage() : null,
            'prev'              => ($pagerfanta->hasPreviousPage()) ? $pagerfanta->getPreviousPage() : null,
            'paginate'          => $pagerfanta->haveToPaginate(),
        ];
    }
@sampart
Copy link
Collaborator

sampart commented Nov 21, 2018

Hi @zspine, thanks for getting in touch. There's not a method which does what you describe. Could you tell us more about your use-case for this so that we can see whether it's something to consider adding into the project itself? Your workaround looks pretty good to me.

(You can also use iterator_to_array($pagerfanta->getCurrentPageResults()) if you'd rather do that than casting.)

@zspine
Copy link
Author

zspine commented Nov 22, 2018

Hi @sampart, Thanks a lot for the quick response :). I am using the WhiteOctoberPagerfantaBundle in a symfony project and front-end uses angularjs (SPA). So all the request and response handled through json.

Possible use cases

  1. API (XML/JSON)
  2. SPA (Single Page Application)

Thanks again for pointing out the iterator_to_array, much better than casting.

Cheers!

@sampart
Copy link
Collaborator

sampart commented Nov 22, 2018

Hi @zspine, thanks for explaining your use-case. You're right - a method like this would help people using javascript frameworks. How would you feel about making a PR to incorporate the method you've created (and updating the README and tests to cover it)?

@stof
Copy link
Contributor

stof commented Nov 22, 2018

anyone building an API probably relies on some serializer (because items also needs to be turned into array/json), and they could configure the handling of a Pagerfanta object there. I'm not sure the helper method used here would actually be useful for that case.

@zspine
Copy link
Author

zspine commented Nov 23, 2018

@sampart Hi, PR am more than happy to do it... as @stof suggested it may not be useful in API scenarios, so now I am not sure whether adding this to the main project is a good idea or not, maybe the actual use cases are very rare. Another option is we can just update the document with an example and avoid incorporating in to project. Any suggestions?

@sampart
Copy link
Collaborator

sampart commented Nov 28, 2018

@zspine. I think @stof makes a good point; I agree a PR for a helper method isn't necessary. I'm not sure that we need a documentation update either - the details of the method people need will vary, and shouldn't be that complicated in any case.

Did you hit any particular difficulties or points of confusion when making your method? If so, we could think about some specific documentation that would have helped.

@zspine
Copy link
Author

zspine commented Dec 4, 2018

@sampart Sorry for the delayed reply... I agree with your decision.

Well I had few difficulties in the beginning then I got familiar with the library and had no issues. Again it's difficult to tell :) even if something is difficult or not user friendly it becomes normal over time until someone raises the issue again. I raised this issue because I am familiar with Zend and was expecting a similar functionality. So better to watch for demand before making any changes.

P.S. We started to use Pagefanta as the main library for pagination and we really enjoy it, keep up the good work!!!!!

Cheers

@sampart
Copy link
Collaborator

sampart commented Dec 5, 2018

Thanks @zspine, glad you like it! Let's leave this one for now and, as you say, see whether related issues come up from elsewhere. Thanks again for your time here.

@sampart sampart closed this as completed Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants