1

I'm trying to add Paging and sorting to my table but I got this error , howerver I follow all the steps which listed here http://l-lin.github.io/angular-datatables/#/getting-started.

I already check the previous problem but I did't work with me

I install all its dependencies

Here's the code of the component :-

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ProductService } from '../../service/product-service.service';
import { Subscription, Subject } from 'rxjs';



@Component({
  selector: 'app-admin-products',
  templateUrl: './admin-products.component.html',
  styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent implements OnInit, OnDestroy {
  

  products: any[];
  filteredProducts: any[];
  subscribtion: Subscription;
  dtOptions: DataTables.Settings = {};
  dtTrigger: Subject<any> = new Subject();

  constructor(private productService: ProductService) {
    this.subscribtion = productService.getAll().
    // We take a copy of Products and Assigned to filteredProducts
    subscribe(
      products => {
      this.filteredProducts = this.products = products;
      this.dtTrigger.next();
      }
      );
  }

  ngOnInit() {
    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 5,
      processing: true
    };

  }
  filter(queryStr: string) {
    // console.log(this.filteredProducts);
    if (queryStr) {
      this.filteredProducts = this.products.
      filter(p => p.payload.val().title.toLowerCase().includes(queryStr.toLowerCase()));
    } else {
      this.filteredProducts = this.products;
    }
  }

  ngOnDestroy(): void {
    // to UnSubscribe
    this.subscribtion.unsubscribe();
  }

}

Here's the code of the the HTML :-
I follow also all the steps here

<p>
    <a routerLink="/admin/products/new" class="btn btn-primary">New Product</a>
</p>

<p>
   <input type="text"
    #query
    (keyup)="filter(query.value)"
   placeholder="Search ..." class="form-control">
</p>
<table 
datatable [dtOptions]="dtOptions"
[dtTrigger]="dtTrigger" class="table" >
    <thead class="thead-dark">
      <tr>
        <th scope="col">Title</th>
        <th scope="col">Price</th>
        <th scope="col">Edit</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let product of filteredProducts">
       
        <td>{{ product.payload.val().title }}</td>
        <td>{{ product.payload.val().price }}</td>
        <td>
            <a [routerLink]="['/admin/products/', product.key]">Edit</a>
        </td>
      </tr>
    </tbody>
  </table>

4
  • 1
    sounds like you are not including jQuery.... Commented Oct 25, 2018 at 20:06
  • If you're not already loading jQuery, you might ask yourself if DataTables is the right move. There are other options that don't require another library. Commented Oct 25, 2018 at 20:12
  • I already added this scripts in angular.json "styles": [ "node_modules/datatables.net-dt/css/jquery.dataTables.css" ], "scripts": [ "node_modules/jquery/dist/jquery.js", "node_modules/datatables.net/js/jquery.dataTables.js" ], Commented Oct 26, 2018 at 19:11
  • In polyfill.ts add import * as jQuery from 'jquery'; window['$'] = jQuery; Commented May 5, 2019 at 17:54

1 Answer 1

5

$ not defined mostly means you are not including JQuery.

try adding: to your program

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js" type="text/javascript"></script>

source

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

4 Comments

I already tried this but not work
It worked Now , But Why It works when I added it in the Index.html and not work in angular.json
In angular you use JQuery, without defining JQuery before doing something in angular it will not work, cause its not defined.
This doesn't work. Try these steps- 1. npm install jquery @types/jquery --save 2. go to angular.json in the project, add 'node_modules/jquery/dist/jquery.min.js' in the scripts: [ ]. 3. Reload project.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.