1

Sorry if it is a silly question since it is covered in the documentation, but I can't make it work (I am new to AngularJS).

I am trying to write an interface to my database using ng-admin on top of a django-rest-framework. The problem is that the default pagination in ng-admin is like _page=1&_perPage=30 while in django-rest-framework it's ?limit=30&offset=1. So when ng-admin tries to GET it, it recives a 301 response. In the documentation it says:

For instance, to use offset and limit instead of _page and _perPage across the entire application, use the following code:

myApp.config(['RestangularProvider', function(RestangularProvider) {
   RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
       if (operation == 'getList' && what == 'entityName') {
           params.offset = (params._page - 1) * params._perPage;
           params.limit = params._perPage;
           delete params._page;
           delete params._perPage;
        }
        return { params: params };
    }); 
}]);

The problem is that I don't know exactly how to incorporate this into my code, which is:

var myApp = angular.module('myApp', ['ng-admin']);
myApp.config(['NgAdminConfigurationProvider', function (nga) {
    var admin = nga.application('My First Admin')
        .baseApiUrl('http://localhost:8000/CSP_site/'); 
    var algae = nga.entity('algae');
    algae.listView().fields([
        nga.field('name'),
        nga.field('id'),
    ]);
    admin.addEntity(algae);
    nga.configure(admin);
}]);

Any hint will be appreciated.

Thanks!

Omar Einea
  • 2,478
  • 7
  • 23
  • 35

1 Answers1

1

This should do it for you:

var myApp = angular.module('myApp', ['ng-admin']);
myApp.config(['NgAdminConfigurationProvider', 'RestangularProvider', function (nga, RestangularProvider) {
    var admin = nga.application('My First Admin')
        .baseApiUrl('http://localhost:8000/CSP_site/'); 
    var algae = nga.entity('algae');
    algae.listView().fields([
        nga.field('name'),
        nga.field('id'),
    ]);
    admin.addEntity(algae);
    nga.configure(admin);

    RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig) {
        if (operation == 'getList' && what == 'algae') {
            params.offset = (params._page - 1) * params._perPage;
            params.limit = params._perPage;
            delete params._page;
            delete params._perPage;
        }
        return { params: params };
    }); 
}]);

Added RestangularProvider in the second index of the array, which injects it as the second parameter in the .config function.

The rest is copy paste except what == 'entityName', it became what == 'algae' as algae is the entity you added above.

I hope that helps.

Omar Einea
  • 2,478
  • 7
  • 23
  • 35
  • Thanks for the quick replay, but unfortunately it is still trying to GET _page _perPage... don't know why!! Django version 1.11, using settings 'CSP.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. [02/Jan/2018 02:30:57] "GET /CSP_site/algae?_page=1&_perPage=30&_sortDir=DESC&_sortField=id HTTP/1.1" 301 0 Any suggestion? – Leonardo Hardtke Jan 02 '18 at 02:32
  • Oh that's probably because of the `if` condition. I updated the answer, check the last couple of lines. – Omar Einea Jan 02 '18 at 03:23
  • Oh! I see... Thanks! I think I understand what the problem was! Now there is something else... Still 301 but because a diferent reason. GET /CSP_site/algae?_page=1&_perPage=30&_sortDir=DESC&_sortField=id HTTP/1.1" 301 0 there is a `/` missing after algae... after a few trials the best I coudl get is GET /CSP_site/algae%2F?_sortDir=DESC&_sortField=id&limit=30&offset=0 HTTP/1.1 any idea how to add the `/` after algae in the request? – Leonardo Hardtke Jan 02 '18 at 03:36
  • hmmm, can't you add a `/` to the URL you're requesting.. like `URL`? – Omar Einea Jan 02 '18 at 03:45
  • 1
    Thanks, but I read a few docs, and I think I have to fix it from the djang-rest router site... Thanks again for your help – Leonardo Hardtke Jan 02 '18 at 03:51