在Appmaker中显示数据源总页数的最佳方法 [英] Best way for displaying total Pages for a datasource in Appmaker
问题描述
我有一个Google Drive表数据源,该数据源存储未结头寸列表.现在,在数据源中,我将按尺寸查询"字段设置为10,这样我就可以每页获取10条记录.我还添加了一个Pager以显示分页.
I have a Google drive table data source which stores list of open positions. Now in the data source I've set "Query per size" field to 10 so that I can get 10 records per page. I've added a Pager as well to show pagination.
我的查询是我想向最终用户显示"X的第1页",并且该X会根据某些搜索过滤器而有所不同.在Appmaker中实现此目标的最佳方法是什么?
My query is I want to display like "Page 1 of X" to my end users and this X will vary based on certain search filters. What will the best way to achieve this in Appmaker?
我尝试按照以下代码对数据源中的总记录进行计数,但是每次使用搜索条件对其进行更新并重新计数都不是一个合适的解决方案.
I've tried counting total records in a data source as per below code but every time updating that with the search criteria and recounting it is not a proper solution.
//Server side
var newQuery = app.models.Company.newQuery();
var records = newQuery.run();
var totalCount =0;
for(var i=0;i<records.length;i++)
{
totalCount=totalCount+1;
}
return totalCount;
推荐答案
我已经使用 Pavel 建议的计算模型来实现这一目标.
I've achieved this using Calculated Model as suggested by Pavel.
步骤:
- 使用一个字段计数创建一个计算的数据源.
- 在该数据源中添加一个参数
searchQuery
.这将包含用户筛选器.目前,我只有一个搜索查询,用户可以在其中搜索许多内容.所以我只添加了一个参数. - 在此数据源中,添加以下服务器脚本.
- Create a calculated data source with one field count.
- In that data source add one parameter
searchQuery
. This will contain users filter going forward. Currently I have only one search query in which user can search many things. So I've added one parameter only. - In this data source add following server script.
代码:
// Server script
function getTotalRecords(query) {
var receivedQuery = query.parameters.searchQuery;
// console.log('Received query:' + query.parameters.searchQuery);
var records = app.models.Company.newQuery();
records.parameters.SearchText = query.parameters.searchQuery;
if(receivedQuery !== null) {
records.where = '(Name contains? :SearchText or InternalId contains? ' +
':SearchText or LocationList contains? :SearchText )';
}
var recordsCount = records.run().length;
var calculatedModelRecords = [];
var draftRecord = app.models.RecordCount.newRecord();
draftRecord.count = ''+recordsCount;
calculatedModelRecords.push(draftRecord);
return calculatedModelRecords;
}
.
- 在Appmaker页面上,将标签与此数据源绑定.
- 在搜索查询/应用了过滤器的事件上,添加以下代码,以重新加载此数据源并将值分配给Parameter.
// Client script
function updateRecordCount(newValue) {
var ds = app.datasources.RecordCount;
ds.query.parameters.searchQuery = newValue;
ds.unload();
ds.load();
}
这篇关于在Appmaker中显示数据源总页数的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!