marmelab / ng-admin

Add an AngularJS admin GUI to any RESTful API
http://ng-admin-book.marmelab.com/
MIT License
3.95k stars 725 forks source link

dynamic permanent filters #1350

Open saimirg opened 7 years ago

saimirg commented 7 years ago

Hi, i read that i should ask on StackOverflow and Gitter and some time ago I published the same question on both but got no answer.

Here is my question: The following link is clicked at some point during the workflow to add a subscription record. http://example.com/admin/#/Subscriptions/create?defaultValues={"login_id":1234}

This is part of the creationView code:

.fields([
            nga.field('login_id', 'reference')
        .targetEntity(admin.getEntity('LoginData'))
                .targetField(nga.field('username'))
        .attributes({ placeholder: 'Select Account' })
        .validation({ required: true })
        .label('Username'),

...which when loads, calls the API to the LoginData entity. Our problem is that the response is bringing back all the records and we're not finding a way to pass the _loginid parameter as query parameter so we can filter records on the backend; (or as a permanent filter ?)

So API to LoginData should call http://example.com/api/LoginData?login_id=1234

emresebat commented 7 years ago

Hi

I have small hack for this, using the addFullRequestInterceptor, but I wish we had a better solution.

So for your case it would be something like:

RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params) {
  if (operation == 'getList') {
    //WARN: intercept only for specific view
    if (what === 'LoginData') {
      const hash = location.hash;
      if (hash.includes('Subscriptions/create')) {
        const search = 'defaultValues=';
        const defaultValuesStr = decodeURIComponent(hash.substring(hash.indexOf(search) + search.length));
        const defaultValues = JSON.parse(defaultValuesStr);
        params['login_id'] = defaultValues.login_id;
      }
    }
  }
  return {
    params: params
  };
});