【问题标题】:DataTable with AngularJS $http.get带有 AngularJS 的数据表 $http.get
【发布时间】:2018-02-03 05:36:09
【问题描述】:

我不想将引导数据表与 angularJS 一起使用 - 这是我的代码:https://codepen.io/bafu2203/pen/VzBVmy

如您所见,当我想从 $http.get 调用中列出表数据时,该表不起作用。但它工作得很好,当我 ng-repeat 来自 $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
    ],
  };
});

提前感谢您的回答!

【问题讨论】:

    标签: javascript jquery angularjs twitter-bootstrap datatable


    【解决方案1】:

    你必须像这样访问它的数据:

    $scope.test = data.data.records;
    

    【讨论】:

    • 是的,但是现在排序和过滤在使用 ng-repeat 的表格中不起作用
    • 你知道为什么吗?
    • 因为数据来自一个对象和一个名为 records 的键 :)
    • 查看数据结果看json输出的架构
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多