Skip to content

Commit

Permalink
Pipes personalizados
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrotomassini committed Feb 20, 2022
1 parent 64c46b6 commit a121e31
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 8 deletions.
40 changes: 40 additions & 0 deletions workspace/04-pipesApp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions workspace/04-pipesApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"private": true,
"dependencies": {
"@angular/animations": "~13.2.0",
"@angular/cdk": "^13.2.3",
"@angular/common": "~13.2.0",
"@angular/compiler": "~13.2.0",
"@angular/core": "~13.2.0",
Expand Down
6 changes: 5 additions & 1 deletion workspace/04-pipesApp/src/app/prime-ng/prime-ng.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import { ButtonModule } from 'primeng/button';
import { CardModule } from 'primeng/card';
import { MenubarModule } from 'primeng/menubar';
import { FieldsetModule } from 'primeng/fieldset';
import { ToolbarModule } from 'primeng/toolbar';
import { TableModule } from 'primeng/table';

@NgModule({
exports: [
ButtonModule,
CardModule,
MenubarModule,
FieldsetModule
FieldsetModule,
ToolbarModule,
TableModule
]
})
export class PrimeNgModule { }
3 changes: 2 additions & 1 deletion workspace/04-pipesApp/src/app/shared/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class MenuComponent implements OnInit {
},
{
label: 'Pipes personalizados',
icon: 'pi pi-cog'
icon: 'pi pi-cog',
routerLink: '/ordenar'
}
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum Color {
rojo, negro, azul, verde
}

export interface Heroe {
nombre: string;
vuela: boolean;
color: Color;
}
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
<p>ordenar works!</p>
<div class="text-layout">

<h1>Pipes personalizados</h1>
<p>Pipes creados por {{ 'nosotros' | mayusculas:enMayusculas }}</p>

</div>

<p-toolbar>
<div class="p-toolbar-group-left">

</div>

<div class="p-toolbar-group-right">
<p-button label="En myúsculas" icon="pi pi-check" styleClass="p-button-md" (onClick)="cambiarTamanio()"
class="mr-1">
</p-button>
<button pButton icon="pi pi-sort" label="Por nombre" class="mr-1 p-button-warning"
(click)="cambiarOrden('nombre')"></button>
<button pButton icon="pi pi-sort" label="Vuela" class="mr-1 p-button-success "
(click)="cambiarOrden('vuela')"></button>
<button pButton icon="pi pi-sort" label="Por color" class="mr-1 p-button-help"
(click)="cambiarOrden('color')"></button>
</div>
</p-toolbar>

<div class="grid">
<div class="col mt-2">
<p-table [value]="heroes | ordenar: ordenarPor " responsiveLayout="scroll">
<ng-template pTemplate="header">
<tr>
<th>Nombre</th>
<th>Vuela</th>
<th>Color</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-heroe>
<tr>
<td>{{heroe.nombre | titlecase}}</td>
<td>{{heroe.vuela | vuela | titlecase }}</td>
<td>{{heroe.color }}</td>
</tr>
</ng-template>
</p-table>
</div>
</div>


<div class="grid">
<div class="col mt-2">
<p-table [value]="heroes | slice:0" responsiveLayout="scroll">
<ng-template pTemplate="header">
<tr>
<th pSortableColumn="nombre">Nombre <p-sortIcon field="nombre"></p-sortIcon>
</th>
<th pSortableColumn="vuela">Vuela <p-sortIcon field="vuela"></p-sortIcon>
</th>
<th pSortableColumn="color">Color <p-sortIcon field="color"></p-sortIcon>
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-heroe>
<tr>
<td>{{heroe.nombre | titlecase}}</td>
<td>{{heroe.vuela | vuela | titlecase }}</td>
<td>{{heroe.color }}</td>
</tr>
</ng-template>
</p-table>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
import { Component, OnInit } from '@angular/core';
import { Component } from '@angular/core';
import { Color, Heroe } from '../../interfaces/ventas.interface';

@Component({
selector: 'app-ordenar',
templateUrl: './ordenar.component.html',
styles: [
]
})
export class OrdenarComponent implements OnInit {
export class OrdenarComponent {

constructor() { }
enMayusculas: boolean = false;

ngOnInit(): void {
ordenarPor: string = '';

heroes: Heroe[] = [
{
nombre: 'Superman',
vuela: true,
color: Color.azul
},
{
nombre: 'Bataman',
vuela: false,
color: Color.negro
},
{
nombre: 'Robin',
vuela: false,
color: Color.verde
},
{
nombre: 'Dart Devil',
vuela: false,
color: Color.rojo
},
{
nombre: 'Linterna Verde',
vuela: true,
color: Color.verde
},
];


cambiarTamanio() {
(this.enMayusculas) ? this.enMayusculas = false : this.enMayusculas = true;
}

cambiarOrden(valor: string){
this.ordenarPor = valor;
}
}
13 changes: 13 additions & 0 deletions workspace/04-pipesApp/src/app/ventas/pipes/mayusculas.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
name: 'mayusculas'
})
export class MayusculasPipe implements PipeTransform {

transform(valor: string, enMayusculas: boolean = true): string {
return (enMayusculas) ? valor.toUpperCase() : valor.toLowerCase();
}

}

25 changes: 25 additions & 0 deletions workspace/04-pipesApp/src/app/ventas/pipes/ordenar.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Heroe } from '../interfaces/ventas.interface';

@Pipe({
name: 'ordenar'
})
export class OrdenarPipe implements PipeTransform {

transform(heroes: Heroe[], ordenarPor: string = 'sin valor'): Heroe[] {

switch (ordenarPor) {
case 'nombre':
return heroes = heroes.sort((a, b) => (a.nombre > b.nombre) ? 1 : -1);
case 'vuela':
return heroes = heroes.sort((a, b) => (a.vuela > b.vuela) ? -1 : 1);
case 'color':
return heroes = heroes.sort((a, b) => (a.color > b.color) ? 1 : -1);

default:
return heroes;
}

}

}
12 changes: 12 additions & 0 deletions workspace/04-pipesApp/src/app/ventas/pipes/vuela.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
name: 'vuela'
})
export class VuelaPipe implements PipeTransform {

transform(vuela: boolean): string {
return (vuela) ? 'vuela' : 'no vuela';
}

}
11 changes: 10 additions & 1 deletion workspace/04-pipesApp/src/app/ventas/ventas.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ import { NumerosComponent } from './pages/numeros/numeros.component';
import { NoComunesComponent } from './pages/no-comunes/no-comunes.component';
import { BasicosComponent } from './pages/basicos/basicos.component';
import { OrdenarComponent } from './pages/ordenar/ordenar.component';
import { MayusculasPipe } from './pipes/mayusculas.pipe';
import { VuelaPipe } from './pipes/vuela.pipe';
import { OrdenarPipe } from './pipes/ordenar.pipe';



@NgModule({
declarations: [
// Componentes
NumerosComponent,
NoComunesComponent,
BasicosComponent,
OrdenarComponent
OrdenarComponent,

// Pipes
MayusculasPipe,
VuelaPipe,
OrdenarPipe
],
exports: [
NumerosComponent,
Expand Down

0 comments on commit a121e31

Please sign in to comment.