【问题标题】:Angular + Jquery Datatable responsivness not workingAngular + Jquery Datatable响应不起作用
【发布时间】:2021-08-04 19:39:05
【问题描述】:

我正在使用以下扩展名https://l-lin.github.io/angular-datatables/#/getting-started

让我的 jquery 数据表响应。在我的 angular.json 文件中,我添加了以下几行。

        "styles": [
          "node_modules/datatables.net-dt/css/jquery.dataTables.css",
          "node_modules/bootstrap/dist/css/bootstrap.min.css",
          "node_modules/font-awesome/css/font-awesome.css",
          "node_modules/material-icons/css/material-icons.css",
          "node_modules/datatables.net-colreorder-dt/css/colReorder.dataTables.css",
          "node_modules/ngx-toastr/toastr.css",
          "src/styles.css"
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.js",
          "node_modules/datatables.net/js/jquery.dataTables.js",
          "node_modules/datatables.net-colreorder/js/dataTables.colReorder.js"
        ]

然后我在我的 app.module 中导入了 DataTableModule

imports: [
    CommonModule,
    BrowserModule,
    DataTablesModule,
    HttpClientModule,
    BrowserAnimationsModule,
    ToastrModule.forRoot(),
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: httpTranslateLoader,
        deps: [HttpClient]
      }
    }),
    FormsModule,
    ReactiveFormsModule,
    AppRoutingModule,
    NgbModule
  ],

然后我创建了下表,这里是该表的 html

<table datatable [dtOptions]="dtOptions" class="display table table-striped table-bordered dt-responsive row-border hover" *ngIf="employees">
  <thead>
    <tr>
      <th>{{ 'employee.Number' | translate }} </th>
      <th>{{'employee.UserName' | translate}}</th>
      <th>
        <span class="material-icons" (click)="addEmployee()" href="javascript:void(0);">
          add_box
        </span>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let employee of employees">
      <td>{{ employee.id }}</td>
      <td>{{ employee.userName }}</td>
      <td>
        <a [title]="employee.id + ' employee-details'" [routerLink]="['/employees', employee.id]">
          <span class="material-icons md-18">
            edit
          </span>
        </a>
      </td>
    </tr>
  </tbody>
</table>

这是表格的打字稿文件。

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Employee } from '../models/Employee';
import { UserService } from '../services/employee.service';

@Component({
  selector: 'app-employees',
  templateUrl: './employees.component.html',
  styleUrls: ['./employees.component.css']
})
export class EmployeesComponent implements OnInit {
  dtOptions: DataTables.Settings = {};
  public employees: Employee[];

  constructor(private employeeService: UserService ,private http: HttpClient) { }


  ngOnInit(): void {
    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 10,
      processing: true,
      responsive: true,
      columnDefs: [{
        targets: [6],
        orderable: false,
        width: "50px"
      }]
    };
    this.employeeService.getEmployees().subscribe(result => {
      this.employees = result;
    }, error => {
      console.log(error)
    });
  }

}

很遗憾,它不起作用任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 错误是什么?
  • @danday74 我在终端和 Visual Studio 2019 控制台中都没有看到任何错误。但是当屏幕较小时,我没有在我的 jquery 数据表中看到带有加号的绿色圆圈。

标签: angular datatables


【解决方案1】:

请访问Download Page,并确保选择您计划在 DataTable 中使用的所有内容。在您的 angular.json 文件中,我可以告诉您缺少实际启用 responsive: true, 选项所需的两个文件。

您尚未包含用于 DataTables 响应选项的 JS 脚本和 CSS 样式表。

以下是下载脚本和样式表的链接: https://cdn.datatables.net/responsive/2.2.9/css/responsive.dataTables.min.css https://cdn.datatables.net/responsive/2.2.9/js/dataTables.responsive.min.js

下载它们并确保在 Angular.json 文件中正确引用它们。

        "styles": [
          "node_modules/datatables.net-dt/css/jquery.dataTables.css",
          "node_modules/bootstrap/dist/css/bootstrap.min.css",
          "node_modules/font-awesome/css/font-awesome.css",
          "node_modules/material-icons/css/material-icons.css",
          "node_modules/datatables.net-colreorder-dt/css/colReorder.dataTables.css",
          "node_modules/datatables.net-responsive-dt/css/responsive.dataTables.min.css",
          "node_modules/ngx-toastr/toastr.css",
          "src/styles.css"
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.js",
          "node_modules/datatables.net/js/jquery.dataTables.js",
          "node_modules/datatables.net-colreorder/js/dataTables.colReorder.js",
          "node_modules/datatables.net-responsive/js/dataTables.responsive.min.js"
        ]

【讨论】:

  • 谢谢。谢谢它有效。但我不明白为什么我在 index.html 中的引用不起作用,但在 index.html 中添加时它起作用。你能解释一下区别吗?
  • @JuniorCortenbach 抱歉回复晚了,但是 Angular 有自己的方式来初始化你的全局 css/js 源文件。这是通过将它们添加到位于您的angular.json 中的stylesscripts 数组中。如果要添加 index.html 的 head 标签,则必须将其链接到正确的节点模块。即&lt;script src="node_modules/datatables.net-responsive/js/dataTables.responsive.min.js"&gt;&lt;/script&gt;
  • 如果我的回答有帮助,请不要忘记奖励赏金:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多