Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit a85ba5c

Browse files
add ngPipeArgs
1 parent 019da62 commit a85ba5c

File tree

8 files changed

+109
-65
lines changed

8 files changed

+109
-65
lines changed

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@angular/platform-browser": "^13.0.1",
2222
"@angular/platform-browser-dynamic": "^13.0.1",
2323
"@angular/router": "^13.0.1",
24-
"angular-datatables": "^12.0.1",
24+
"angular-datatables": "^13.1.0",
2525
"clipboard": "^2.0.8",
2626
"core-js": "^3.19.1",
2727
"datatables.net": "^1.11.3",

demo/src/app/advanced/using-ng-pipe.component.spec.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { BaseDemoComponent } from '../base-demo/base-demo.component';
88
import { AppRoutingModule } from '../app.routing';
99
import { FormsModule } from '@angular/forms';
1010
import { UsingNgPipeComponent } from './using-ng-pipe.component';
11-
import { UpperCasePipe } from '@angular/common';
11+
import { UpperCasePipe, CurrencyPipe } from '@angular/common';
1212
import { By } from '@angular/platform-browser';
1313
import { Person } from 'app/person';
1414

@@ -37,7 +37,8 @@ describe('UsingNgPipeComponent', () => {
3737
],
3838
schemas: [NO_ERRORS_SCHEMA],
3939
providers: [
40-
UpperCasePipe
40+
UpperCasePipe,
41+
CurrencyPipe
4142
]
4243
}).createComponent(UsingNgPipeComponent);
4344

@@ -74,14 +75,44 @@ describe('UsingNgPipeComponent', () => {
7475
expect(testsArray.map(index => {
7576
const dataRow = rows[index];
7677

77-
const firstNameFromData = (instance.row(dataRow).data() as Person).firstName;
78+
const firstNameFromData = (instance.row(dataRow).data() as Person).firstName;
7879
const firstNameFromTable = $('td:nth-child(2)', dataRow).text();
7980

80-
const lastNameFromData = (instance.row(dataRow).data() as Person).lastName;
81+
const lastNameFromData = (instance.row(dataRow).data() as Person).lastName;
8182
const lastNameFromTable = $('td:nth-child(3)', dataRow).text();
8283
return firstNameFromTable === firstNameFromData.toUpperCase() && lastNameFromTable === lastNameFromData.toUpperCase();
8384
}))
84-
.toEqual(expectedArray);
85+
.toEqual(expectedArray);
86+
});
87+
88+
89+
it('should have money on id column', async () => {
90+
const app = fixture.debugElement.componentInstance as UsingNgPipeComponent;
91+
await fixture.whenStable();
92+
93+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
94+
const dir = query.injector.get(DataTableDirective);
95+
expect(dir).toBeTruthy();
96+
97+
const instance = await dir.dtInstance;
98+
99+
const rows = fixture.nativeElement.querySelectorAll('tbody tr');
100+
101+
const testsArray = [0, 3, 6];
102+
const expectedArray = testsArray.map(_ => true);
103+
104+
expect(testsArray.map(index => {
105+
const dataRow = rows[index];
106+
107+
let pipeInstance = app.pipeCurrencyInstance;
108+
109+
const NumberFromData = (instance.row(dataRow).data() as Person).id;
110+
const NumberFromTable = $('td:nth-child(1)', dataRow).text();
111+
console.log(NumberFromData)
112+
console.log(NumberFromTable)
113+
return NumberFromTable === pipeInstance.transform(NumberFromData,'USD','symbol');
114+
}))
115+
.toEqual(expectedArray);
85116
});
86117

87118
});

demo/src/app/advanced/using-ng-pipe.component.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UpperCasePipe } from '@angular/common';
1+
import { CurrencyPipe, UpperCasePipe } from '@angular/common';
22
import { Component, OnInit } from '@angular/core';
33
import { ADTSettings } from 'angular-datatables/src/models/settings';
44

