bellotas
bellotas

Reputation: 2461

How to call a method when creating row of table - Angular 2

I need to call a method every time that I create a row of a table, but I do not really know how to do it.

I have tried:

HTML

<div class="table-responsive">
    <table class="table table-bordred table-striped ">
        <tbody >
            <tr *ngFor="let car of cars; let i = index;" (onCreate)="formatXml(car.ID)">
                <td>{{_xmlFormated}}</td>
            </tr>
        </tbody>
    </table>
</div>

Method

     formatXml(xml) {

        xml = '';
        var reg = /(>)(<)(\/*)/g;
        xml = xml.replace(reg, '$1\r\n$2$3');
        var pad = 0;
        jQuery.each(xml.split('\r\n'), function (index, node) {
          var indent = 0;
          if (node.match(/.+<\/\w[^>]*>$/)) {
            indent = 0;
          } else if (node.match(/^<\/\w/)) {
            if (pad != 0) {
              pad -= 1;
            }
          } else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
            indent = 1;
          } else {
            indent = 0;
          }

          var padding = '';
          for (var i = 0; i < pad; i++) {
            padding += '  ';
          }

          xml += padding + node + '\r\n';
          pad += indent;
        });
return xml
      }

But I do not call the method. Any suggestion?

Upvotes: 0

Views: 439

Answers (1)

Gurkan Yesilyurt
Gurkan Yesilyurt

Reputation: 2675

Create format-xml.pipe.ts and type as follows:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'formatXml'})
export class FormatXmlPipe implements PipeTransform {
  transform(xml: string): string {
     // your logic
    return xml;
  }
}

Declare it in the module that you will use it:

@NgModule({
  declarations: [ FormatXmlPipe ],
  ...
})
export class AppModule { }

And use it in the template:

{{car.ID | formatXml}}

Upvotes: 1

Related Questions