Skip to content

Commit 7da7587

Browse files
authored
Merge pull request #174 from zvonimirfras/master
Improve table component
2 parents a3ced8b + fcea0ef commit 7da7587

File tree

5 files changed

+57
-36
lines changed

5 files changed

+57
-36
lines changed

src/checkbox/checkbox.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class CheckboxChange {
6767
[indeterminate]="indeterminate"
6868
[attr.aria-label]="ariaLabel"
6969
[attr.aria-labelledby]="ariaLabelledby"
70-
[attr.aria-checked]="indeterminate ? 'mixed' : checked"
70+
[attr.aria-checked]="(indeterminate ? 'mixed' : checked)"
7171
(change)="onChange($event)"
7272
(click)="onClick($event)">
7373
<label [for]="id" class="bx--checkbox-label">

src/dialog/overflow-menu/overflow-menu.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import { Component, ElementRef } from "@angular/core";
55
*
66
* html:
77
* ```
8-
* <ibm-overflow-menu [options]="overflowContent"></ibm-overflow-menu>
9-
* <ng-template #overflowContent>
8+
* <ibm-overflow-menu>
109
* <ibm-overflow-menu-option>Option 1</ibm-overflow-menu-option>
1110
* <ibm-overflow-menu-option>Option 2</ibm-overflow-menu-option>
12-
* </ng-template>
11+
* </ibm-overflow-menu>
1312
* ```
1413
*/
1514
@Component({

src/table/table-model.class.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ describe("Table", () => {
88
expect(tableModel.totalDataLength).toEqual(0);
99
});
1010

11+
it("model should handle different variants of empty data", () => {
12+
let tableModel = new TableModel();
13+
tableModel.data = undefined;
14+
15+
expect(tableModel.data).toEqual([[]]);
16+
expect(tableModel.totalDataLength).toEqual(0);
17+
18+
tableModel.data = null;
19+
20+
expect(tableModel.data).toEqual([[]]);
21+
expect(tableModel.totalDataLength).toEqual(0);
22+
23+
tableModel.data = [];
24+
25+
expect(tableModel.data).toEqual([[]]);
26+
expect(tableModel.totalDataLength).toEqual(0);
27+
});
28+
1129
it("should set rowsSelected when setting data", () => {
1230
let tableModel = new TableModel();
1331
tableModel.data = [

src/table/table-model.class.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class TableModel implements PaginationModel {
1515
* @memberof TableModel
1616
*/
1717
set data(newData: Array<Array<TableItem>>) {
18+
if (!newData || (Array.isArray(newData) && newData.length === 0) ) {
19+
newData = [[]];
20+
}
21+
1822
this._data = newData;
1923

2024
// init rowsSelected

src/table/table.component.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ import { getScrollbarWidth } from "../common/utils";
1313
/**
1414
* Build your table with this component by extending things that differ from default.
1515
*
16-
* demo: [https://pages.github.ibm.com/peretz/neutrino/#/table](https://pages.github.ibm.com/peretz/neutrino/#/table)
16+
* demo: [https://angular.carbondesignsystem.com/?selectedKind=Table](https://angular.carbondesignsystem.com/?selectedKind=Table)
1717
*
1818
* Instead of the usual write-your-own-html approach you had with `<table>`,
19-
* neutrino table uses model-view-controller approach.
19+
* carbon table uses model-view-controller approach.
2020
*
2121
* Here, you create a view (with built-in controller) and provide it a model.
2222
* Changes you make to the model are reflected in the view. Provide same model you use
23-
* in the table to the `<ibm-table-pagination>` and `<ibm-table-goto-page>` components.
23+
* in the table to the `<ibm-pagination>` components.
2424
* They provide a different view over the same data.
2525
*
2626
* ## Basic usage
2727
*
2828
* ```html
29-
* <ibm-table [model]="simpleModel"></ibm-table>
29+
* <ibm-table [model]="model"></ibm-table>
3030
* ```
3131
*
3232
* ```typescript
33-
* public simpleModel = new TableModel();
33+
* public model = new TableModel();
3434
*
35-
* this.simpleModel.data = [
35+
* this.model.data = [
3636
* [new TableItem({data: "asdf"}), new TableItem({data: "qwer"})],
3737
* [new TableItem({data: "csdf"}), new TableItem({data: "zwer"})],
3838
* [new TableItem({data: "bsdf"}), new TableItem({data: "swer"})],
@@ -102,14 +102,10 @@ import { getScrollbarWidth } from "../common/utils";
102102
*
103103
* See `TableHeaderItem` class for more information.
104104
*
105-
* ## Build your own table footer with neutrino components
105+
* ## Use pagination as table footer
106106
*
107107
* ```html
108-
* <p class="table-footer">
109-
* <span class="table-selection-info">{{model.selectedRowsCount()}} of {{model.totalDataLength}} rows selected</span>
110-
* <ibm-table-pagination [model]="model" (selectPage)="selectPage($event)"></ibm-table-pagination>
111-
* <ibm-table-goto-page (selectPage)="selectPage($event)"></ibm-table-goto-page>
112-
* </p>
108+
* <ibm-pagination [model]="model" (selectPage)="selectPage($event)"></ibm-pagination>
113109
* ```
114110
*
115111
* `selectPage()` function should fetch the data from backend, create new `data`, apply it to `model.data`,
@@ -119,28 +115,30 @@ import { getScrollbarWidth } from "../common/utils";
119115
*
120116
* ```typescript
121117
* selectPage(page) {
122-
* this.service.getPage(page).then((data: Array<Array<any>>) => {
123-
* let newData = [];
124-
*
125-
* // create new data from the service data
126-
* data.forEach(dataRow => {
127-
* let row = [];
128-
* dataRow.forEach(dataElement => {
129-
* row.push(new TableItem({
130-
* data: dataElement,
131-
* template: typeof dataElement === "string" ? undefined : this.customTableItemTemplate
132-
* // your template can handle all the data types so you don't have to conditionally set it
133-
* // you can also set different templates for different columns based on index
134-
* }));
135-
* });
136-
* newData.push(row);
137-
* });
138-
*
118+
* this.getPage(page).then((data: Array<Array<any>>) => {
139119
* // set the data and update page
140-
* this.model.data = newData;
120+
* this.model.data = this.prepareData(data);
141121
* this.model.currentPage = page;
142122
* });
143123
* }
124+
*
125+
* private prepareData(data: Array<Array<any>>) {
126+
* // create new data from the service data
127+
* let newData = [];
128+
* data.forEach(dataRow => {
129+
* let row = [];
130+
* dataRow.forEach(dataElement => {
131+
* row.push(new TableItem({
132+
* data: dataElement,
133+
* template: typeof dataElement === "string" ? undefined : this.paginationTableItemTemplate
134+
* // your template can handle all the data types so you don't have to conditionally set it
135+
* // you can also set different templates for different columns based on index
136+
* }));
137+
* });
138+
* newData.push(row);
139+
* });
140+
* return newData;
141+
* }
144142
* ```
145143
*
146144
* @export
@@ -162,9 +160,10 @@ import { getScrollbarWidth } from "../common/utils";
162160
<th *ngIf="model.hasExpandableRows()"></th>
163161
<th *ngIf="showSelectionColumn">
164162
<ibm-checkbox
165-
[size]="size !== 'lg' ? 'sm' : 'md'"
163+
[size]="size !== ('lg' ? 'sm' : 'md')"
166164
[(ngModel)]="selectAllCheckbox"
167165
[indeterminate]="selectAllCheckboxSomeSelected"
166+
aria-label="Select all rows"
168167
(change)="onSelectAllCheckboxChange()">
169168
</ibm-checkbox>
170169
</th>
@@ -301,6 +300,7 @@ import { getScrollbarWidth } from "../common/utils";
301300
</td>
302301
<td *ngIf="showSelectionColumn">
303302
<ibm-checkbox
303+
aria-label="Select row"
304304
[size]="size !== ('lg' ? 'sm' : 'md')"
305305
[(ngModel)]="model.rowsSelected[i]"
306306
(change)="onRowCheckboxChange(i)">
@@ -383,7 +383,7 @@ export class Table {
383383
/**
384384
* Controls whether to show the selection checkboxes column or not.
385385
*
386-
* @deprecated in the next major neutrino version in favour of
386+
* @deprecated in the next major carbon-components-angular version in favour of
387387
* `showSelectionColumn` because of new attribute `enableSingleSelect`
388388
* please use `showSelectionColumn` instead
389389
*/

0 commit comments

Comments
 (0)