@@ -9,7 +9,8 @@ import { ADTSettings } from 'angular-datatables/src/models/settings';
99
export class UsingNgPipeComponent implements OnInit {
1010

1111
constructor(
12-
private pipeInstance: UpperCasePipe
12+
private pipeInstance: UpperCasePipe,
13+
public pipeCurrencyInstance: CurrencyPipe
1314
) { }
1415

1516
pageTitle = 'Using Angular Pipe';
@@ -26,8 +27,10 @@ export class UsingNgPipeComponent implements OnInit {
2627
ajax: 'data/data.json',
2728
columns: [
2829
{
29-
title: 'ID',
30-
data: 'id'
30+
title: 'Id (Money)',
31+
data: 'id',
32+
ngPipeInstance: this.pipeCurrencyInstance,
33+
ngPipeArgs: ['USD','symbol']
3134
},
3235
{
3336
title: 'First name',
@@ -41,6 +44,7 @@ export class UsingNgPipeComponent implements OnInit {
4144
}
4245
]
4346
};
47+
4448
}
4549

4650
}

demo/src/app/app.module.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { SelectExtensionComponent } from './extensions/select-extension.componen
3737
import { UsingNgPipeComponent } from './advanced/using-ng-pipe.component';
3838

3939
// Using Angular Pipe
40-
import { UpperCasePipe } from '@angular/common';
40+
import { CurrencyPipe, UpperCasePipe } from '@angular/common';
4141

4242
// Markdown
4343
import { MarkdownModule } from 'ngx-markdown';
@@ -47,52 +47,53 @@ import { UsingNgTemplateRefComponent } from './advanced/using-ng-template-ref.co
4747
import { DemoNgComponent } from './advanced/demo-ng-template-ref.component';
4848

4949
@NgModule({
50-
declarations: [
51-
AppComponent,
52-
WelcomeComponent,
53-
GettingStartedComponent,
54-
PersonComponent,
50+
declarations: [
51+
AppComponent,
52+
WelcomeComponent,
53+
GettingStartedComponent,
54+
PersonComponent,
5555

56-
ZeroConfigComponent,
57-
WithOptionsComponent,
58-
WithAjaxComponent,
59-
AngularWayComponent,
60-
ServerSideAngularWayComponent,
56+
ZeroConfigComponent,
57+
WithOptionsComponent,
58+
WithAjaxComponent,
59+
AngularWayComponent,
60+
ServerSideAngularWayComponent,
6161

62-
CustomRangeSearchComponent,
63-
DtInstanceComponent,
64-
IndividualColumnFilteringComponent,
65-
LoadDtOptionsWithPromiseComponent,
66-
RerenderComponent,
67-
RowClickEventComponent,
68-
MultipleTablesComponent,
69-
RouterLinkComponent,
62+
CustomRangeSearchComponent,
63+
DtInstanceComponent,
64+
IndividualColumnFilteringComponent,
65+
LoadDtOptionsWithPromiseComponent,
66+
RerenderComponent,
67+
RowClickEventComponent,
68+
MultipleTablesComponent,
69+
RouterLinkComponent,
7070

71-
ButtonsExtensionComponent,
72-
ColreorderExtensionComponent,
73-
ResponsiveExtensionComponent,
74-
SelectExtensionComponent,
75-
UsingNgPipeComponent,
76-
BaseDemoComponent,
77-
FAQComponent,
78-
UsingNgTemplateRefComponent,
79-
DemoNgComponent
80-
],
81-
imports: [
82-
BrowserModule,
83-
FormsModule,
84-
HttpClientModule,
85-
DataTablesModule,
86-
AppRoutingModule,
87-
MarkdownModule.forRoot(
88-
{
89-
sanitize: SecurityContext.NONE
90-
}
91-
)
92-
],
93-
providers: [
94-
UpperCasePipe
95-
],
96-
bootstrap: [AppComponent]
71+
ButtonsExtensionComponent,
72+
ColreorderExtensionComponent,
73+
ResponsiveExtensionComponent,
74+
SelectExtensionComponent,
75+
UsingNgPipeComponent,
76+
BaseDemoComponent,
77+
FAQComponent,
78+
UsingNgTemplateRefComponent,
79+
DemoNgComponent
80+
],
81+
imports: [
82+
BrowserModule,
83+
FormsModule,
84+
HttpClientModule,
85+
DataTablesModule,
86+
AppRoutingModule,
87+
MarkdownModule.forRoot(
88+
{
89+
sanitize: SecurityContext.NONE
90+
}
91+
)
92+
],
93+
providers: [
94+
UpperCasePipe,
95+
CurrencyPipe
96+
],
97+
bootstrap: [AppComponent]
9798
})
9899
export class AppModule { }

