Skip to content

Commit b2a4ee3

Browse files
authored
[新增] 表格支持根据容器高度来自动计算当前页记录数,关联@6740
1 parent ee80fce commit b2a4ee3

File tree

8 files changed

+183
-13
lines changed

8 files changed

+183
-13
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- ignore the following lines, they are not important to this demo -->
2+
<jigsaw-demo-description [summary]="summary" [content]="description">
3+
</jigsaw-demo-description>
4+
5+
6+
<!-- start to learn the demo from here -->
7+
<div style="height: calc(100vh - 120px); margin-bottom: 10px;">
8+
<jigsaw-table #tableCmp style="margin-bottom: 10px;" height="100%" [data]="pageable" [hideHeader]="hideHeader"></jigsaw-table>
9+
<div style="display: flex; align-items: center;">
10+
<jigsaw-pagination [data]="pageable" [pageSizeOptions]="[5, 10, 20]"></jigsaw-pagination>
11+
<span>共{{pageable.pagingInfo.totalRecord}}条记录</span>
12+
<jigsaw-checkbox [(checked)]="pageable.pagingInfo.autoPageSizing" (checkedChange)="cdr.markForCheck()"
13+
style="margin-left: 20px">
14+
单页记录数自适应屏幕高度
15+
</jigsaw-checkbox>
16+
<jigsaw-checkbox [(checked)]="hideHeader" (checkedChange)="cdr.markForCheck()"
17+
style="margin-left: 20px">
18+
隐藏表头
19+
</jigsaw-checkbox>
20+
</div>
21+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Component, ViewChild, ChangeDetectorRef } from "@angular/core";
2+
import { HttpClient } from "@angular/common/http";
3+
import { LocalPageableTableData, JigsawTable } from "jigsaw/public_api";
4+
5+
@Component({
6+
templateUrl: './demo.component.html'
7+
})
8+
export class TableAutoPageableDemoComponent {
9+
@ViewChild('tableCmp')
10+
tableCmp: JigsawTable;
11+
pageable: LocalPageableTableData;
12+
hideHeader: boolean = false;
13+
14+
constructor(http: HttpClient, public cdr: ChangeDetectorRef) {
15+
this.pageable = new LocalPageableTableData();
16+
this.pageable.http = http;
17+
this.pageable.fromAjax('mock-data/hr-list');
18+
this.pageable.pagingInfo.autoPageSizing = true;
19+
this.pageable.pagingInfo.itemHeight = 30;
20+
}
21+
22+
// ====================================================================
23+
// ignore the following lines, they are not important to this demo
24+
// ====================================================================
25+
summary: string = '此demo展示了根据容器高度来自动设置分页页数的功能,目前仅支持表格所有记录都等高的情况。';
26+
description: string = '提示:在表格的`columnDefines`属性里添加一个cell.tooltip属性可以让对应的列保持不换行,' +
27+
'避免表格中某些行文本过多导致的意外换行,从而造成自动计算的当前也条数错误的问题,参考[这个demo](#/pc/table/renderer)的源码。';
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {NgModule} from '@angular/core';
2+
import {JigsawTableModule, JigsawPaginationModule, JigsawButtonBarModule, JigsawCheckBoxModule} from "jigsaw/public_api";
3+
import {TableAutoPageableDemoComponent} from './demo.component';
4+
import {JigsawDemoDescriptionModule} from "app/demo-description/demo-description";
5+
6+
@NgModule({
7+
imports: [JigsawTableModule, JigsawPaginationModule, JigsawDemoDescriptionModule, JigsawButtonBarModule, JigsawCheckBoxModule],
8+
declarations: [TableAutoPageableDemoComponent],
9+
exports: [TableAutoPageableDemoComponent]
10+
})
11+
export class TableAutoPageableDemoModule {
12+
}

src/app/demo/pc/table/demo-set.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ import { TableCellRenderFullComponent } from "./cell-render-full/demo.component"
101101
import { TableCellRenderFullDemoModule } from "./cell-render-full/demo.module";
102102
import { TableAutoFillUpDemoComponent } from "./auto-fill-up/demo.component";
103103
import { TableAutoFillUpDemoModule } from "./auto-fill-up/demo.module";
104+
import { TableAutoPageableDemoComponent } from "./auto-page-sizing/demo.component"
105+
import { TableAutoPageableDemoModule } from "./auto-page-sizing/demo.module"
106+
104107

105108
export const routerConfig = [
106109
{
@@ -252,6 +255,9 @@ export const routerConfig = [
252255
},
253256
{
254257
path: "auto-fill-up", component: TableAutoFillUpDemoComponent
258+
},
259+
{
260+
path: "auto-page-sizing", component: TableAutoPageableDemoComponent
255261
}
256262
];
257263

@@ -269,7 +275,7 @@ export const routerConfig = [
269275
TableScrollListenDemoModule, SodokuGameModule, TableAddCheckboxColumnPageableDemoModule, TableSwitchRendererDemoModule,
270276
TableNoDataDemoModule, TableHtmlRendererDemoModule, RebuildTableDataDemoModule, TableCellSelectRenderDemoModule, TableMixinTableDemoModule,
271277
TreeTableDemoModule, TableCellEditablePropertyDemoModule,TableDraggableDemoModule, TableUpdateColumnDefinesDemoModule, TableAutoSaveDemoModule,
272-
TableCellRenderFullDemoModule, TableAutoFillUpDemoModule
278+
TableCellRenderFullDemoModule, TableAutoFillUpDemoModule, TableAutoPageableDemoModule
273279
]
274280
})
275281
export class TableDemoModule {

src/jigsaw/common/core/data/component-data.ts

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export interface IAjaxComponentData extends IComponentData {
272272
*
273273
* $demo = data-encapsulation/ajax-events
274274
*
275-
* param callback 回调函数
275+
* @param callback 回调函数
276276
* @param context 回调函数`callback`执行的上下文
277277
* @returns 返回一个函数,调用它后,`callback`则不会再次被触发。
278278
* 如果你注册了这个回调,则请在组件的`ngOnDestroy()`方法中调用一下这个函数,避免内存泄露。
@@ -636,8 +636,6 @@ export class PagingInfo implements IEmittable {
636636
this.totalRecord = totalRecord;
637637
}
638638

639-
private _currentPage: number = 1;
640-
private _pageSize: number = 20;
641639
private _totalPage: number = 1;
642640

643641
/**
@@ -646,6 +644,9 @@ export class PagingInfo implements IEmittable {
646644
*/
647645
public totalRecord: number = 0;
648646

647+
648+
private _pageSize: number = 20;
649+
649650
/**
650651
* 当前单页记录数
651652
*
@@ -658,11 +659,15 @@ export class PagingInfo implements IEmittable {
658659
}
659660

660661
public set pageSize(value: number) {
661-
if (isNaN(value) || value < 1) return;
662+
if (isNaN(value) || value < 1 || this.autoPageSizing || this._pageSize === value) {
663+
return;
664+
}
662665
this._pageSize = value;
663666
this.emit();
664667
}
665668

669+
private _currentPage: number = 1;
670+
666671
/**
667672
* 当前页索引,从1开始计数。修改此属性后,`PagingInfo`会发出获取对应页数据的事件,通过`subscribe`添加监听器可处理此事件。
668673
*
@@ -691,6 +696,81 @@ export class PagingInfo implements IEmittable {
691696
return this.totalRecord && this.pageSize != Infinity ? Math.ceil(this.totalRecord / this.pageSize) : 1;
692697
}
693698

699+
private _autoPageSizing: boolean = false;
700+
701+
/**
702+
* 自动分页
703+
*
704+
* 自动分页的开关,开启时,会依据 `containerHeight` 和 `itemHeight` 参数计算 `pageSize`,
705+
* 注意:在本属性为true时,直接操作`pageSize`无效
706+
*/
707+
public get autoPageSizing(): boolean {
708+
return this._autoPageSizing;
709+
}
710+
711+
public set autoPageSizing(value: boolean) {
712+
if (this._autoPageSizing == !!value) {
713+
return;
714+
}
715+
if (!!value) {
716+
this._calcAutoPageSize();
717+
}
718+
this._autoPageSizing = !!value;
719+
this.emit();
720+
}
721+
722+
private _containerHeight: number;
723+
724+
/**
725+
* 本属性用于实现动态分页记录数的功能,它指明了用于显示分页数据的容器总高度,配合 `itemHeight` 属性可计算出当前最佳单页记录数
726+
*
727+
* 如:table组件内容高度
728+
*
729+
* $demo=table/auto-page-sizing
730+
*/
731+
public get containerHeight(): number {
732+
return this._containerHeight;
733+
}
734+
735+
public set containerHeight(value: number) {
736+
if (isNaN(value) || value < 1) {
737+
return;
738+
}
739+
this._containerHeight = value;
740+
this._calcAutoPageSize();
741+
this.emit();
742+
}
743+
744+
private _itemHeight: number;
745+
746+
/**
747+
* 本属性用于实现动态分页记录数的功能,它指明了单条记录的高度,配合 `containerHeight` 属性可计算出当前最佳单页记录数
748+
*
749+
* 如:table组件内容高度
750+
*
751+
* $demo=table/auto-page-sizing
752+
*/
753+
public get itemHeight(): number {
754+
return this._itemHeight;
755+
}
756+
757+
public set itemHeight(value: number) {
758+
if (isNaN(value) || value < 1) {
759+
return;
760+
}
761+
this._itemHeight = value;
762+
this._calcAutoPageSize();
763+
this.emit();
764+
}
765+
766+
private _calcAutoPageSize(): void {
767+
if (!this.autoPageSizing || isNaN(this.containerHeight) || isNaN(this.itemHeight)) {
768+
return;
769+
}
770+
const newPageSize = Math.floor(this.containerHeight / this.itemHeight);
771+
this._pageSize = newPageSize || 1;
772+
}
773+
694774
private _emitter = new EventEmitter<any>();
695775

696776
public emit(value?: any): void {
@@ -726,8 +806,6 @@ export class PagingInfo implements IEmittable {
726806
export class DataFilterInfo {
727807
constructor(/**
728808
* 过滤关键字
729-
*
730-
*
731809
*/
732810
public key: string = '',
733811
/**

src/jigsaw/pc-components/pagination/pagination.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
labelField="label"
2727
width="80"
2828
[data]="pageSizeOptions"
29-
class="jigsaw-paging-page-size">
29+
class="jigsaw-paging-page-size"
30+
[disabled]="data?.pagingInfo?.autoPageSizing">
3031
</jigsaw-select>
3132

3233
<div class="jigsaw-paging-goto" *ngIf="showQuickJumper">
@@ -63,7 +64,8 @@
6364
[trackItemBy]="['value','label']"
6465
labelField="label"
6566
width="80"
66-
[data]="pageSizeOptions">
67+
[data]="pageSizeOptions"
68+
[disabled]="data?.pagingInfo?.autoPageSizing">
6769
</jigsaw-select>
6870
</ng-container>
6971
</ng-container>

src/jigsaw/pc-components/pagination/pagination.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export class JigsawPagination extends AbstractJigsawComponent implements OnInit,
220220
// pagingInfo.currentPage采用的getter&setter,不可随便赋值
221221
this.data.pagingInfo.currentPage = newValue;
222222
}
223+
this._changeDetectorRef.detectChanges();
223224
}
224225
}
225226

@@ -265,8 +266,8 @@ export class JigsawPagination extends AbstractJigsawComponent implements OnInit,
265266
private _setCurrentPage(): void {
266267
this._pages.forEach(page => {
267268
page.current = page.pageNumber == this.current;
268-
page._changeDetectorRef.markForCheck();
269269
});
270+
this._changeDetectorRef.detectChanges();
270271
}
271272

272273
/**
@@ -313,7 +314,7 @@ export class JigsawPagination extends AbstractJigsawComponent implements OnInit,
313314
this._$pageNums = Array.from(new Array(this._totalPage)).map((item, index) => ++index);
314315
}
315316
}
316-
this._changeDetectorRef.markForCheck();
317+
this._changeDetectorRef.detectChanges();
317318
}
318319

319320
/**

src/jigsaw/pc-components/table/table.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
TableHeadSetting
3838
} from "./table-typings";
3939
import {CallbackRemoval, CommonUtils} from "../../common/core/utils/common-utils";
40-
import {SortOrder, IPageable} from "../../common/core/data/component-data";
40+
import {SortOrder, IPageable, PagingInfo} from "../../common/core/data/component-data";
4141
import {
4242
DefaultCellRenderer,
4343
JigsawTableRendererModule,
@@ -327,7 +327,7 @@ export class JigsawTable extends AbstractJigsawComponent implements OnInit, Afte
327327
private _updateFillUpBlankRow(): void {
328328
this._$blankRow = [];
329329
this._changeDetectorRef.detectChanges();
330-
330+
331331
if (!this.autoFillUp) {
332332
return;
333333
}
@@ -364,6 +364,23 @@ export class JigsawTable extends AbstractJigsawComponent implements OnInit, Afte
364364
this._changeDetectorRef.detectChanges();
365365
}
366366

367+
private _updateAutoPageSizing(): void {
368+
const data: IPageable = <any>this.data;
369+
if (!(data?.pagingInfo instanceof PagingInfo) || !data?.pagingInfo.autoPageSizing) {
370+
return;
371+
}
372+
const tableEle = this._elementRef.nativeElement.querySelector(".jigsaw-table-range");
373+
if (!tableEle) {
374+
return;
375+
}
376+
const bodyHeight = tableEle.getBoundingClientRect().bottom - tableEle.getBoundingClientRect().top;
377+
const containerSize = this.hideHeader ? bodyHeight - 1 : bodyHeight - 42;
378+
if (!isNaN(data.pagingInfo.containerHeight) && data.pagingInfo.containerHeight === containerSize) {
379+
return
380+
}
381+
data.pagingInfo.containerHeight = containerSize;
382+
}
383+
367384
/**
368385
* 生成混合后的列定义序列
369386
*/
@@ -412,6 +429,8 @@ export class JigsawTable extends AbstractJigsawComponent implements OnInit, Afte
412429
this.runMicrotask(() => {
413430
// 自动添加空白行
414431
this._updateFillUpBlankRow();
432+
// 自动分页
433+
this._updateAutoPageSizing();
415434
// 等待additionalTableData在renderer更新完成
416435
this.additionalDataChange.emit(this.additionalData);
417436
// 等待滚动条初始化
@@ -602,6 +621,7 @@ export class JigsawTable extends AbstractJigsawComponent implements OnInit, Afte
602621
this._handleScrollBar();
603622
this._setVerticalScrollbarOffset();
604623
this._updateFillUpBlankRow();
624+
this._updateAutoPageSizing();
605625
}
606626

607627
private _tableHeaderElement: HTMLElement;
@@ -895,6 +915,8 @@ export class JigsawTable extends AbstractJigsawComponent implements OnInit, Afte
895915
this._renderer.setStyle(this._elementRef.nativeElement.querySelector('.jigsaw-table-body-range'),
896916
'max-height', this._maxHeight);
897917
this._tableHeaderElement = this._elementRef.nativeElement.querySelector(".jigsaw-table-header");
918+
// 自动分页
919+
this._updateAutoPageSizing();
898920
}
899921

900922
ngOnDestroy() {

0 commit comments

Comments
 (0)