Skip to content

Commit 03366d5

Browse files
authored
Merge pull request #134 from stanislavgeorgiev/Table
Table Header: More sorting options #122
2 parents 55c937f + f33eb90 commit 03366d5

File tree

3 files changed

+94
-18
lines changed

3 files changed

+94
-18
lines changed

src/table/table-header-item.class.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ export class TableHeaderItem {
1919
*/
2020
sorted = false;
2121

22+
/**
23+
* Enables sorting on click by default.
24+
* If false then this column won't show a sorting arrow at all.
25+
*
26+
* @memberof TableHeaderItem
27+
*/
28+
sortable = true;
29+
2230
/**
2331
* Number of applied filters.
2432
*
@@ -28,6 +36,13 @@ export class TableHeaderItem {
2836
*/
2937
filterCount = 0;
3038

39+
/**
40+
* Attach a class to the column, both the header and column cells.
41+
*
42+
* @memberof TableHeaderItem
43+
*/
44+
className: string;
45+
3146
/**
3247
* Style for the column, applied to every element in the column.
3348
*
@@ -69,6 +84,15 @@ export class TableHeaderItem {
6984
*/
7085
data: any;
7186

87+
/**
88+
* Data for the header item for general usage in the controller.
89+
* For example, which `field` is this column related to.
90+
*
91+
* @type {*}
92+
* @memberof TableHeaderItem
93+
*/
94+
metadata: any;
95+
7296
/**
7397
* Used to display data in a desired way.
7498
*
@@ -204,14 +228,11 @@ export class TableHeaderItem {
204228
};
205229
// fill our object with provided props, and fallback to defaults
206230
const data = Object.assign({}, defaults, rawData);
207-
this.data = data.data;
208-
this.visible = data.visible;
209-
this.filterCount = data.filterCount;
210-
this.template = data.template;
211-
this.filterTemplate = data.filterTemplate;
212-
this.filterFooter = data.filterFooter;
213-
this.filterData = data.filterData;
214-
this.style = data.style;
231+
for (let property of Object.getOwnPropertyNames(data)) {
232+
if (data.hasOwnProperty(property)) {
233+
this[property] = data[property];
234+
}
235+
}
215236
}
216237

217238
/**

src/table/table.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ import { getScrollbarWidth } from "../common/utils";
171171
<ng-container *ngFor="let column of model.header; let i = index">
172172
<th [ngClass]='{"thead_action": column.filterTemplate || this.sort.observers.length > 0}'
173173
*ngIf="column.visible"
174+
[class]="column.className"
174175
[ngStyle]="column.style"
175176
[draggable]="columnsDraggable"
176177
(dragstart)="columnDragStart($event, i)"
@@ -182,6 +183,7 @@ import { getScrollbarWidth } from "../common/utils";
182183
</div>
183184
<button
184185
class="bx--table-sort-v2"
186+
*ngIf="this.sort.observers.length > 0 && column.sortable"
185187
[ngClass]="{
186188
'bx--table-sort-v2--active': column.sorted,
187189
'bx--table-sort-v2--ascending': column.ascending
@@ -200,6 +202,14 @@ import { getScrollbarWidth } from "../common/utils";
200202
<path d="M0 0l5 4.998L10 0z" fill-rule="evenodd" />
201203
</svg>
202204
</button>
205+
<span
206+
class="bx--table-header-label"
207+
*ngIf="this.sort.observers.length === 0 || (this.sort.observers.length > 0 && !column.sortable)">
208+
<span *ngIf="!column.template" [title]="column.data">{{column.data}}</span>
209+
<ng-template
210+
[ngTemplateOutlet]="column.template" [ngTemplateOutletContext]="{data: column.data}">
211+
</ng-template>
212+
</span>
203213
<button
204214
[ngClass]="{'active': column.filterCount > 0}"
205215
*ngIf="column.filterTemplate"
@@ -298,6 +308,7 @@ import { getScrollbarWidth } from "../common/utils";
298308
</td>
299309
<ng-container *ngFor="let item of row; let i = index">
300310
<td *ngIf="model.header[i].visible"
311+
[class]="model.header[i].className"
301312
[ngStyle]="model.header[i].style">
302313
<ng-container *ngIf="!item.template">{{item.data}}</ng-container>
303314
<ng-template

src/table/table.stories.ts

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
Component,
55
ViewChild,
66
OnInit,
7-
Input
7+
Input,
8+
OnChanges,
9+
SimpleChanges
810
} from "@angular/core";
911
import { storiesOf, moduleMetadata } from "@storybook/angular";
1012
import {
@@ -29,6 +31,51 @@ import { clone } from "../utils/utils";
2931

3032
const en = require("./../../src/i18n/en.json");
3133

34+
@Component({
35+
selector: "app-table",
36+
template: `
37+
<ibm-table
38+
[model]="model"
39+
[size]="size"
40+
[showSelectionColumn]="showSelectionColumn"
41+
[striped]="striped"
42+
(sort)="simpleSort($event)">
43+
</ibm-table>
44+
`
45+
})
46+
class TableStory implements OnInit, OnChanges {
47+
@Input() model = new TableModel();
48+
@Input() size = "md";
49+
@Input() showSelectionColumn = true;
50+
@Input() striped = true;
51+
@Input() sortable = true;
52+
53+
ngOnInit() {
54+
this.model.header = [
55+
new TableHeaderItem({
56+
data: "Name"
57+
}),
58+
new TableHeaderItem({
59+
data: "hwer",
60+
style: {"width": "auto"},
61+
className: "my-class"
62+
})
63+
];
64+
}
65+
66+
ngOnChanges(changes: SimpleChanges): void {
67+
if (changes.sortable) {
68+
for (let column of this.model.header) {
69+
column.sortable = changes.sortable.currentValue;
70+
}
71+
}
72+
}
73+
74+
simpleSort(index: number) {
75+
sort(simpleModel, index);
76+
}
77+
}
78+
3279
@Component({
3380
selector: "app-custom-table",
3481
template: `
@@ -390,10 +437,6 @@ function sort(model, index: number) {
390437
model.sort(index);
391438
}
392439

393-
function simpleSort(index: number) {
394-
sort(simpleModel, index);
395-
}
396-
397440

398441
storiesOf("Table", module).addDecorator(
399442
moduleMetadata({
@@ -405,6 +448,7 @@ storiesOf("Table", module).addDecorator(
405448
TranslateModule.forRoot()
406449
],
407450
declarations: [
451+
TableStory,
408452
DynamicTableStory,
409453
ExpansionTableStory,
410454
OverflowTableStory,
@@ -415,20 +459,20 @@ storiesOf("Table", module).addDecorator(
415459
.addDecorator(withKnobs)
416460
.add("default", () => ({
417461
template: `
418-
<ibm-table
462+
<app-table
419463
[model]="model"
420464
[size]="size"
421465
[showSelectionColumn]="showSelectionColumn"
422466
[striped]="striped"
423-
(sort)="simpleSort($event)">
424-
</ibm-table>
467+
[sortable]="sortable">
468+
</app-table>
425469
`,
426470
props: {
427471
model: simpleModel,
428-
simpleSort: simpleSort,
429472
size: selectV2("size", {Small: "sm", Normal: "md", Large: "lg"}, "md", "table-size-selection"),
430473
showSelectionColumn: boolean("showSelectionColumn", true),
431-
striped: boolean("striped", true)
474+
striped: boolean("striped", true),
475+
sortable: boolean("sortable", true)
432476
}
433477
}))
434478
.add("with expansion", () => ({

0 commit comments

Comments
 (0)