How to jump to specific page using Query Cursor?

試著忘記壹切 提交于 2019-12-01 10:55:24

问题


I am developing my website in python(webapp2) and google data store in back end. I have added query cursor for pagination and it's working good but it has only next and previous functions for pagination, the question is that how i will jump to specific page like i am on page 1 and i want to jump to page 3, how i will manage it in Query Cursor?

i have also visited below links but didn't find any solution https://www.the-swamp.info/blog/pagination-google-app-engine/

https://cloud.google.com/appengine/docs/standard/python/datastore/query-cursors


回答1:


You can't. Datastore doesn't know the number of results found by your query.

You can however use some tricks to simulate a full pagination. For example one technique is to generate a certain number of cursors in a loop and generating the "last page" one.

Like in the following pseudo-code:

(results, next_curs, more) = model.query....fetch_page(...)

for p in xrange(5):
    # generate cursor for page number 'b' 
    # by using the next_cursor from previous page
    (results, next_curs, more) = model.....fetch_page(cursor=next_cursor,...)

As you don't access the results cursor the performance is somewhat acceptable (depending on your models complexity, query complexity and such). You can tweak it on your data.



来源:https://stackoverflow.com/questions/43908888/how-to-jump-to-specific-page-using-query-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!