首页 > 解决方案 > 如何将 DataTable 与对象数组一起使用?

问题描述

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

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>

在此处输入图像描述

标签: javascriptangulardatatable

解决方案


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


推荐阅读