1

I found this very cool Angular Datatable library which adds pagination, search and stuff to the table. I works well with predefined table headers but I need to paginate a table who's headers ain't predefined.

I tried following this example on their official documentation, with a few changes of my own, but it gave me this error:

DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7

This is how I tried it:

in my html file

<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="row-border hover"></table>

in my controller

angular.module("app").controller("uploadDataController", ['$scope', 
 'DTOptionsBuilder', 'DTColumnBuilder',
 function($scope, DTOptionsBuilder, DTColumnBuilder) {
 
 SetupScreen();
 function SetupScreen() {
 $scope.dtOptions = DTOptionsBuilder.fromSource('data.json')
 .withPaginationType('full_numbers');
 $scope.dtColumns = [
 DTColumnBuilder.newColumn('id').withTitle('ID'),
 DTColumnBuilder.newColumn('firstName').withTitle('First name'),
 DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
 ];
 }
 }

I'm receiving data from server that can contain any kind of headers so I cannot define the columns.

any ideas?

asked Oct 17, 2016 at 12:39
2

1 Answer 1

3

I think you can use the ng-repeat both for headers and rows data to populate the table .

I Will explain for the data which comes from the server . Change the header also depending on your requirement

By defining The controller this way

angular.module('showcase.angularWay.dataChange', ['datatables', 'ngResource'])
.controller('AngularWayChangeDataCtrl', AngularWayChangeDataCtrl);

By defining the Datatable in this way

$scope.dtOptions = DTOptionsBuilder.newOptions().withPaginationType('full_numbers').withOption('info', false).withOption('bFilter', false).withOption('paging', false);

By defining the columns the following way . This can be as many as columns you need

$scope.dtColumnDefs = [
 DTColumnDefBuilder.newColumnDef(0),
 DTColumnDefBuilder.newColumnDef(1)
 ];

And the HTML code as follows change the data based on your requirement

<table datatable="ng" dt-options="dtOptions" dt-column-defs="dtColumnDefs" class="row-border hover">
 <thead>
 <tr>
 <th>Form ID</th>
 <th>Form Description</th>
 <th>Edit</th>
 </tr>
 </thead>
 <tr ng-repeat="form_elements in View_Forms" >
 <td>{{ form_elements.form_id }} </td>
 <td>{{ form_elements.description }} </td>
 <td ng-click="modify(form_elements.form_id)"><a>Edit</a></td>
 </tr>
 </table>
Bhargav Rao
52.6k29 gold badges130 silver badges142 bronze badges
answered Nov 15, 2016 at 12:59
Sign up to request clarification or add additional context in comments.

4 Comments

Here only the rows are generated dynamic? how to generate headers dynamically?
the following function can be used in columns def to add dynamic column names DTColumnDefBuilder.newColumnDef(0).withOption('title', 'hello')
Ya i have done it!! How it works is, populated Key as columns and Values as row in a datatable from server response as a JSON. @trainoasis
@NischalRevooru colud you please share a snippit to achive the same in angular6 inside the ts component file (If it is possible).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.