要求:
使用ngx-datatable显示动态数据,并使用页面ID使用分页
说明:
我有一个动态数据,我在其中使用ngx-datatable以角度显示它,在这里一切正常,但是问题是我不确定如何使用page_id(使用post body发送到服务器)应用分页。在这里,我将获得page_id以及API响应,这是第一个API调用。在这里page_id必须作为正文发送,以获取完全相同的API,以获取其余结果。
例如:假设我在1ST API调用中有20个结果,我将通过使用页面ID获得10条记录和一个页面ID,如何获得下一个10个结果
我实现的目标:
获取数据并将其显示在已应用的基本表分页中
下面是我的代码:
using System;
using System.Diagnostics;
namespace Main
{
class Program
{
public static int Entry(string args)
{
SetDebugPrivileges();
var proc = System.Diagnostics.Process.GetCurrentProcess();
proc.EnableRaisingEvents = true; // Throws Acess Denied error
return 0;
}
}
}
HTML代码:
Result=[];
reorderable: boolean = true;
selected = [];
rows = [];
columns = [];
DataArray = [];
Results = {
"data": [
{
"_score": 0.36464313,
"_type": "data",
"_id": "abcd",
"_source": {
"filter": "data",
"information": {
"product_id": "abcd",
"creation_utctime": "1477335693653"
},
"enduser": "free"
},
"_index": "dell_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcde",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcdef",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
}
],
"total": 4,
"page_id": "WpNdVJMMjlJVnJTYTFuUklB"
}
LoadInfo(){
this.DataArray = ["filter","information.product_id","information.creation_utctime","enduser"];
this.rows=[];
this.Result = this.Results['data'];
// tslint:disable-next-line: forin
for (var res in this.Result) {
var row = {};
for (var key in this.Result[res]['_source']) {
if (typeof this.Result[res]['_source'][key] === 'object') {
for (var k in this.Result[res]['_source'][key]) {
let temp = key + "." + k;
row[temp] = this.Result[res]['_source'][key][k];
}
} else {
row[key] = this.Result[res]['_source'][key]
}
row['_id'] = this.Result[res]['_id'];
}
this.rows.push(row);
}
console.log(this.rows);
}
onActivate(event) {
// console.log('Activate Event', event);
}
onSelect({ selected }) {
// console.log('Select Event', selected, this.selected);
this.selected.splice(0, this.selected.length);
this.selected.push(...selected);
}
Stackblitz链接:https://stackblitz.com/edit/angular-secw8u
注意:即使在10条记录之后也有pageid,也可能不会有更多记录
此处api调用是简单的发布请求
<button type="button" (click)="LoadInfo()">LoadData</button>
<div>
<ngx-datatable class="material ui" [rows]="rows" [columnMode]="'force'" [headerHeight]="50"
[footerHeight]="50" [limit]="2" [rowHeight]="'auto'" [reorderable]="reorderable" [selected]="selected"
[selectionType]="'checkbox'" [scrollbarH]="true" [sortType]="'multi'" (activate)="onActivate($event)"
(select)='onSelect($event)'>
<ngx-datatable-column [width]="30" [sortable]="true" [canAutoResize]="false" [draggable]="false"
[resizeable]="false" class="uih">
<ng-template ngx-datatable-header-template let-value="value" let-allRowsSelected="allRowsSelected"
let-selectFn="selectFn">
<input type="checkbox" [checked]="allRowsSelected" (change)="selectFn(!allRowsSelected)" />
</ng-template>
<ng-template ngx-datatable-cell-template let-value="value" let-isSelected="isSelected"
let-onCheckboxChangeFn="onCheckboxChangeFn">
<input type="checkbox" [checked]="isSelected" (change)="onCheckboxChangeFn($event)" />
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column *ngFor="let attr of DataArray" [sortable]="true" prop={{attr}} name={{attr}}>
</ngx-datatable-column>
</ngx-datatable>
</div>
在第一个api调用中,我们不会发送页面ID,因为在调用第一个api调用后,我们将获得响应,因为我们将获得pageid,而对于第二个api调用,我的意思是进行分页,那么我们必须使用pageid < / p>
答案 0 :(得分:2)
对于分页,您需要知道总页数。否则,您需要总数 项目数和每页项目数(得出总页数)。就你而言 您只有一个页面ID,甚至不说您所在的页面。页面ID 仅允许您访问下一页项目。
如果您实现无限滚动功能,则此API很有用。否则你只能 实现 more 按钮,将新项目加载到表中。 link 您在评论中提供的内容将实现此更多按钮功能。
因此,您可以覆盖默认的页脚ngx-datatable
来添加更多按钮以进行加载
还有更多物品要上桌。
<ngx-datatable-footer>
<ng-template ngx-datatable-footer-template
let-rowCount="rowCount"
let-pageSize="pageSize"
let-selectedCount="selectedCount"
let-curPage="curPage"
let-offset="offset"
let-isVisible="isVisible">
<div class="datatable-footer-inner selected-count">
<div class="page-count" *ngIf="selectedCount">
<span> {{selectedCount}} selected / </span> {{rowCount}} total
</div>
<div class="datatable-pager">
<ul class="pager">
<li class="pages active" role="button" aria-label="next" *ngIf="rowCount">
<a href="javascript:void(0)" (click)="LoadInfo()"> Next </a>
</li>
</ul>
</div>
</div>
</ng-template>
</ngx-datatable-footer>