编辑:
我需要在我的 Laravel 4 - Angular JS 应用程序中使用分页,它是使用 twitter bootstrap 3 构建的。你可以建议 angularui-bootstrap pagination .但我现在不打算使用它。我需要使用 angular.js 探索和利用 Laravel 分页的功能。我看过一个博客article here描述相同。但我运气不好,它不起作用,文章中有很多错误。
所以基于那篇文章,我有一个使用 pagination 的 Laravel Controller 函数。像这样,请注意,我正在使用 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
, 从
和到
,包括我的数据
。
现在看看我在 Angular 脚本中做了什么,
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
和分页指令。
最佳答案
我为您的案例准备了一个工作示例http://jsfiddle.net/alexeime/Rd8MG/101/
更改 Career
服务以使您的代码正常工作。
希望对你有帮助
编辑:
用索引号编辑 jsfiddle http://jsfiddle.net/alexeime/Rd8MG/106/
关于javascript - Laravel 4 和 Angular JS 以及 Twitter Bootstrap 3 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19817540/