demo/src/assets/docs/advanced/using-ng-pipe/source-ts.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
// app.module.ts
33
...,
44
providers: [
5-
UpperCasePipe // declare your Pipe here
5+
UpperCasePipe,
6+
CurrencyPipe // declare your Pipe here
67
],
78

89
// using-ng-pipe.component
910

10-
import { UpperCasePipe } from '@angular/common';
11+
import { UpperCasePipe, CurrencyPipe } from '@angular/common';
1112
import { Component, OnInit } from '@angular/core';
1213
import { ADTSettings } from 'angular-datatables/src/models/settings';
1314

@@ -18,7 +19,8 @@ import { ADTSettings } from 'angular-datatables/src/models/settings';
1819
export class UsingNgPipeComponent implements OnInit {
1920

2021
constructor(
21-
private pipeInstance: UpperCasePipe // inject the Pipe
22+
private pipeInstance: UpperCasePipe, // inject the Pipe
23+
private pipeCurrencyInstance: CurrencyPipe // inject the Pipe
2224
) { }
2325

2426
dtOptions: ADTSettings = {};
@@ -29,21 +31,24 @@ export class UsingNgPipeComponent implements OnInit {
2931
ajax: 'data/data.json',
3032
columns: [
3133
{
32-
title: 'ID',
33-
data: 'id'
34+
title: 'Id (Money)',
35+
data: 'id',
36+
ngPipeInstance: this.pipeCurrencyInstance,
37+
ngPipeArgs: ['USD','symbol']
3438
},
3539
{
3640
title: 'First name',
3741
data: 'firstName',
38-
ngPipeInstance: this.pipeInstance // pipe is referred here
42+
ngPipeInstance: this.pipeInstance
3943
},
4044
{
4145
title: 'Last name',
4246
data: 'lastName',
43-
ngPipeInstance: this.pipeInstance // pipe is referred here
47+
ngPipeInstance: this.pipeInstance
4448
}
4549
]
4650
};
51+
4752
}
4853
}
4954

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-datatables",
3-
"version": "13.0.1",
3+
"version": "13.1.0",
44
"description": "Angular directive for DataTables",
55
"scripts": {
66
"build": "npm run clean && npm run compile && npm run bundles && npm run schematics:build",

src/angular-datatables.directive.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ export class DataTableDirective implements OnDestroy, OnInit {
106106
const colsWithPipe = columns.filter(x => x.ngPipeInstance && !x.ngTemplateRef);
107107
colsWithPipe.forEach(el => {
108108
const pipe = el.ngPipeInstance;
109+
const pipeArgs = el.ngPipeArgs ? el.ngPipeArgs : [];
109110
// find index of column using `data` attr
110111
const i = columns.findIndex(e => e.data === el.data);
111112
// get <td> element which holds data using index
112113
const rowFromCol = row.childNodes.item(i);
113-
// Transform data with Pipe
114+
// Transform data with Pipe and PipeArgs
114115
const rowVal = $(rowFromCol).text();
115-
const rowValAfter = pipe.transform(rowVal);
116+
const rowValAfter = pipe.transform(rowVal, ...pipeArgs);
116117
// Apply transformed string to <td>
117118
$(rowFromCol).text(rowValAfter);
118119
});

src/models/settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export interface ADTSettings extends DataTables.Settings {
66
export interface ADTColumns extends DataTables.ColumnSettings {
77
/** Set instance of Angular pipe to transform the data of particular column */
88
ngPipeInstance?: PipeTransform;
9+
/** Define the arguments for the tranform method of the pipe, to change its behavior */
10+
ngPipeArgs?: any[];
911
/** Set `TemplateRef` to transform the data of this column */
1012
ngTemplateRef?: ADTTemplateRef;
1113
}

0 commit comments

Comments
 (0)