Laravel 4 and Angular JS and Twitter Bootstrap 3 Pagination

大城市里の小女人 提交于 2019-12-05 04:56:52

问题


Edit:

I need a pagination in my Laravel 4 - Angular JS application which is structured using twitter bootstrap 3. You may suggest the angularui-bootstrap pagination. But I am not planning to use this right now. I need to explore and utilize the features of Laravel pagination with angular.js. I have seen one blog article here which describe the same. But my bad luck, it is not working and there is a lot of mistakes in the article.

So based on that article, I have a Laravel controller function which uses pagination like this, Please not that, I am converting my return data to array using 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
        );
    }
}

Now see how it is loaded the data in my Firebug console using angularjs REST http $resource call,

Here I have some pagination details such as total, per_page, current_page, last_page, from and to including my data.

And now look what I am doing in angular script,

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 = [];
        }); 

    };
}

And my view,

<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-->

And the paginate directive,

app.directive( 'paginate', [ function() {
    return {
      scope: { results: '=paginate' },
      template: '<ul class="pagination" ng-show="totalPages > 1">' +
               '  <li><a ng-click="firstPage()">&laquo;</a></li>' +
               '  <li><a ng-click="prevPage()">&lsaquo;</a></li>' +
               '  <li ng-repeat="n in pages">' +
               '    <a ng-bind="n" ng-click="setPage(n)">1</a>' +
               '  </li>' +
               '  <li><a ng-click="nextPage()">&rsaquo;</a></li>' +
               '  <li><a ng-click="last_page()">&raquo;</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 );
     }
   }
 }]);

Now I am getting maximum 10 records in my html table, the pagination links are not working.

Console shows Error: results is undefined with the pagination directive.


回答1:


I prepared a working example for your case http://jsfiddle.net/alexeime/Rd8MG/101/
Change Career service for your code to work.
Hope this will help you
EDIT:
Edited jsfiddle with index number http://jsfiddle.net/alexeime/Rd8MG/106/




回答2:


You haven't bound your careers to results. Just make it: paginate="careers" instead. However, I did realise there's a mistake there in my original article as well - and that is that in your paginate directive, where it defines scope - it should look like this:

scope: { results: '=paginate' },

What this is doing, is telling our directive to bind "results" to the $scope object, like so:

$scope.results

This will then bind to the result set (in this case, careers) and use that as the basis for the pagination work.

Hope that helps!



来源:https://stackoverflow.com/questions/19817540/laravel-4-and-angular-js-and-twitter-bootstrap-3-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!