Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 22, 2016 @ 07:26
    Michaël Vanbrabandt
    0

    Angularjs pagination issue custom plugin

    Hi all,

    I am building a new content section in the umbraco backoffice where I display a table which will contains some data.

    To minify the rows I am implementing pagination using the umb-pagination directive.

    However this doesn't work and I do not see the pagination. If I use static valus in the directive then it works.

    Below you can find all the code I use to get this to work..

    View

      <table class="notely-table notely-table-bordered">
               <thead>
                         <tr>
                               ....
                         </tr>
               </thead>
               <tbody>
                        <tr ng-repeat="comment in $parent.filteredComments = (comments | filterLogType: options.type | pagination: pagination)">
                                 .....
                        </tr>
               </tbody>
       </table>
    
       <!-- Begin umb-pagination -->
       <umb-pagination page-number="pagination.current"
                 total-pages="pagination.total"
                 on-next="next()"
                 on-prev="prev()"
                 on-go-to-page="goto()">
       </umb-pagination><!-- End umb-pagination -->
    

    Controller

        $scope.comments = [];
        $scope.filteredComments = [];
    
        $scope.pagination = {
            current: 1,
            limit: 10,
            total: $scope.filteredComments.length
        };
    
        // Load
        $scope.load = function () {
            // Get all comments
            notelyResources.getAllComments().then(function (data) {
                $scope.comments = noteCommentsBuilder.convert(data);
    
                $scope.loaded = true;
            });
        };
    

    Filters

    angular.module('notely.filters').filter('pagination',
    
        function () {
            return function (elements, pagination) {
                var filtered = [];
    
                if (pagination) {
    
                    var begin, end, index;
    
                    begin = (pagination.current - 1) * pagination.limit;
                    end = begin + pagination.limit;
    
                    angular.forEach(elements, function (element) {
                        index = elements.indexOf(element);
    
                        if (begin <= index && index < end)
                            filtered.push(element);
                    });
    
                } else {
                    return elements;
                }
    
                return filtered;
            };
        }
    
    );
    

    It displays the first 10 elements, even with the filterLogType so tis works fine. But rendering the pagination gives issues.

    If you view the values of the pagination object I see the correct values but the pagination wouldn't render.

    Is it because of the resouce I use to get the data from the API? And with the extra filter?

    Any help is welcome, stuck with this for a few hours now!

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 22, 2016 @ 09:38
    Michaël Vanbrabandt
    0

    UPDATE

    Ok I got it working for the first time run.

    It gives me the correct amount of pages in the pagination.

    The reason is we need to update the pageNumber so that the directive re-runs because it contains a $watch on this property.

    But it doesn't has one for the totalPages.

    Can we extend the directive to watch also on the totalPages to re-run the directive?

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 22, 2016 @ 11:08
    Michaël Vanbrabandt
    0

    Ok solved.

    Instead of filtering on client side I filter the results in the api and send these back.

    Then totalPages and everything else gets updated.

    /Michaël

  • Bilal Haidar 144 posts 410 karma points
    Sep 22, 2016 @ 11:53
    Bilal Haidar
    0

    Hi Michael, how are you filtering on the server? You pass the filter text to the server? Or is there a built-in service in Umbraco to help on that?

    /Thanks :)

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 22, 2016 @ 12:44
    Michaël Vanbrabandt
    0

    I pass the value of my filter as a param to my api controller.

    Its even better this way then on the client side because else it needs to load in all of the data to do the filtering.

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 22, 2016 @ 12:49
    Michaël Vanbrabandt
    1

    You can do this in the following way:

    1. Create resource js file and include this in your package manifest
    2. Add factory

      angular.module('notely.resources').factory('notelyResources',
          function ($q, $http, umbRequestHelper) {
              return {
                   // Get all elements
                  get: function (param) {
                      return umbRequestHelper.resourcePromise(
                          $http.get("path_to_the_api_controller", { params: { filter: param} }),
                          "Unable to get all the elements!"
                      );
                  },
              };
          }
      );
      

    /Michaël

  • Bilal Haidar 144 posts 410 karma points
    Sep 22, 2016 @ 13:56
    Bilal Haidar
    1

    Thanks for sharing :)

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 22, 2016 @ 14:27
    Michaël Vanbrabandt
    1

    No problem Bilal!

    Thats why we are here for!

    /Michaël

  • Bilal Haidar 144 posts 410 karma points
    Sep 22, 2016 @ 15:15
    Bilal Haidar
    1

    Actually, I found this link, might give some more ideas also: http://24days.in/umbraco/2015/custom-listview/

    /Bilal

Please Sign in or register to post replies

Write your reply to:

Draft