Skip to content

Commit 6e31536

Browse files
authored
Merge pull request #178 from zvonimirfras/master
feat(table): add support for custom "no data" template
2 parents 7db7b20 + e660ba8 commit 6e31536

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/table/table.component.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ import { getScrollbarWidth } from "../common/utils";
102102
*
103103
* See `TableHeaderItem` class for more information.
104104
*
105+
* ## No data template
106+
*
107+
* When table has no data to show, it can show a message you provide it instead.
108+
*
109+
* ```html
110+
* <ibm-table [model]="model">No data.</ibm-table>
111+
* ```
112+
*
113+
* ... will show `No data.` message, but you can get creative and provide any template you want
114+
* to replace table's default `tbody`.
115+
*
105116
* ## Use pagination as table footer
106117
*
107118
* ```html
@@ -269,6 +280,7 @@ import { getScrollbarWidth } from "../common/utils";
269280
</tr>
270281
</thead>
271282
<tbody
283+
*ngIf="!noData; else noDataTemplate"
272284
[ngStyle]="{'overflow-y': 'scroll'}"
273285
(scroll)="onScroll($event)">
274286
<ng-container *ngFor="let row of model.data; let i = index">
@@ -331,6 +343,7 @@ import { getScrollbarWidth } from "../common/utils";
331343
</tr>
332344
</ng-container>
333345
</tbody>
346+
<ng-template #noDataTemplate><ng-content></ng-content></ng-template>
334347
<tfoot>
335348
<tr *ngIf="this.model.isLoading">
336349
<td class="table_loading-indicator">
@@ -511,6 +524,12 @@ export class Table {
511524
*/
512525
@Output() scrollLoad = new EventEmitter<TableModel>();
513526

527+
get noData() {
528+
return !this.model.data ||
529+
this.model.data.length === 0 ||
530+
this.model.data.length === 1 && this.model.data[0].length === 0;
531+
}
532+
514533
private _model: TableModel;
515534

516535
private columnResizeWidth: number;

src/table/table.stories.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const en = require("./../../src/i18n/en.json");
4040
[showSelectionColumn]="showSelectionColumn"
4141
[striped]="striped"
4242
(sort)="simpleSort($event)">
43+
<ng-content></ng-content>
4344
</ibm-table>
4445
`
4546
})
@@ -429,6 +430,11 @@ simpleModel.header = [
429430
new TableHeaderItem({data: "Name"}), new TableHeaderItem({data: "hwer", style: {"width": "auto"} })
430431
];
431432

433+
const emptyModel = new TableModel();
434+
emptyModel.header = [
435+
new TableHeaderItem({data: "Name"}), new TableHeaderItem({data: "hwer", style: {"width": "auto"} })
436+
];
437+
432438
function sort(model, index: number) {
433439
if (model.header[index].sorted) {
434440
// if already sorted flip sorting direction
@@ -475,6 +481,30 @@ storiesOf("Table", module).addDecorator(
475481
sortable: boolean("sortable", true)
476482
}
477483
}))
484+
.add("with no data", () => ({
485+
template: `
486+
<app-table
487+
[model]="model"
488+
[size]="size"
489+
[showSelectionColumn]="showSelectionColumn"
490+
[striped]="striped">
491+
<tbody><tr><td class="no-data" colspan="3"><div>No data.</div></td></tr></tbody>
492+
</app-table>
493+
`,
494+
styles: [`
495+
.no-data {
496+
width: 100%;
497+
height: 150px;
498+
text-align: center;
499+
}
500+
`],
501+
props: {
502+
model: emptyModel,
503+
size: selectV2("size", {Small: "sm", Normal: "md", Large: "lg"}, "md", "table-size-selection"),
504+
showSelectionColumn: boolean("showSelectionColumn", true),
505+
striped: boolean("striped", true)
506+
}
507+
}))
478508
.add("with expansion", () => ({
479509
template: `
480510
<app-expansion-table

0 commit comments

Comments
 (0)