Skip to content

Commit af8e00e

Browse files
Toolitoazhukaudev
authored andcommitted
feat(custom-actions): add the custom actions feature (#338)
1 parent 2bb9bd8 commit af8e00e

10 files changed

+131
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'basic-example-custom-actions',
5+
template: `
6+
<ng2-smart-table [settings]="settings" [source]="data" (custom)="onCustom($event)"></ng2-smart-table>
7+
`,
8+
})
9+
export class BasicExampleCustomActionsComponent {
10+
11+
settings = {
12+
actions: {
13+
add: false,
14+
edit: false,
15+
delete: false,
16+
custom: true
17+
},
18+
custom: [{
19+
action: 'view',
20+
buttonContent: `view `
21+
}, {
22+
action: 'duplicate',
23+
buttonContent: 'duplicate '
24+
}],
25+
columns: {
26+
id: {
27+
title: 'ID',
28+
},
29+
name: {
30+
title: 'Full Name',
31+
},
32+
username: {
33+
title: 'User Name',
34+
},
35+
email: {
36+
title: 'Email',
37+
}
38+
},
39+
};
40+
41+
data = [
42+
{
43+
id: 1,
44+
name: 'Leanne Graham',
45+
username: 'Bret',
46+
email: 'Sincere@april.biz',
47+
},
48+
{
49+
id: 2,
50+
name: 'Ervin Howell',
51+
username: 'Antonette',
52+
email: 'Shanna@melissa.tv',
53+
},
54+
{
55+
id: 3,
56+
name: 'Clementine Bauch',
57+
username: 'Samantha',
58+
email: 'Nathan@yesenia.net',
59+
},
60+
{
61+
id: 4,
62+
name: 'Patricia Lebsack',
63+
username: 'Karianne',
64+
email: 'Julianne.OConner@kory.org',
65+
},
66+
{
67+
id: 5,
68+
name: 'Chelsey Dietrich',
69+
username: 'Kamren',
70+
email: 'Lucio_Hettinger@annie.ca',
71+
},
72+
];
73+
74+
constructor() { }
75+
76+
onCustom(event) {
77+
console.log(event);
78+
}
79+
}

src/app/pages/examples/custom-edit-view/custom-edit-view-examples.component.html

+7
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ <h3>
3535
target="_blank">Demo Source</a>
3636
<basic-example-button-view></basic-example-button-view>
3737
</div>
38+
39+
<h3><a id="multiselect" class="anchor" href="#customactions" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Custom actions</h3>
40+
<p>An example on how to use custom actions:</p>
41+
<div class="with-source">
42+
<a class="source" href="https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/various/basic-example-custom-actions.component.ts" target="_blank">Demo Source</a>
43+
<basic-example-custom-actions></basic-example-custom-actions>
44+
</div>

src/app/pages/examples/examples.module.ts

+3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import { CustomRenderComponent } from './custom-edit-view/custom-render.componen
2020
import { FilterExamplesComponent } from './filter/filter-examples.component';
2121
import { ServerExamplesComponent } from './server/server-examples.component';
2222
import { CustomViewEditExamplesComponent } from './custom-edit-view/custom-edit-view-examples.component';
23+
import { BasicExampleCustomActionsComponent } from './custom-edit-view/basic-example-custom-actions.component';
2324
import { VariousExamplesComponent } from './various/various-examples.component';
25+
2426
import {
2527
BasicExampleButtonViewComponent,
2628
ButtonViewComponent,
@@ -42,6 +44,7 @@ const EXAMPLES_COMPONENTS = [
4244
CustomViewEditExamplesComponent,
4345
VariousExamplesComponent,
4446
BasicExampleButtonViewComponent,
47+
BasicExampleCustomActionsComponent,
4548
ButtonViewComponent,
4649
];
4750

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ChangeDetectionStrategy, Component, Input, Output, EventEmitter } from '@angular/core';
2+
3+
import { Grid } from '../../../lib/grid';
4+
import { Row } from '../../../lib/data-set/row';
5+
6+
@Component({
7+
selector: 'ng2-st-tbody-custom',
8+
changeDetection: ChangeDetectionStrategy.OnPush,
9+
template: `
10+
<a *ngFor="let custom of grid.getSetting('custom')" href="#"
11+
class="ng2-smart-action ng2-smart-action-custom-custom" [innerHTML]="custom.buttonContent" (click)="onCustom(custom, $event)"></a>
12+
`
13+
})
14+
export class TbodyCustomComponent {
15+
16+
@Input() grid: Grid;
17+
@Input() row: Row;
18+
@Input() source: any;
19+
@Output() custom = new EventEmitter<any>();
20+
21+
onCustom(custom: any, event: any) {
22+
event.preventDefault();
23+
event.stopPropagation();
24+
25+
this.custom.emit({
26+
custom: custom,
27+
data: this.row.getData(),
28+
source: this.source
29+
});
30+
}
31+
32+
}

