dev*_*evo 2 javascript laravel angularjs laravel-4 twitter-bootstrap-3
编辑:
我需要在我的Laravel 4 - Angular JS应用程序中使用twitter bootstrap 3进行分页.您可以建议使用angularui-bootstrap分页.但我现在不打算使用它.我需要探索并利用带有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
);
}
}
Run Code Online (Sandbox Code Playgroud)
现在看看如何使用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 = [];
});
};
}
Run Code Online (Sandbox Code Playgroud)
我的看法,
<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-->
Run Code Online (Sandbox Code Playgroud)
和paginate指令,
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 );
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
现在我在html表中获得最多10条记录,分页链接不起作用.
控制台显示Error: results is undefined
分页指令.
我为您的案例准备了一个工作示例http://jsfiddle.net/alexeime/Rd8MG/101/
更改Career
服务以使您的代码正常工作.
希望这会帮助你
编辑:
编辑jsfiddle与索引号http://jsfiddle.net/alexeime/Rd8MG/106/
归档时间: |
|
查看次数: |
7310 次 |
最近记录: |