【问题标题】:ERROR : valLists is undefined in pagination of table rows using AngularJS, AJAX错误:使用 AngularJS、AJAX 对表行进行分页时未定义 valLists
【发布时间】:2015-09-29 19:50:16
【问题描述】:

我正在使用此代码的一部分 http://www.jitendrazaa.com/blog/webtech/web/pagination-and-sorting-of-data-table-using-angularjs/ 对 AngularJS 进行分页,但我想使用名为“用户”的 MySQL 数据库表中的数据... 但我面临一个错误,即 valLists 未定义,因此它返回 null 并且什么也不显示....我认为数据类型来自 select.php 导致了这种情况,我不知道如何解决它..任何帮助!

HTML 代码:

<body>
    <br />
        <br />
        <div ng-app="myApp">
            <div ng-controller="TableCtrl">

                <table class="table">
                    <thead>
                        <tr>
                            <th class="id">User Id
                            </th>
                            <th class="name">Name
                            </th>
                            <th class="phone">Phone
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="item in ItemsByPage[currentPage]">
                            <td>{{item.id}}</td>
                            <td>{{item.name}}</td>
                            <td>{{item.phone}}</td>
                        </tr>
                    </tbody>
                </table>

                <ul class="pagination">
                    <li><a href="#" ng-click="firstPage()">First</a>

                    </li>
                    <li ng-repeat="n in range(ItemsByPage.length)"> <a href="#" ng-click="setPage()" ng-bind="n+1">1</a>

                    </li>
                    <li><a href="#" ng-click="lastPage()">Last</a>

                    </li>
                </ul>
             </div>
      </div>
</body>

JavaScript 代码:

var myApp = angular.module('myApp', []);
        myApp.service('ListService', function () {                
            this.paged = function (valLists, pageSize) {
                retVal = [];                  
                for (var i = 0; i < valLists.length; i++) {
                    if (i % pageSize === 0) {
                        retVal[Math.floor(i / pageSize)] = [valLists[i]];
                    } else {
                        retVal[Math.floor(i / pageSize)].push(valLists[i]);
                    }
                }
                return retVal;
            };
        });

        var TableCtrl = myApp.controller('TableCtrl', function ($scope, ListService) {
            $scope.pageSize = 2;
            $scope.allItems = getData();

            $scope.resetAll = function () {
                $scope.List = $scope.allItems;
                $scope.newId = '';
                $scope.newName = '';
                $scope.newPhone = '';
                $scope.currentPage = 0;
            };
           function getData1(){
                 return [{
                         id : 33,
                         name : 'hala',
                         phone: 12345
                 }]
             };
            var d = new Array();
            function getData() {
                   $.ajax ({
                     url: "select.php",
                     type: 'POST',
                     //dataType: "json",
                     data: '',
                     success: function( data ) {
                        d = data;

                        // alert('succ');
                     },
                     error: function(  ) {

                         alert('Error select');
                     }
                     });    
                   //  alert(d);
               return d;
             }
$scope.pagination = function () {
                $scope.ItemsByPage = ListService.paged($scope.List, $scope.pageSize);

            };

            $scope.setPage = function () {
                $scope.currentPage = this.n;
            };

            $scope.firstPage = function () {
                $scope.currentPage = 0;
            };

            $scope.lastPage = function () {
                $scope.currentPage = $scope.ItemsByPage.length - 1;
            };

            $scope.range = function (input) {
                var ret = [];                    
                for (var i = 0; i < input; i++) {
                    if (i != 0 && i != input - 1) {
                        ret.push(i);
                    }
                }
                return ret;
            };

            $scope.resetAll();            
            $scope.pagination();

        });

select.php 代码

<?php
header('Content-Type: application/json');
include 'connect.php';
$db = new database();
$db->setDb_name('training');
$db->connect();

$db->select('user',"*");
$data = $db->getResult();    
echo json_encode($data);  
?>

这里是选择功能:

public function select($table, $rows, $where = null){        
    $case = 0;   

    if($this->tableExists($table))
    {          
       $q = ' SELECT ' .$rows.' FROM '.$table;
        $query = @mysql_query($q);                   

        $this->numresults = mysql_num_rows($query);
        for($i = 0; $i < $this->numresults; $i++)
        {
            $r = mysql_fetch_array($query);
            $key = array_keys($r); 
            for($x = 0; $x < count($key); $x++)
            {
                // Sanitizes keys so only alphavalues are allowed
                if(!is_int($key[$x]))
                {
                    if(mysql_num_rows($query) > 1)
                        $this->result[$i][$key[$x]] = $r[$key[$x]];
                    else if(mysql_num_rows($query) < 1)
                        $this->result = null; 
                    else
                        $this->result[$key[$x]] = $r[$key[$x]]; 
                }
               // $this->result[$i][$key[$x]] = $r[$key[$x]];
            }
        }   
       // print_r ($this->result)."<br/>";


   }
    else
        return false; 

}    

和getResult()函数:

public function getResult() {
    return $this->result;
}

注意1:使用函数 getData1() 而不是 getData() 时效果很好。 注意2:我确信选择功能运行良好并带来如下数据:

[{"id":"3","name":"newname","phone":"321"},
{"id":"4","name":"nnnnnnnnnnn","phone":"66555566"},      {"id":"10","name":"dss","phone":"45321"},    {"id":"11","name":"www","phone":"1234"},    {"id":"15","name":"fghjkl","phone":"34567890"}]

【问题讨论】:

  • 这是因为你不知道asynchronousajax中的含义
  • 你能解释一下吗,或者告诉我该怎么做?

标签: javascript php mysql ajax angularjs


【解决方案1】:

试试我们 Angular 的 $http 服务。像这样的。

var TableCtrl = myApp.controller('TableCtrl', function ($scope, ListService, $http) {
  // ...
  $http.post('select.php')
       .success(function(response) {
          $scope.allItems = response;
       });

   // ...   
}

【讨论】:

  • 同样的错误仍然出现..我使用alert(response)并显示警报包括这个[object Object],[object Object],[object Object]你知道为什么吗?
  • 使用控制台日志查看实际发生的情况。使用 alert() 并没有那么有用。该响应应该包含来自您的 php.ini 的数据。您可以在将数据分配给 $scope.alItems 之前处理逻辑(用于分页)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多