src/ng2-smart-table/components/tbody/tbody.component.html

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<input type="checkbox" class="form-control" [ngModel]="row.isSelected">
44
</td>
55
<td *ngIf="!row.isInEditing && grid.showActionColumn('left')" class="ng2-smart-actions">
6+
<ng2-st-tbody-custom [grid]="grid" (custom)="custom.emit($event)" [row]="row" [source]="source"></ng2-st-tbody-custom>
7+
68
<ng2-st-tbody-edit-delete [grid]="grid"
79
[deleteConfirm]="deleteConfirm"
810
[editConfirm]="editConfirm"

src/ng2-smart-table/components/tbody/tbody.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class Ng2SmartTableTbodyComponent {
1919
@Output() cancel = new EventEmitter<any>();
2020
@Output() edit = new EventEmitter<any>();
2121
@Output() delete = new EventEmitter<any>();
22+
@Output() custom = new EventEmitter<any>();
2223
@Output() edited = new EventEmitter<any>();
2324
@Output() userSelectRow = new EventEmitter<any>();
2425
@Output() editRowSelect = new EventEmitter<any>();

src/ng2-smart-table/components/tbody/tbody.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import { CellModule } from '../cell/cell.module';
77
import { Ng2SmartTableTbodyComponent } from './tbody.component';
88
import { TbodyCreateCancelComponent } from './cells/create-cancel.component';
99
import { TbodyEditDeleteComponent } from './cells/edit-delete.component';
10+
import { TbodyCustomComponent } from './cells/custom.component';
1011

1112
const TBODY_COMPONENTS = [
1213
TbodyCreateCancelComponent,
1314
TbodyEditDeleteComponent,
14-
Ng2SmartTableTbodyComponent,
15+
TbodyCustomComponent,
16+
Ng2SmartTableTbodyComponent
1517
];
1618

1719
@NgModule({

src/ng2-smart-table/lib/grid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Grid {
3232
}
3333

3434
isActionsVisible(): boolean {
35-
return this.getSetting('actions.add') || this.getSetting('actions.edit') || this.getSetting('actions.delete');
35+
return this.getSetting('actions.add') || this.getSetting('actions.edit') || this.getSetting('actions.delete') || this.getSetting('actions.custom');
3636
}
3737

3838
isMultiSelectVisible(): boolean {

src/ng2-smart-table/ng2-smart-table.component.html

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
[editConfirm]="editConfirm"
1818
(edit)="edit.emit($event)"
1919
(delete)="delete.emit($event)"
20+
(custom)="custom.emit($event)"
2021
(userSelectRow)="onUserSelectRow($event)"
2122
(editRowSelect)="editRowSelect($event)"
2223
(multipleSelectRow)="multipleSelectRow($event)"

src/ng2-smart-table/ng2-smart-table.component.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class Ng2SmartTableComponent implements OnChanges {
2121
@Output() delete = new EventEmitter<any>();
2222
@Output() edit = new EventEmitter<any>();
2323
@Output() create = new EventEmitter<any>();
24+
@Output() custom = new EventEmitter<any>();
2425
@Output() deleteConfirm = new EventEmitter<any>();
2526
@Output() editConfirm = new EventEmitter<any>();
2627
@Output() createConfirm = new EventEmitter<any>();
@@ -38,6 +39,7 @@ export class Ng2SmartTableComponent implements OnChanges {
3839
add: true,
3940
edit: true,
4041
delete: true,
42+
custom: false,
4143
position: 'left', // left|right
4244
},
4345
filter: {

0 commit comments

Comments
 (0)