javascript Parse.com:如何对 Parse.Query 的结果进行分页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20950648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:41:55  来源:igfitidea点击:

Parse.com: How to paginate results from a Parse.Query?

javascriptbackbone.jspaginationunderscore.jsparse-platform

提问by Hendrik Vlaanderen

Currently, I am querying results with the Javascript Parse.Object.extend, and templating these results in a list with underscoreJS.

目前,我正在使用 Javascript Parse.Object.extend 查询结果,并使用 underscoreJS 将这些结果模板化到列表中。

Here is the code that queries the Parse Object and adds the objects to a Underscore template.

这是查询 Parse 对象并将对象添加到 Underscore 模板的代码。

var Assignment = Parse.Object.extend("Assignments");
var query = new Parse.Query(Assignment);
query.descending('updatedAt');  

query.find({
    success: function(results) {
        console.log("Success");
            var tableTemplate = $("#list-template").html();
$("#assignmentdisplay").html(_.template(tableTemplate,{results:results}));

    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
});

And this is the Underscore template.

这是下划线模板。

<script type="text/html" id='list-template'>
                <% _.each(results,function(result){ %>
            <li id="list-group-item">
        <h4 class="list-group-item-heading"><%= result.get("Title") %></h4>
                    <p class="list-group-item-text"><%= result.get("Content") %></p>
                    <p class="list-group-item-text"><%= result.get("Categories") %></p>
                    </li>
    <% }) %>

</script>

However, I do not understand how to paginate the results in Parse and Underscore.

但是,我不明白如何在 Parse 和 Underscore 中对结果进行分页。

I've tried the backbone paginator, but I am not great with Backbone, and I just don't understand how to combine it with the Parse queries.

我已经尝试过主干分页器,但我对 Backbone 不是很好,我只是不明白如何将它与 Parse 查询结合起来。

If I have to use another templating solution, or another pagination solution besides backbone paginator, it is also fine. Anything will be helpful, I am quite stuck with this pagination.

如果我必须使用其他模板解决方案,或者除了主干分页器之外的其他分页解决方案,也可以。任何事情都会有帮助,我非常坚持这个分页。

EDIT:

编辑:

Parse.com has skip() and limit(), somehow this is said to be useful, but I don't know how to implement it.

Parse.com 有skip() 和limit(),不知怎么的,据说这很有用,但我不知道如何实现它。

回答by jmk2142

Here's just an abstracted example of pagination. The concept should be illustrated and you can adjust it to fit your specific situation.

这里只是一个抽象的分页示例。这个概念应该被说明,你可以调整它以适应你的具体情况。

// Set the current page number usually zero works great to start
// This is an arbitrary ex. but this value will come from your page (e.g. link etc.)
var page = 0;

// How much you want on a page
var displayLimit = 50;

// Get the count on a collection
var count;
myCollection.count().then(function(result){ count = result; });

// Your query
var Assignment = Parse.Object.extend("Assignments");
var query = new Parse.Query(Assignment);
query.descending('updatedAt');
query.limit(displayLimit);
query.skip(page * displayLimit);
// So with this above code, on page 0, you will get 50 results and skip 0 records.
// If your page var is 1, you'll skip the first 50 and get 50 results starting at 51
// So on so forth...
query.find()...

So your links can have the page data encoded in it somehow in that when you click on it, your function will know what page to jump to and grab the appropriate ones. The concept is the same for next / prev. On Next you just do a page++and on Prev you can do a page--.

因此,您的链接可以以某种方式将页面数据编码在其中,因为当您单击它时,您的函数将知道要跳转到哪个页面并抓取适当的页面。下一个/上一个的概念是相同的。在 Next 上,您只需执行一个,page++而在 Prev 上,您可以执行一个page--.

Hope this helps.

希望这可以帮助。

EDIT:

编辑:

If you want to do something like displaying Assignments 51-100 of 237 you'd need to do a .count()to get the total number of records before hand.

如果您想显示 237 个作业中的第 51-100 个作业,则需要先执行 a.count()以获取记录总数。

To get the start number it's simply something like (page * displayLimit) + 1To get the end number just keep in mind that if you're on the last page, you might not have a full 50 records or whatever your displayLimit is.

要获得开始编号,它就像(page * displayLimit) + 1要获得结束编号,请记住,如果您在最后一页,则可能没有完整的 50 条记录或无论您的 displayLimit 是什么。