(服务器端)如何从 MarkLogic 中检索数据并一次分页几个
(Server Side) How to retrieve data from MarkLogic and paginate a few at a time
我的目标是每次单击 "next" 按钮时从数据库中检索接下来的 10 个项目。
因此,我不想一次从数据库中检索 100 条记录,这可能会使系统滞后,我只想一次检索 10 条记录。
下面是我所做的一些片段,不知道我还遗漏了什么...
在resource.java
@Autowired
protected QueryManager queryManager;
@RequestMapping(value = "/retrieveObjects/{page}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object[]> retrieveObjects(@PathVariable("page") String page){
int pageNum = Integer.valueOf(page);
int start = PAGE_SIZE_TEN * (pageNum - 1) + 1;
SearchHandle resultsHandle = new SearchHandle();
queryDef.setDirectory(DIRECTORY);
queryManager.setPageLength(PAGE_SIZE_TEN);
queryManager.search(queryDef, resultsHandle, start);
return new ResponseEntity<Object[]>(handleSearch(resultsHandle), HttpStatus.OK);
}
在component.ts
this.restclient.getjson('/api/retrieveObjects/'+page)
.subscribe(objects => {
for(let i=0; i<objects .length; i++){
this.objects.push(objects[i]);
}
});
在component.html
<div *ngFor="let object of objects | paginate: { itemsPerPage: 10, currentPage: p }">
<div (click)="gotoObject(object.id)">
{{object.id}}
</div>
</div>
<pagination-controls class="pagination" (pageChange)="p = $event;getPage($event)"></pagination-controls>
我有 100 条记录。
但是,结果只显示我前 10 条记录的一页,"next" 按钮被禁用。
预期结果应显示前 10 条记录的一页,单击 "next" 按钮将调用 api/retrieveObjects/ 检索下 10 条记录。
我想你想实现服务器端分页:
为此你必须传递一个额外的参数totalItems
{
"count": 100, // no of total records
"data": [ // chunk of data (in your case chunk of 10 records)
{ /* item 1 */ },
{ /* item 2 */ },
{ /* item 3 */ },
{ /* ... */ },
{ /* item 10 */ }
]
}
<div *ngFor="let object of objects.data | paginate: { itemsPerPage: 10, currentPage: p, totalItems: objects.count }">
我的目标是每次单击 "next" 按钮时从数据库中检索接下来的 10 个项目。
因此,我不想一次从数据库中检索 100 条记录,这可能会使系统滞后,我只想一次检索 10 条记录。
下面是我所做的一些片段,不知道我还遗漏了什么...
在resource.java
@Autowired
protected QueryManager queryManager;
@RequestMapping(value = "/retrieveObjects/{page}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object[]> retrieveObjects(@PathVariable("page") String page){
int pageNum = Integer.valueOf(page);
int start = PAGE_SIZE_TEN * (pageNum - 1) + 1;
SearchHandle resultsHandle = new SearchHandle();
queryDef.setDirectory(DIRECTORY);
queryManager.setPageLength(PAGE_SIZE_TEN);
queryManager.search(queryDef, resultsHandle, start);
return new ResponseEntity<Object[]>(handleSearch(resultsHandle), HttpStatus.OK);
}
在component.ts
this.restclient.getjson('/api/retrieveObjects/'+page)
.subscribe(objects => {
for(let i=0; i<objects .length; i++){
this.objects.push(objects[i]);
}
});
在component.html
<div *ngFor="let object of objects | paginate: { itemsPerPage: 10, currentPage: p }">
<div (click)="gotoObject(object.id)">
{{object.id}}
</div>
</div>
<pagination-controls class="pagination" (pageChange)="p = $event;getPage($event)"></pagination-controls>
我有 100 条记录。
但是,结果只显示我前 10 条记录的一页,"next" 按钮被禁用。
预期结果应显示前 10 条记录的一页,单击 "next" 按钮将调用 api/retrieveObjects/ 检索下 10 条记录。
我想你想实现服务器端分页:
为此你必须传递一个额外的参数totalItems
{
"count": 100, // no of total records
"data": [ // chunk of data (in your case chunk of 10 records)
{ /* item 1 */ },
{ /* item 2 */ },
{ /* item 3 */ },
{ /* ... */ },
{ /* item 10 */ }
]
}
<div *ngFor="let object of objects.data | paginate: { itemsPerPage: 10, currentPage: p, totalItems: objects.count }">