Laravel 4角JS和Twitter的引导3分页 [英] Laravel 4 and Angular JS and Twitter Bootstrap 3 Pagination

查看:194
本文介绍了Laravel 4角JS和Twitter的引导3分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

我需要在我Laravel 4分页 - 角JS应用程序,它使用Twitter的引导3.您可以建议的 angularui自举分页。但我不打算现在就用这个​​吧​​。我需要angular.js探索和利用Laravel分页的功能。我在这里其描述相同见过一个博客文章。但我的运气不好,它不工作并没有在文章中犯了很多错误。

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.

因此​​,基于该文章中,我有一个使用分页这样,请不Laravel控制器功能是,我使用我的转换返回数据数组的toArray()

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
        );
    }
}

现在看看它是如何加载使用angularjs在我的Firebug的控制台数据REST HTTP $资源调用

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

在这里,我有一些分页详细信息,如 per_page CURRENT_PAGE last_page 包括我的数据

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

    };
}

和我的看法,

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

而PAGINATE指令,

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

现在,我在我的HTML表中获取最大的10条记录,分页链接不工作。

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.

推荐答案

我prepared你的情况下,工作的例子 HTTP ://jsfiddle.net/alexeime/Rd8MG/101/ 结果
更改职业为您code服务工作。结果
希望这会帮助你结果
修改结果
编辑的jsfiddle索引号 http://jsfiddle.net/alexeime/Rd8MG/106/

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

Edited jsfiddle with index number http://jsfiddle.net/alexeime/Rd8MG/106/

这篇关于Laravel 4角JS和Twitter的引导3分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