编辑:
我需要在我的 Laravel 4 - Angular JS 应用程序中进行分页,该应用程序使用 twitter bootstrap 3 构建。您可以建议angularui-bootstrap pagination。但我现在不打算使用它。我需要用 angular.js 探索和利用 Laravel 分页的特性。我在这里看到了一篇描述相同的博客文章。但我运气不好,它不起作用,文章中有很多错误。
所以基于那篇文章,我有一个 Laravel 控制器函数,它使用这样的分页,请不要那样,我正在使用toArray()
.
class CareerController extends BaseController {
public function index() {
$careers = Career::paginate( $limit = 10 );
return Response::json(array(
'status' => 'success',
'message' => 'Careers successfully loaded!',
'careers' => $careers->toArray()),
200
);
}
}
现在看看它是如何使用 angularjs REST http$resource
调用在我的 Firebug 控制台中加载数据的,
在这里,我有一些分页细节,例如total
, per_page
, current_page
, last_page
,包括我的.from
to
data
现在看看我在用角脚本做什么,
var app = angular.module('myApp', ['ngResource']); // Module for the app
// Set root url to use along the scripts
app.factory('Data', function(){
return {
rootUrl: "<?php echo Request::root(); ?>/"
};
});
// $resource for the career controller
app.factory( 'Career', [ '$resource', 'Data', function( $resource, Data ) {
return $resource( Data.rootUrl + 'api/v1/careers/:id', { id: '@id'}, {
query: {
isArray: false,
method: 'GET'
}
});
}]);
// the career controller
function CareerCtrl($scope, $http, Data, Career) {
// load careers at start
$scope.init = function () {
Career.query(function(response) {
$scope.careers = response.careers.data;
$scope.allCareers = response.careers;
}, function(error) {
console.log(error);
$scope.careers = [];
});
};
}
而我的观点,
<div class="col-xs-8 col-sm-9 col-md-9" ng-controller="CareerCtrl" data-ng-init="init()">
<table class="table table-bordered">
<thead>
<tr>
<th width="4">S.No</th>
<th>Job ID</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="career in careers">
<td style="text-align:center">{{ $index+1 }}</td>
<td>{{ career.job_id }}</td>
<td>{{ career.job_title }}</td>
</tr>
<tr ng-show="careers.length == 0">
<td colspan="3" style="text-align:center"> No Records Found..!</td>
</tr>
</tbody>
</table>
<div paginate="allCareers"></div>
</div><!--/row-->
和分页指令,
app.directive( 'paginate', [ function() {
return {
scope: { results: '=paginate' },
template: '<ul class="pagination" ng-show="totalPages > 1">' +
' <li><a ng-click="firstPage()">«</a></li>' +
' <li><a ng-click="prevPage()">‹</a></li>' +
' <li ng-repeat="n in pages">' +
' <a ng-bind="n" ng-click="setPage(n)">1</a>' +
' </li>' +
' <li><a ng-click="nextPage()">›</a></li>' +
' <li><a ng-click="last_page()">»</a></li>' +
'</ul>',
link: function( scope ) {
var paginate = function( results ) {
if ( !scope.current_page ) scope.current_page = 0;
scope.total = results.total;
scope.totalPages = results.last_page;
scope.pages = [];
for ( var i = 1; i <= scope.totalPages; i++ ) {
scope.pages.push( i );
}
scope.nextPage = function() {
if ( scope.current_page < scope.totalPages ) {
scope.current_page++;
}
};
scope.prevPage = function() {
if ( scope.current_page > 1 ) {
scope.current_page--;
}
};
scope.firstPage = function() {
scope.current_page = 1;
};
scope.last_page = function() {
scope.current_page = scope.totalPages;
};
scope.setPage = function(page) {
scope.current_page = page;
};
};
var pageChange = function( newPage, last_page ) {
if ( newPage != last_page ) {
scope.$emit( 'page.changed', newPage );
}
};
scope.$watch( 'results', paginate );
scope.$watch( 'current_page', pageChange );
}
}
}]);
现在我的 html 表中最多有 10 条记录,分页链接不起作用。
控制台显示Error: results is undefined
分页指令。