﻿
{"id":7884,"date":"2018-05-01T22:28:13","date_gmt":"2018-05-01T22:28:13","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=7884"},"modified":"2018-05-01T22:28:13","modified_gmt":"2018-05-01T22:28:13","slug":"angular-4-ile-custom-pipes-olusturmak","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/angular-4-ile-custom-pipes-olusturmak\/","title":{"rendered":"Angular 4 \u0130le Custom Pipes Olu\u015fturmak"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bir \u00f6nceki <a href=\"http:\/\/www.gencayyildiz.com\/blog\/angular-4-uygulamalarinda-built-in-pipes-ile-calismak\/\" rel=\"noopener\" target=\"_blank\">Angular 4 Uygulamalar\u0131nda Built-in Pipes \u0130le \u00c7al\u0131\u015fmak<\/a> ba\u015fl\u0131kl\u0131 makalemizde Angular 4 uygulamalar\u0131nda dahili pipeslara de\u011finmi\u015ftik. Bu i\u00e7eri\u011fimizde ise kendi ihtiyac\u0131m\u0131za istinaden \u00f6zel olu\u015fturaca\u011f\u0131m\u0131z Custom Pipes yap\u0131lar\u0131n\u0131 ele alaca\u011f\u0131z.<\/p>\n<p>Angular projesinde Custom Pipe olu\u015fturabilmek i\u00e7in &#8220;Integrated Terminal&#8221; penceresinde a\u015fa\u011f\u0131daki g\u00f6r\u00fcnt\u00fcde oldu\u011fu gibi generate komutu vermemiz gerekmektedir.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/05\/Angular-4-\u0130le-Custom-Pipes-Olu\u015fturmak.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/05\/Angular-4-\u0130le-Custom-Pipes-Olu\u015fturmak.png\" alt=\"Angular 4 \u0130le Custom Pipes Olu\u015fturmak\" width=\"588\" height=\"55\" class=\"aligncenter size-full wp-image-7887\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/05\/Angular-4-\u0130le-Custom-Pipes-Olu\u015fturmak.png 588w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/05\/Angular-4-\u0130le-Custom-Pipes-Olu\u015fturmak-300x28.png 300w\" sizes=\"auto, (max-width: 588px) 100vw, 588px\" \/><\/a><\/p>\n<p>Olu\u015fturdu\u011fumuz pipe i\u00e7eri\u011fini incelersek e\u011fer;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Pipe, PipeTransform } from '@angular\/core';\r\n@Pipe({\r\n  name: 'product'\r\n})\r\nexport class ProductPipe implements PipeTransform {\r\n  transform(value: any, args?: any): any {\r\n    return null;\r\n  }\r\n}\r\n<\/pre>\n<p>Dikkat ederseniz; bir class\u0131n pipe olabilmesi i\u00e7in &#8220;@Pipe&#8221; deklarasyonundan beslenmesi ve &#8220;PipeTransform&#8221; interfaceini implemente ediyor olmas\u0131 gerekmektedir. &#8220;PipeTransform&#8221; interfacei &#8220;transform&#8221; isminde bir metodu uygulatmaktad\u0131r. \u0130\u015fte bu metot sayesinde, olu\u015fturaca\u011f\u0131m\u0131z bu custom pipe&#8217;in kullan\u0131laca\u011f\u0131 de\u011fer \u00fczerinde bir de\u011fersel d\u00f6n\u00fc\u015f\u00fcm ger\u00e7ekle\u015ftirece\u011fiz. Ayr\u0131ca bu pipe&#8217;\u0131 \u00e7a\u011f\u0131rabilmek i\u00e7in &#8220;@Pipe&#8221; deklarasyonu i\u00e7erisinde &#8220;name&#8221; \u00f6zelli\u011fine &#8220;product&#8221; ismi verilmi\u015ftir. Buraya hangi ismi belirtirseniz o isimle \u00e7a\u011f\u0131rabilirsiniz.<\/p>\n<p>&#8220;transform&#8221; metodunu daha detayl\u0131 inceleyebilmek i\u00e7in a\u015fa\u011f\u0131ya alal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n  transform(value: any, args?: any): any {\r\n    return null;\r\n  }\r\n<\/pre>\n<p>&#8220;value&#8221; parametresi, bu pipe&#8217;\u0131n uygulanaca\u011f\u0131 de\u011feri bize getirecekken, &#8220;args&#8221; parametresi ise pipe&#8217;a verilebilecek parametreleri elde etmemizi sa\u011flayacakt\u0131r. Haliyle burada yap\u0131lan t\u00fcm de\u011fi\u015fiklikler ilgili de\u011fer yerine yans\u0131t\u0131lacak ve bu \u015fekilde filtreleme i\u015flemi ger\u00e7ekle\u015ftirilmi\u015f olacakt\u0131r.<\/p>\n<p>\u015eimdi gelin bu pipe&#8217;\u0131 say\u0131sal de\u011ferler \u00fczerinde kullan\u0131labilir hale getirelim. Mesele bir product nesnesinin fiyat\u0131n\u0131 %18 KDV eklenmi\u015f hale getiren i\u015flevsellikte in\u015fa etmemiz isminede uygun olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Pipe, PipeTransform } from '@angular\/core';\r\n@Pipe({\r\n  name: 'product'\r\n})\r\nexport class ProductPipe implements PipeTransform {\r\n  transform(value: number, args?: number): number {\r\n    return value + (value * 18 \/ 100);\r\n  }\r\n}\r\n<\/pre>\n<p>\u015eimdi pipe&#8217;\u0131 kullanaca\u011f\u0131m\u0131z nesneyi olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Component, OnInit } from '@angular\/core';\r\nimport { Product } from '..\/entitys\/product';\r\n@Component({\r\n  selector: 'app-product',\r\n  templateUrl: '.\/product.component.html',\r\n  styleUrls: &#x5B;'.\/product.component.css']\r\n})\r\nexport class ProductComponent implements OnInit {\r\n  product: Product;\r\n  constructor() { }\r\n  ngOnInit() {\r\n    let product = new Product();\r\n    product.price = 1000;\r\n    this.product = product;\r\n  }\r\n}\r\n<\/pre>\n<p>Ve \u015fimdide olu\u015fturdu\u011fumuz pipe&#8217;\u0131 uygulayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{{product.price | product}}\r\n<\/pre>\n<p>Ekran \u00e7\u0131kt\u0131s\u0131 : &#8220;1180&#8221;<\/p>\n<p>Angular 4 uygulamalar\u0131nda Custom Pipes yap\u0131lar\u0131n\u0131n nas\u0131l in\u015fa edildi\u011fini g\u00f6rm\u00fc\u015f olduk. Art\u0131k bu \u00f6\u011frendiklerimizle bir liste \u00fczerinde filtreleme i\u015flemini \u00f6rneklendirelim ve ard\u0131ndan makalemizi noktalayal\u0131m.<\/p>\n<p>Listelemede kullanaca\u011f\u0131m\u0131z entity s\u0131n\u0131f\u0131m\u0131z;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nexport class Product {\r\n    productName: string;\r\n    introduction: string;\r\n    code: number;\r\n    stock: number;\r\n    price: number;\r\n}\r\n<\/pre>\n<p>Veri k\u00fcmemiz;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Product } from &quot;.\/product&quot;;\r\nexport const ProductList: Product&#x5B;] = &#x5B;\r\n    { productName: &quot;Kalem&quot;, code: 10150, introduction: &quot;...&quot;, price: 500, stock: 1000 },\r\n    { productName: &quot;Silgi&quot;, code: 10160, introduction: &quot;...&quot;, price: 250, stock: 1000 },\r\n    { productName: &quot;Defter&quot;, code: 10170, introduction: &quot;...&quot;, price: 1500, stock: 500 },\r\n    { productName: &quot;Tahta Kalemi&quot;, code: 10180, introduction: &quot;...&quot;, price: 250, stock: 250 },\r\n    { productName: &quot;\u015eeffaf Dosya&quot;, code: 10190, introduction: &quot;...&quot;, price: 10, stock: 150 },\r\n    { productName: &quot;Kalem Tra\u015f&quot;, code: 10200, introduction: &quot;...&quot;, price: 50, stock: 4000 },\r\n    { productName: &quot;Tahta Silgisi&quot;, code: 10210, introduction: &quot;...&quot;, price: 500, stock: 150 }\r\n];\r\n<\/pre>\n<p>Custom Pipe&#8217;\u0131m\u0131z;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Pipe, PipeTransform } from '@angular\/core';\r\nimport { Product } from '..\/entitys\/product';\r\n@Pipe({\r\n  name: 'product'\r\n})\r\nexport class ProductPipe implements PipeTransform {\r\n  transform(value: Product&#x5B;], filterText?: string): Product&#x5B;] {\r\n    return filterText ? value.filter((p: Product) =&gt; p.productName.toLocaleLowerCase().indexOf(filterText.toLocaleLowerCase()) != -1) : value;\r\n  }\r\n}\r\n<\/pre>\n<p>Dikkat ederseniz e\u011fer bir veri k\u00fcmesi \u00fczerinde \u00e7al\u0131\u015faca\u011f\u0131ndan dolay\u0131 &#8220;Product[]&#8221; dizisi tipinden &#8220;value&#8221; parametresinde \u00e7al\u0131\u015fmaktay\u0131z. Ayr\u0131ca pipe kullan\u0131l\u0131rken string tipte bir parametre alacakt\u0131r ve o parametre de\u011ferini burada &#8220;filterText&#8221; isimli parametre ile yakalamaktay\u0131z.<\/p>\n<p>\u015eimdi veri k\u00fcmemizi listeleyelim ve ard\u0131ndan ilgili liste \u00fczerinde pipe&#8217;\u0131m\u0131z\u0131 kullanal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;text&quot; &#x5B;(ngModel)]=&quot;filterText&quot;\/&gt;\r\n\r\n&lt;table style=&quot;width: 550px;&quot;&gt;\r\n  &lt;thead&gt;\r\n    &lt;tr&gt;\r\n      &lt;th&gt;Product Name&lt;\/th&gt;\r\n      &lt;th&gt;Introduction&lt;\/th&gt;\r\n      &lt;th&gt;Code&lt;\/th&gt;\r\n      &lt;th&gt;Stock&lt;\/th&gt;\r\n      &lt;th&gt;Price&lt;\/th&gt;\r\n    &lt;\/tr&gt;\r\n  &lt;\/thead&gt;\r\n  &lt;tbody&gt;\r\n    &lt;tr style=&quot;text-align: center;&quot; *ngFor=&quot;let product of productList | product : filterText&quot;&gt;\r\n      &lt;td&gt;{{product.productName}}&lt;\/td&gt;\r\n      &lt;td&gt;{{product.introduction}}&lt;\/td&gt;\r\n      &lt;td&gt;{{product.code}}&lt;\/td&gt;\r\n      &lt;td&gt;{{product.stock}}&lt;\/td&gt;\r\n      &lt;td&gt;{{product.price}} ({{product.price | product}}+KDV)&lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n  &lt;\/tbody&gt;\r\n&lt;\/table&gt;\r\n<\/pre>\n<p>Yap\u0131lan bu \u00e7al\u0131\u015fmalar neticesinde uygulamay\u0131 kaydedip yay\u0131nlad\u0131\u011f\u0131m\u0131zda a\u015fa\u011f\u0131daki gibi bir i\u015flevsellikle kar\u015f\u0131la\u015faca\u011f\u0131z.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/05\/Angular-4-\u0130le-Custom-Pipes-Olu\u015fturmak.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/05\/Angular-4-\u0130le-Custom-Pipes-Olu\u015fturmak.gif\" alt=\"Angular 4 \u0130le Custom Pipes Olu\u015fturmak\" width=\"528\" height=\"296\" class=\"aligncenter size-full wp-image-7898\" \/><\/a><\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bir \u00f6nceki Angular 4 Uygulamalar\u0131nda Built-in Pipes \u0130le \u00c7al\u0131\u015fmak ba\u015fl\u0131kl\u0131 makalemizde Angular 4 uygulamalar\u0131nda dahili pipeslara de\u011finmi\u015ftik. Bu i\u00e7eri\u011fimizde ise kendi ihtiyac\u0131m\u0131za istinaden \u00f6zel olu\u015fturaca\u011f\u0131m\u0131z Custom Pipes yap\u0131lar\u0131n\u0131 ele alaca\u011f\u0131z. Angular projesinde Custom&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":7898,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2144],"tags":[2333,2332,2334,2331,2335],"class_list":["post-7884","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-4","tag-angular-4-custom-pipes","tag-angular-4-ile-custom-pipes","tag-angular-4-ile-custom-pipes-olusturmak","tag-custom-pipes","tag-pipetransform"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/7884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=7884"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/7884\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/7898"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=7884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=7884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=7884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}