5

I used jQuery dataTable for display data on list view. And I tried to bind data to dataTable using following way.

On users.component.ts

getUsers() {
    this.UserService.getUsersList()
        .subscribe(
        success => {
            this.userList = success;
            $("#userTable").find('tbody').empty();
            var dataClaims = this.userList;
            for (let i = 0; i < dataClaims.length; i++) {
                $('#userTable').dataTable().fnAddData([
                    (i + 1),
                    dataClaims[i].name,
                    dataClaims[i].email,
                    dataClaims[i].contact_number,
                    dataClaims[i].address,
                    '<a [routerLink]="[' +"'"+"../../user/update" +"'" +']"' + ' class="fa fa-1x fa-pencil-square-o"></a>',
                ]);
            }
        }
        );
} 

Above function is working properly and dataTable is working without issue.

But [routerLink] is not converted to html. On the output it is displayed following way,

<a [routerlink]="['../../user/update']" class="fa fa-1x fa-pencil-square-o"></a>

But it should be converted to following way,

<a _ngcontent-c0="" ng-reflect-router-link="user/update" href="/user/update" class="fa fa-1x fa-pencil-square-o"></a>

Could someone please explain how to convert [routerlink] to normal link when render html data from a .ts file. Thank you.

4
  • first , i don't think this is the best way to use HTML injection in DOM. Still if you want to use then you can try to sanitize it , then it will work Commented Dec 20, 2017 at 6:07
  • Instead of using JQuery's dataTable. You might want to check PrimeNG. primefaces.org/primeng/#/datatable . It's much cleaner and there's no DOM injection Commented Dec 20, 2017 at 6:52
  • @Ashitoshbirajdar Even when you try to sanitize it, the code will not get run through Angular's compiler. Commented Dec 29, 2017 at 15:12
  • (PS. It's also a bad idea to mix Angular and jQuery together.) Commented Dec 29, 2017 at 15:12

3 Answers 3

2
+25

If you really want to use dataTable, maybe this can be helpful for you:

https://stackoverflow.com/a/38983367/2624360

Basically you should create an Directive that encapsulate your dataTable logic.

import {Directive, ElementRef} from '@angular/core';
import {Input, OnInit}         from '@angular/core';


import { JqueryDataTableEvent } from './jquery-datable-event';
import 'jquery.dataTables';

declare var jQuery:any;

@Directive({
    selector: '[jqueryDatatable]'
})

export class JqueryDatatableDirective implements OnInit {
    private _datatable : any;

    @Input()
    jqueryDatatable: any;

    @Input()
    dataTableEvents: JqueryDataTableEvent[];

    constructor(private _element: ElementRef) {}

    ngOnInit() {
        this.applyOptions();
        this.applyEvents();
    }

    applyOptions()
    {
        if (!this.jqueryDatatable)
            console.error("Empty options array was passed to initialize jqueryDatatable.");

        this._datatable = jQuery(this._element.nativeElement).DataTable( this.jqueryDatatable || {} );

    }

    applyEvents() {
        this.dataTableEvents.map((event)=> {
            this._datatable.on(event.eventName, event.selector, event.callback)
        });
    }
}

Someone (@DarioN1) create and example with this:

https://plnkr.co/edit/t0Zwc3AtQTt98XvIirZ9?p=preview

Sign up to request clarification or add additional context in comments.

Comments

1

[routerLink] doesn't work. You need to use innerHTML functionality.

ts file:-

 tool = '<a href="../../user/update" class="fa fa-1x fa-pencil-square-o">View</a>';

html file:-

<div [innerHTML]="tool"></div>

Output:-

<div _ngcontent-c1=""><a href="../../user/update" class="fa fa-1x fa-pencil-square-o">View</a></div>

2 Comments

The OP wanted routerLink and not innerHTML.
Seems not to work. Testet it with a small example. The innerHTML prop isnt doing anything.
0

You have to tell Angular that your HTML is safe:

  1. import DomSanitizer;
  2. inject DomSanitizer in your Component constructor;
  3. wrap your link in bypassSecurityTrustHtml.

 import { DomSanitizer } from '@angular/platform-browser';
 
 export class UserComponent {
   constructor(private sanitizer: DomSanitizer) {}

   getUsers() {
     this.UserService.getUsersList()
      .subscribe(
        success => {
          this.userList = success;
          $("#userTable").find('tbody').empty();
          var dataClaims = this.userList;
          for (let i = 0; i < dataClaims.length; i++) {
              $('#userTable').dataTable().fnAddData([
                  (i + 1),
                  dataClaims[i].name,
                  dataClaims[i].email,
                  dataClaims[i].contact_number,
                  dataClaims[i].address,
                  this.sanitizer.bypassSecurityTrustHtml('<a [routerLink]="[' +"'"+"../../user/update" +"'" +']"' + ' class="fa fa-1x fa-pencil-square-o"></a>'),
              ]);
          }
      }
      );
   } 
 }

2 Comments

Could you please explain that using my code sample.
Even if you use DomSanitizer, it still does not compile it through Angular.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.