1

I wan't to use bootstrap datatable with angularJS - here's my code : https://codepen.io/bafu2203/pen/VzBVmy

As you see, when i want to list in table datas from $http.get call, the table doesn't work. But it work great, when i ng-repeat from $scope.data

HTML :

<div class="container" ng-app="formvalid">
  <div class="panel" data-ng-controller="validationCtrl">
  <div class="panel-heading border">    
    <h2>Data table using jquery datatable in Angularjs
    </h2>
  </div>
  <div class="panel-body">
      <table class="table table-bordered bordered table-striped table-condensed datatable" ui-jq="dataTable" ui-options="dataTableOpt">
      <thead>
        <tr>
          <th>#</th>
          <th>Name</th>
          <th>Position</th>

        </tr>
      </thead>
        <tbody>
          <tr ng-repeat="n in data">
            <td>{{$index+1}}</td>
            <td>{{n.name}}</td>
             <td>{{n.system}}</td>

          </tr>
        </tbody>
    </table>

     <table class="table table-bordered bordered table-striped table-condensed datatable" ui-jq="dataTable" ui-options="dataTableOpt">
      <thead>
        <tr>
          <th>#</th>
          <th>Name</th>
          <th>Position</th>

        </tr>
      </thead>
        <tbody>
          <tr ng-repeat="n in test">
            <td>{{$index+1}}</td>
            <td>{{n.Name}}</td>
             <td>{{n.City}}</td>

          </tr>
        </tbody>
    </table>
  </div>
</div>
</div>

JS :

var app=angular.module('formvalid', ['ui.bootstrap','ui.utils']);
app.controller('validationCtrl',function($scope, $http, $timeout){
  $scope.getapi = function(){
        $http({
            method: 'GET',
            url: 'https://www.w3schools.com/angular/customers_mysql.php',
        })
            .then(function successCallback(data) {
                $scope.test = data.data;
                console.log($scope.test);
                $timeout($scope.getapi, 1000);
            }, function errorCallback(response) {
                console.log(response);
                console.log('error');
            });

    };
    $scope.getapi();


  $scope.data=[
  {
    "name" : "Tiger Nixon",
    "system" : "System Architect"
  },
  {
    "name" : "Tiger asd",
    "system" : "System Architect"
  },
  {
    "name" : "Tiger d",
    "system" : "System gadfadgf"
  },
  {
    "name" : "Tiger Nixon",
    "system" : "gadsf Architect"
  },
  {
    "name" : "Tiger Nixon",
    "system" : "asdd Architect"
  }
];



$scope.dataTableOpt = {
   //custom datatable options 
  // or load data through ajax call also
  "aLengthMenu": [[10, 50, 100,-1], [10, 50, 100,'All']],
  "aoSearchCols": [
      null
    ],
  };
});

Thanks for answers in advance!

1 Answer 1

2

You have to access it the data like this:

$scope.test = data.data.records;
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but now sorting, and filtering doesn't work in table with ng-repeat
Have you any idea why?
because the data comes inside an object and inside a key called records :)
Check the result of data to see the architecture of the json output

Your Answer

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.