【问题标题】:How to use a DataTable with an array of objects?如何将 DataTable 与对象数组一起使用?
【发布时间】:2020-01-29 17:26:39
【问题描述】:

我正在尝试使用属性“数据”在表格中显示我的数据,但表格不起作用。我使用本地字符串数组进行了测试并且效果很好,我不知道为什么使用对象时表格不起作用

ngAfterViewInit() {

  this.listarNotificacao()

  $('#datatables').DataTable({
    "pagingType": "full_numbers",
    "lengthMenu": [
      [10, 25, 50, -1],
      [10, 25, 50, "All"]
    ],
    lengthChange: false,
    responsive: true,
    searching: true,
    data: this.notificacoes,
    columns: [{
        data: 'guidNotificacao'
      },
      {
        data: 'titulo'
      },
      {
        data: 'descricao'
      },
      {
        data: 'escopo'
      },
    ],
    language: {
      search: "_INPUT_",
      searchPlaceholder: "Pesquisar promoções",
      },
    }

  });

  const table = $('#datatables').DataTable();

  $('#inputSearch').on('keyup', function() {
    table.search(this.value).draw();
  });
}
<div class="material-datatables">
  <table id="datatables" class="table  table-no-bordered " cellspacing="0" width="100%" style="width:100%">
    <thead>
      <tr>
        <th>#</th>
        <th>Título</th>
        <th>Descrição</th>
        <th>Escopo</th>
      </tr>
    </thead>
  </table>
</div>

【问题讨论】:

  • 在我看来你有一个额外的}
  • const table = $('#datatables').DataTable(); 不需要在那里。分配 table 您之前构建的数据表对象。
  • 正如@nullptr.t 所说,您正在实例化您的数据表两次。

标签: javascript angular datatable


【解决方案1】:

您的数据表初始化中有错字。语言后面有一个额外的大括号。如果您查看浏览器的开发控制台,它会告诉您这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-24
    • 2016-04-17
    • 1970-01-01
    • 2020-10-13
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    相关资源
    最近更新 更多