2017-05-20 11 views
1

私は角度2で単純なアプリケーションを使用しています。データをページテーブルに表示します。私はthis example非常にいいと私のアプリで使用したいと思います。角度2のアプリケーションにdataTableを含めるにはどうすればよいですか?

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css"> 
 
    <link rel="stylesheet" href="https://cdn.datatables.net/select/1.2.2/css/select.dataTables.min.css">

  • 例のhtmlは次のようindex.htmlのスクリプトである、home.component.html

  • cssあります210

    私はこれを動作させるためにJavaスクリプトコードをどこに置くべきか知りたい。 index.htmlhome.compose.htmlを入れようとしましたが、正しく動作しませんでした。

    Javaスクリプトのコードがどこにあるのか教えてください。 ありがとうございます。 これはjavascriptのである:

    $(document).ready(function() { 
        $('#example').DataTable({ 
         columnDefs: [ { 
          orderable: false, 
          className: 'select-checkbox', 
          targets: 0 
         } ], 
         select: { 
          style: 'os', 
          selector: 'td:first-child' 
         }, 
         order: [[ 1, 'asc' ]] 
        }); 
    }); 
    
+0

[Angular2でのjQueryを使用する方法は?]すでにこの上の合理的なリソースであるように思わ(http://stackoverflow.com/questions/30623825/how-to-use-jquery-with-angular2) –

答えて

2

も、まだそれは、1つのコンポーネントで使われている場合は、それらを使用したい場合は、その角度互換性のあるバージョンを使用し、その後、ちょうどあなたのコンポーネントにngOnIntにコードの一部を入れて試してみてくださいこのように、あなたのコンポーネントで何かコードをインポートするインポートを使用します。

import {Component} from "@angular/core"; 
import {$} from "jquery"; 
//also import the datatables plugin for jQuery 


@Component({ 
    selector: "app", 
    templateUrl: "app.html", 
    styleUrls: ["jquery.dataTables.min.css", "select.dataTables.min.css"] 
}); 
export class LoginComponent { 
    constructor() {  
    } 

    ngOnInit() { 
    $('#example').DataTable({ 
    columnDefs: [ { 
     orderable: false, 
     className: 'select-checkbox', 
     targets: 0 
    } ], 
    select: { 
     style: 'os', 
     selector: 'td:first-child' 
    }, 
    order: [[ 1, 'asc' ]] 
    }); 
    } 

} 
+0

あなたの答えに感謝します。私はこのエラーが発生しています:Module '"jquery"'にエクスポートされたメンバ '$'がありません – theCode

+0

jQueryをインストールする方法に依存します。このページから試してみてください:http://stackoverflow.com/questions/39511788/how-to-import- jqueryのツーangular2-typescrypt・プロジェクト:ステップ1:タイプ/ jqueryの ステップ3 @ -Dを NPMをインストールjqueryのためのタイプを追加します:jqueryの ステップ2をインストールし、プロジェクト NPMにjqueryのを取得するには、あなたのコンポーネントでそれを使用します! import * as $ from 'jquery'; すぐに使える$! – Alireza

0
import {Component} from "@angular/core"; import {$} from "jquery"; 
    //also import the datatables plugin for jQuery 


    @Component({ selector: "app", templateUrl: "app.html", 
    styleUrls: ["jquery.dataTables.min.css", 
    "select.dataTables.min.css"] }); export class LoginComponent { 
    constructor() {  } 

     ngOnInit() { 
     $('#example').DataTable({ 
     columnDefs: [ { 
      orderable: false, 
      className: 'select-checkbox', 
      targets: 0 
     } ], 
     select: { 
      style: 'os', 
      selector: 'td:first-child' 
     }, 
     order: [[ 1, 'asc' ]] }); } 

    } 
+0

申し訳ありませんが、エラーが発生しました:モジュール 'jquery'にエクスポートされたメンバー '$'がありません – theCode

+0

問題について話して、コードに説明を追加してください。 –

4

すでにcomponent.tsファイルにインポートする必要はありませんよりも、あなたのHTMLページ内のjQueryのリファレンスを取った場合。それは私のためにうまく動作している下のコードを参照してください。

import { Component, OnInit, ViewChild } from '@angular/core'; 
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 
import { ModalComponent } from 'ng2-bs3-modal/ng2-bs3-modal'; 
import { Observable } from 'rxjs/Rx'; 
import { Global } from '../shared/global'; 
declare var $ : any; 

@Component({ 
    templateUrl: 'app/Component/assignfeatureview.component.html' 
}) 

export class AssignFeatureViewComponent { 
    constructor() { 

    } 
    ngOnInit() { 
     $(document).ready(function() { 
      $('#tblAssignFeature').DataTable(); 
     }); 
    } 
} 
関連する問題